Object
Oriented
Programming
Using
C ++
Abdul Wahab abdul_bu@yahoo.com
Lecturer
University of Science and Technology Bannu
1
Object
Any thing which has some properties and some behaviors.
Object is an instance of a class.
2
Class
A class is grouping of objects having identical properties, common
behavior and shared relationship.
Class: Car
Properties: Company, Model, Color and Capacity etc.
Action: Speed() and Break().
Class: Computer
Properties: Brand, Price, Processor Speed and Ram etc.
Action: Processing(), Display() and Printing().
The entire group of data and code of an object can be built as a user-
defined data type using class.
3
Access Specifiers
Private:
The private members of a class are accessible only by the member
functions or friend functions of the class.
Public:
The public members are accessible from any where in the program.
Protected:
The protected members of a class are accessible by the member
functions of the class and the classes which are derived from this class.
4
General Form of a Class
class class_name
{
private:
//private data and functions
public:
//public data and function
}object_list;
5
Declaring Objects
Defining objects of class is known as “class instantiation”
When objects are created only during that moment, memory is allocated
to them
int x, y;
char ch;
item a, b, *c;
6
Properties of Objects
It is individual
It holds data as well as operation method that handles data
Its scope is limited to the block in which it is defined
7
The class has a mechanism to prevent direct access to its members,
which is the central idea of object oriented programming
Object cannot directly access private members
Private members can only be accessed by public member function of the
same class
8
Accessing Class Members
Object can access public member variables and functions by using
operators dot( . ) and arrow ( -> )
[object name] [operator] [member name]
e.g. a.show(); Where a is normal object
b->show(); Where b is pointer object
9
Example
class item void main()
{ { Optional
int codno;
float price; class item a;
int qty; clrscr();
}; a.codeno=123; // Direct
a.price=150.00; // Access
a.qty=15; // Not allowed
}
10
The Public Keyword
The keyword public can be used to allow object to access the member
variables of a class directly
Public is written inside the class terminated by colon. e.g.
class Sample
{
public:
// Public Members
};
11
Example
class item void main()
{ {
public: class item a;
int codno; clrscr();
float price; a.codeno=123;
int qty; a.price=150.00;
}; a.qty=15;
}
12
The Private Keyword
It is used to prevent direct access to members of the class
By default private access specifier is used
Private members can be accessed through the public member function
13
Example
class item void item :: set_show()
{ {
codeno=123;
int codno;
price=150.00;
float price; qty=15;
int qty; cout<< codno <<endl;
public: cout<< price <<endl;
cout<< qty <<endl;
void set_show();
}
};
void main()
{
clrscr();
item a;
a.set_show();
}
14
Output
123
150.00
15
15
The Protected Keyword
Access mechanism of protected keyword is same as private keyword
It is used in inheritance
16
Access Mechanism
Access Specifier Member Functions Class Objects
Public Allowed Allowed
Private Allowed Not Allowed
Protected Allowed Not Allowed
17
Defining Member Functions
The member functions must be declared inside the class. They can be
defined:
– In Public or Private section of a class
– Inside or Outside the class
18
Member Function Inside the Class
A. In Public Section:
class item{
int codno; int main()
float price; {
int qty; clrscr();
public: item one;
void show() one.show();
{
codeno=125; return 0;
price=150.00; }
qty=200;
cout<<“Code No = ”<< codno;
cout<<“Price = ”<< price;
cout<<“Quantity = ”<< qty;
}
};
19
Member Function Inside the Class
B. In Private Section
class item{
int codno; int main()
float price; {
int qty;
clrscr();
void values()
{ item one;
codeno=125; // one.values(); /// Not Accessible
price=150.00; one.show();
qty=200;
}
public: return 0;
void show() }
{
values();
cout<<“Code No = ”<< codno;
cout<<“Price = ”<< price;
cout<<“Quantity = ”<< qty;
}
};
20
Member Function Outside the Class
class item{
int codno;
float price;
int main()
int qty;
{
public:
clrscr();
void show();
item one;
};
one.show();
void item :: show()
return 0;
{
}
codeno=125;
price=150.00;
qty=200;
cout<<“Code No = ”<< codno;
cout<<“Price = ”<< price;
cout<<“Quantity = ”<< qty;
}
21
Have a Nice Day!