Object Oriented Programming Using: Abdul Wahab
Object Oriented Programming Using: Abdul Wahab
++ C
[email protected]
1
Abdul Wahab
Lecturer University of Science and Technology Bannu
Object
Any thing which has some properties and some behaviors. Object is an instance of a class.
Class
A class is group 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 userdefined 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.
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;
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
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
Object can access public member variables and functions by using operators dot( . ) and arrow ( -> ) [object name] [operator] [member name] e.g. a.show(); b->show(); Where a is normal object Where b is pointer object
Example
class item { int codno; float price; int qty; }; void main() {
Optional
class item a; clrscr(); a.codeno=123; // Direct a.price=150.00; // Access a.qty=15; // Not allowed
10
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 { public: int codno; float price; int qty; };
void main() { class item a; clrscr(); a.codeno=123; a.price=150.00; a.qty=15; }
12
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 { int codno; float price; int qty; public: void set_show(); };
void item :: set_show() { codeno=123; price=150.00; qty=15; cout<< codno <<endl; cout<< price <<endl; cout<< qty <<endl; } void main() { clrscr(); item a; a.set_show(); }
14
Output
123 150.00 15
15
16
Access Mechanism
17
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
In Private Section
class item{ int codno; float price; int qty; void values() { codeno=125; price=150.00; qty=200; } public: void show() { values(); cout<<Code No = << codno; cout<<Price = << price; cout<<Quantity = << qty; } };
20
int main() { clrscr(); item one; // one.values(); /// Not Accessible one.show();
return 0; }
void item :: show() { codeno=125; price=150.00; qty=200; cout<<Code No = << codno; cout<<Price = << price; cout<<Quantity = << qty; }
21