object oriented programming (oops) class object data hiding abstraction encapsulation polymorphism...

28
Object Oriented Programming (OOPs) Class Object Data Hiding Abstraction Encapsulation Polymorphism Inheritance fference between Procedural and OOPs program Advantages of OOPs 1. Deals with real life objects, so provide ease in programming. 2. Provides means for handling data security etc. 3. Promotes reusability of code, thus programming and testing becomes easy and fast.

Upload: francis-hodges

Post on 28-Dec-2015

279 views

Category:

Documents


1 download

TRANSCRIPT

Object Oriented Programming (OOPs)

ClassObjectData HidingAbstractionEncapsulationPolymorphismInheritance

Difference between Procedural and OOPs programming

Advantages of OOPs

1. Deals with real life objects, so provide ease in programming.

2. Provides means for handling data security etc.

3. Promotes reusability of code, thus programming and testing becomes easy and fast.

class emp{ int empno; char empname[20]; float basic;public:

void readdata(){ cin>>empno;

gets(empname);cin>>basic;

}void displaydata(){ cout<<empno;

cout<<empname;cout<<basic;

}};// end of class

Member Functions

Data members

keyword Class name(user defined Data type)

emp e1; //object (class variable)

Access Specifiers

A class has 3 levels of visibility mode.

1. Public:

Public members are accessible outside the class

Public members are accessible to both member Functions and non-member Functions.

Objects can access the members directly using dot operator.e.g object name.public data member

object name.public member function

class emp{ int empno; char empname[20]; float basic;public: char address[20];

void readdata(){ cin>>empno;

gets(empname);gets(address);cin>>basic;

}void displaydata(){ cout<<empno;

cout<<empname;cout<<basic<<address;

}};// end of class

emp e1; Outside the class we can access

e1.address //OKObject name.public data member

e1.readdata() //OKObject name.public member function

e1.displaydata()//OKObject name.public member function

Public members are accessible outside the class

void main(){cin>>e1.address //OKObject name.public data member e1.readdata() //OKObject name.public member function}

class emp{ int empno; char empname[20]; float basic;public: char address[20];

void readdata(){ cin>>empno;

gets(empname);gets(address);cin>>basic;

}void displaydata(){ cout<<empno;

cout<<empname;cout<<basic<<address;

}};// end of class

class emp{ int empno; char empname[20]; float basic;public: char address[20];

void readdata(){ cin>>empno;

gets(empname);gets(address);cin>>basic;

}void displaydata(){ cout<<empno;

cout<<empname;cout<<basic<<address;

}};// end of class

Public members are accessible to member Functions

Objects can access the public data members and member functions directly using dot operator.

void main(){cin>>e1.address //OKObject name.public data member

e1.readdata() //OKObject name.public member function}

class emp{ int empno; char empname[20]; float basic;public: char address[20];

void readdata(){ cin>>empno;

gets(empname);gets(address);cin>>basic;

}void displaydata(){ cout<<empno;

cout<<empname;cout<<basic<<address;

}};// end of class

class emp{ int empno; char empname[20]; float basic;public:

void readdata(){ cin>>empno;

gets(empname);cin>>basic;

}void displaydata(){ cout<<empno;

cout<<empname;cout<<basic;

}};// end of class

void main(){ emp e1; //To input details cin>>e1.empno; //NOT OK ? e1.readdata(); //OK //To Display details e1.displaydata();// OK}

WHY ?

Private Visibility mode

Private members can be accessed only by a member function of that class

They cannot be accessed by any non member function(data is hidden from outside world)

Object of a class cannot access private members using dot operator.

All data at the beginning of a class by default is privateuntil any specifier is mentioned.

class emp{ int empno; char empname[20]; float basic;public:

void readdata(){ cin>>empno;

gets(empname);cin>>basic;

}void displaydata(){ cout<<empno;

cout<<empname;cout<<basic;

}};// end of class

Private members can be accessed only by a member function of thatclass

class emp{ int empno; char empname[20]; float basic;public:

void readdata(){ cin>>empno;

gets(empname);cin>>basic;

}void displaydata(){ cout<<empno;

cout<<empname;cout<<basic;

}};// end of class

void main(){ emp e1;

e1.empno; //NOT OK}

Object of a class cannot access private members using dot operator.

All data at the beginning of a class by default is private until any specifier is mentioned.

class emp{ int empno; char empname[20]; float basic;public:

void readdata(){ cin>>empno;

gets(empname);cin>>basic;

}void displaydata(){ cout<<empno;

cout<<empname;cout<<basic;

}};// end of class

Member Functions

They define the set of operations that may be applied to objects of that class. In a way these functions are used to define the behaviour of Objects of the class and the way the data members will interact with other functions of the same or another class.

class emp{ int empno; char empname[20]; float basic;public:

void readdata(){ cin>>empno;

gets(empname);cin>>basic;

}void displaydata(){ cout<<empno;

cout<<empname;cout<<basic;

}};// end of class

Member Function Declaration

Inside the class

If the member functions are defined inside the class then there is no need for function prototype

class emp{ int empno; char empname[20]; float basic;public:

void readdata(){ cin>>empno;

gets(empname);cin>>basic;

}void displaydata(){ cout<<empno;

cout<<empname;cout<<basic;

}};// end of class

Outside the class

class emp{ int empno; char empname[20]; float basic;public: void readdata(); void displaydata();

};// end of class

Function Prototype

Larger functions are defined outside the class, it is done with scope Resolution operator. It requires Function prototype inside the class.

void emp :: displaydata(){ cin>>empno;

gets(empname);cin>>basic;

}

void emp :: readdata(){ cout<<empno;

cout<<empname;cout<<basic;

}

return_data_type class_name :: function_name() - syntax

Function DefinitionOutside the class

Nesting of Member Function

A member function of a class can be called by another member functionOf the same class.takedata() is calling

display()

class emp{ float basic; void display() { cout<<basic; }public:

void takedata() { cin>>basic; display(); }}; void main(){

emp e1; e1.takedata();

}

Arrays within a class

Arrays can be used a data members in a class. If an array is declared as private, then only member functions can access it. If it is public, can be accessed outside the class also.

class company{ public:

int no_of_emp[100];int no_of_dept[30];

private:int no_of_units[20];

};

Arrays of ObjectsWe can have arrays of class objects.class emp{ int empno; char empname[20]; float basic;public:

void readdata(){ cin>>empno;

gets(empname);cin>>basic;

}void displaydata(){ cout<<empno;

cout<<empname;cout<<basic;

}};// end of class

void main(){ emp e[100];//ARRAY of objects for(int I=0;I<100;I++) {

e[I].readdata();e[I].displaydata();

}

Memory Allocation of Objects

When a class is defined, memory is allocated for its Member functions and they are stored in the memory Once.

When an object is created, separate memory space is allocated for its data members.

All objects work with one copy of member functionsShared by all.

class emp{ int empno; char empname[20]; float basic;public:

void readdata(){ cin>>empno;

gets(empname);cin>>basic;

}void displaydata(){ cout<<empno;

cout<<empname;cout<<basic;

}};// end of class

class emp member functionsreaddata( ) displaydata( )

Memory created when Member functions are defined

emp e1,e2;

e1 e2empno

empname

basic

empno

empname

basic

Memory created when objects are declared

Passing Objects as Function Arguments

By value

A copy of the entire object is passed as an argument to a Function any modification made to the object inside the Function is not reflected in the object itself.

class emp{ int empno; char empname[20]; float basic;public:void read(){cin>>empno; gets(empname);cin>>basic;}void disp (){cout<<empno;cout<<empname;cout<<basic; }void mod(emp empl1)//by value{empl1.empno=10; }};// end of class

void main(){ emp e1,e2;e1.read (); e2.read();e1.mod(e2);e2.disp();}

class emp{ int empno; char empname[20]; float basic;public: void readdata(){ cin>>empno>>basic;

gets(empname); }void displaydata(){ cout<<empno<<basic;

cout<<empname; } void modify(emp temp)//By Value{ temp.empno=2; }};// end of class

void main(){ emp e1; e1.readdata( ); emp e2; e2.readdata( ); e2.modify(e1); e1.displaydata(); e2.displaydata( ); }

Passing Objects as Function Arguments

By Reference

A copy of the object is not passed as an argument rather the Member Function works directly on the actual object used In the call. Any modification made to the object inside the Function is reflected in the object itself.

class emp{ int empno; char empname[20]; float basic;public:void read(){cin>>empno; gets(empname);cin>>basic;}void disp (){cout<<empno;cout<<empname;cout<<basic; }void mod(emp &empl1)//by reference{empl1.empno=10; }};// end of class

void main(){ emp e1,e2;e1.read (); e2.read();e1.mod(e2);e2.disp();}

class dist{ float feet,inches;public:void read(){cin>>feet>>inches; }void disp (){cout<<feet<<inches;}dist sum(dist);};// end of class

void main(){ dist l1,l2;l1.read();l2.read();dist l3=l1.sum(l2);l3.disp();}

dist dist::sum(dist d2){ dist d3;d3.feet=feet+2d.feet+(inches+d2.inches)/12; d3.inches=(inches+d2.inches)%12;return d3;}

Function returning objects: Function can also return objects