Sunteți pe pagina 1din 22

Object Oriented Programming Using

++ C
abdul_bu@yahoo.com
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.

General Form of a Class


class class_name { private: //private data and functions public: //public data and function }object_list;

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

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(); 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 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 { public: int codno; float price; int qty; };
void main() { class item a; clrscr(); a.codeno=123; 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 { 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

The Protected Keyword

Access mechanism of protected keyword is same as private keyword It is used in inheritance

16

Access Mechanism

Access Specifier Public Private Protected

Member Functions Allowed Allowed Allowed

Class Objects Allowed Not 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; float price; int qty; public: void show() { codeno=125; price=150.00; qty=200; cout<<Code No = << codno; cout<<Price = << price; cout<<Quantity = << qty; } };
19

int main() { clrscr(); item one; one.show(); return 0; }

Member Function Inside the Class


B.

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; }

Member Function Outside the Class


class item{ int codno; float price; int qty; public: void show(); }; int main() { clrscr(); item one; 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

Have a Nice Day!

S-ar putea să vă placă și