Presentation is loading. Please wait.

Presentation is loading. Please wait.

Object Oriented Programming (OOPs) Class Object Data Hiding Abstraction Encapsulation Polymorphism Inheritance Difference between Procedural and OOPs programming.

Similar presentations


Presentation on theme: "Object Oriented Programming (OOPs) Class Object Data Hiding Abstraction Encapsulation Polymorphism Inheritance Difference between Procedural and OOPs programming."— Presentation transcript:

1 Object Oriented Programming (OOPs) Class Object Data Hiding Abstraction Encapsulation Polymorphism Inheritance 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.

2 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)

3 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

4 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 //OK Object name.public data member e1.readdata() //OK Object name.public member function e1.displaydata()//OK Object name.public member function

5 Public members are accessible outside the class void main() { cin>>e1.address //OK Object name.public data member e1.readdata() //OK 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

6 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

7 Objects can access the public data members and member functions directly using dot operator. void main() { cin>>e1.address //OK Object name.public data member e1.readdata() //OK 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

8 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 ?

9 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 private until any specifier is mentioned.

10 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 that class

11 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.

12 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

13 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

14 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

15 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.

16 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 Definition Outside the class

17 Nesting of Member Function A member function of a class can be called by another member function Of 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(); }

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

19 Arrays of Objects We 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(); }

20 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 functions Shared by all.

21 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 functions readdata( ) displaydata( ) Memory created when Member functions are defined emp e1,e2; e1 e2 empno empname basic empno empname basic Memory created when objects are declared

22 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.

23 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(); }

24 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( ); }

25 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.

26 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(); }

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

28 Function returning objects: Function can also return objects


Download ppt "Object Oriented Programming (OOPs) Class Object Data Hiding Abstraction Encapsulation Polymorphism Inheritance Difference between Procedural and OOPs programming."

Similar presentations


Ads by Google