Presentation is loading. Please wait.

Presentation is loading. Please wait.

By Muhammad Waris Zargar

Similar presentations


Presentation on theme: "By Muhammad Waris Zargar"— Presentation transcript:

1 By Muhammad Waris Zargar
inheritance By Muhammad Waris Zargar

2

3 Inheritance Introduction Advantages Categories of inheritance
Specifying A derive class syntax: class derive class name: access specifier parent class;

4 Specifying A Derive Class
Accessing Members Of Parent Class Accessing Constructor Of Parent Class Accessing Member Function Of Parent Class Function Overriding Types Of Inheritance Public Inheritance Protected Inheritance Private Inheritance Multi Level Inheritance

5 Multi Level Inheritance with parameter
Multiple Inheritance Constructor In Multiple Inheritance Constructor without parameter Constructor with parameter Ambiguity In Multiple Inheritance Removing Ambiguity Containership

6 Introduction Person Reuse an existence class.
New class inherit all behavior of original class. It has also some additional properties. The existing class is called super , parent , base class. New class is called derived , base , child. Example: Person Student Teacher Labour

7 Advantages Reusability. Save time and effort.
Increase program structure and reliability.

8 Categories Parent Parent1 Parent2 Child Child
Single inheritance Multiple Inheritance Parent Parent1 Parent2 Child Child

9 Access Specifier Access Specifier Main Class Derive Class
Main Function Private Yes No Protected Public

10 Accessing Members Of Parent Class
Important point is accessing member of parent class using derive class object. class waris{ public: Int num; }; Class haris : public waris{}; Int main(){ Haris h; h.num=20; Cout << “ Number is : : “ << h.num; }

11 Accessing Constructor Of Parent Class
Important point is accessing member of parent class using derive class object. class waris{ public: Int main(){ Waris(){cout <<“I am in parent class”;} Haris h; } }; Class haris : public waris{ Haris() : waris(){ Cout <<“I am derive class constructor”;

12 Accessing Constructor Of Parent Class
Important point is accessing member of parent class using derive class object. class waris{ public: Int main(){ Waris(int a){cout <<“I am in parent class”;} Haris h(2,3); } }; Class haris : public waris{ Haris(int a , int b) : waris(b){ Cout <<“I am derive class constructor”; } }

13 Accessing Member function Of Parent Class
Important point is accessing member of parent class using derive class object. Class waris{ Void input(); void show(); void tell(); }; Class haris : public waris{ Void squre(); Int main(){ Haris obj; Obj.input(); obj.show(); obj.tell(); Obj.squre(); }

14 Function Overriding Method:
The process of declaring member function in derive with same name as in the parent name is called function overriding. The object of derive class cannot access the member function of parent class. Method: Scope Resolution operator(In the function of derive class). Base pointer. Non member function In the main function i.e (derive object . Parent class name :: function name);

15 Type of inheritance Public Inheritance. Private Inheritance.
Protected Inheritance.

16 Public Inheritance In Derive Class In Main Body Public Public
The derive class can access the member. The derive class can access the member. The derive class cannot access the member. The derive class object can access the member. The derive class object cannot access the member. Public Protected Private In Main Body Public Protected Private

17 Protected Inheritance
In Derive Class The derive class can access the member. The derive class can access the member. The derive class cannot access the member. The derive class object cannot access the member. The derive class object cannot access the member. Public Protected Private In Main Body Public Protected Private

18 Private Inheritance In Derive Class In Main Body Public Public
The derive class can access the member. The derive class can access the member. The derive class cannot access the member. The derive class object cannot access the member. The derive class object cannot access the member. Public Protected Private In Main Body Public Protected Private

19 Multilevel Inheritance
A type of inheritance in which a derive class is derive from an other class. Syntax: Class A{ body of class}; Class B : public A {body of class }; Class C : public B {body of class }; Use of function overriding method Void fun(){ A :: fun2; B :: fun3; }

20 Multi level inheritance with parameter
Class A{ Void in1(int a){} }; Class B : public A{ Void in(int a ,int b){ A : : in1(a); } Class c : public B{ Void in3(int a , int , b , int c){ B :: in(a , b);

21 Multiple Inheritance Class A Class B Class C
If a derive class has more than one parent class is called multiple inheritance. Class B Class A Class C

22 Constructor And Destructor
First Parent class constructor called. Second Derive class constructor is called. First derive class destructor called. Second parent class destructor called.

23 Ambiguity In Inheritance
Call the function in derive class using scope resolution operator . { A : : function name() } Call in main function using derive class on object. Object .parent class name :: function();

24 ContainerShip Example: Relate The classes.
To call the function of one class into other class using object of class. Example: Class Waris { void in() ; void show() ; void tell() ; }; class Haris { Waris object ; Void show(){ Object.in(); object.show(); object.tell(); } };


Download ppt "By Muhammad Waris Zargar"

Similar presentations


Ads by Google