0% found this document useful (0 votes)
7 views

00 CH_4_OOP Lec1

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

00 CH_4_OOP Lec1

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Computer Programming

Lec. #

Chapter 4

Classes and Objects


Classes and Objects

• Introduction
• Properties of OOP
• Class Definition
• Class Examples
• Objects
• Constructors
• Destructors 2
Introduction
• Classes and Objects are the basic building
blocks of Object Oriented Programming.

• Properties of OOP:
– Encapsulation: collect both data and functions in one unit called
class.
– Data Hiding: hide data from mistakenly accessed by using the
access modifiers keywords: private, public or protected.
– Inheritance: is the ability to take an existing class and make a
new modified class from the original. The new class takes all the
properties of the original class plus additional prosperities, Ex.,
adding new member variables and member functions.
– Polymorphism: is the art of taking advantage versatile feature
that brings Object Oriented Methodologies to its full potential.
Class Basics
• What is a Class?
– A class is an Abstract Data Type (ADT) used to
define a new structure data types.
– A class combines data fields (member
variables), operations that can be performed on
data (functions) and access
modifiers/specifiers.
Where a class can be written in a program?
– A class can be written within the header part of
the program.

Note that, the header part may contain more than one class.
Class Declaration
• Classes are generally declared using the keyword class,
with the following format:

• class: Keyword or reserved word in C++


• Class_Name: any valid identifier
• Class Members (Data/Variable members and Function members)
• Access modifier/specifier: an operator for access control
Class
• Class
–A user defined type
–Consists of both data and
methods
–Defines properties and behavior
of that type

6
Define a Class Type
Header
class: is a keyword
class class_name class_name: is any name
{ {
permission_label:
Access Modifiers
member;
Body permission_label: Data or function
member; members
...
};
};

7
Define a Class Type

Header class Rectangle


class class_name {
{ private:
permission_label:
int width;
member;
Body permission_label:
int length;
member; public:
... void set(int w, int l);
}; int area();
};
8
Classes & Objects
Objects: Instance of a class
class Rectangle
{
Rectangle r1;
private: Rectangle r2;
int width; Rectangle r3;

……
int length;
public:
void set(int w, int l);
int area(); int a;
};
9
Class Definition: Data Members
• Can be of any type, built-in or user-defined
• non-static data member
– Each class object has its own copy

• static data member


– Acts as a global variable
– One copy per class type, e.g. counter

10
Static Data Member
Rectangle r1;
class Rectangle Rectangle r2;
{ Rectangle r3;
private:
int width;
count
int length;
static int count; r1 r2
public: width width
length length
void set(int w, int l);
int area(); width
r3 length
} 11
Class Definition: Member Functions
• Used to
– access the values of the data members (accessor)
– perform operations on the data members
(implementor)
• Are declared inside or outside the class body
• Their definition can be placed inside the class
body, or outside the class body
• Can access both public and private members of
the class
• Can be referred to using dot or arrow member
access operator
12
Define a Member Function
class Rectangle EX:
{ r1.set(5,8);
private:
int width, length;
class name
public:
void set (int w, int l);
int area() {return width*length; } member function name
};

void Rectangle :: set (int w, int l)


inline {
width = w;
length = l;
scope operator
}
13
Class Definition - Access Control

• Information hiding
– To prevent the internal representation from direct
access from outside the class
• Access Specifiers
– public
• may be accessible from anywhere within a program
– private
• may be accessed only by the member functions, and friends
of this class
– protected
• acts as public for derived classes
• behaves as private for the rest of the program
14
class Time Specification
class Time
{
public :
void Set ( int hours , int minutes , int seconds ) ;
void Increment ( ) ;
void Write ( ) const ;
Time ( int initHrs, int initMins, int initSecs ) ; // constructor
Time (); // default constructor
private :
int hrs ;
int mins ;
int secs ;
};
15
15
Class Interface Diagram
Time class

Set
Private data:
Increment
hrs
Write
mins
Time secs

Time

16
Class Definition - Access Control
• The default access specifier is private
• The data members are usually private or protected
• A private member function is a helper, may only be
accessed by another member function of the same
class (exception friend function)
• The public member functions are part of the class
interface
• Each access control section is optional,
repeatable, and sections may occur in any order

17
What is an object?

OBJECT
set of methods
Operations (member functions)

Data internal state


(values of private data members)

18
Declaration of an Object
class Rectangle
main()
{ {
private: Rectangle r1;
Rectangle r2;
int width;
int length; r1.set(5, 8);
public: cout<<r1.area()<<endl;

void set(int w, int l); r2.set(8,10);


int area(); cout<<r2.area()<<endl;
}
};
19
Declaration of an Object
r1 is statically allocated
class Rectangle
{ main()
private: {
Rectangle r1;
int width;
r1.set(5, 8);
int length;
}
public:
void set(int w, int l); r1
width = 5
int area(); length = 8
};
21

You might also like