By Muhammad Waris Zargar inheritance By Muhammad Waris Zargar
Inheritance Introduction Advantages Categories of inheritance Specifying A derive class syntax: class derive class name: access specifier parent class;
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
Multi Level Inheritance with parameter Multiple Inheritance Constructor In Multiple Inheritance Constructor without parameter Constructor with parameter Ambiguity In Multiple Inheritance Removing Ambiguity Containership
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
Advantages Reusability. Save time and effort. Increase program structure and reliability.
Categories Parent Parent1 Parent2 Child Child Single inheritance Multiple Inheritance Parent Parent1 Parent2 Child Child
Access Specifier Access Specifier Main Class Derive Class Main Function Private Yes No Protected Public
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; }
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”;
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”; } }
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(); }
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);
Type of inheritance Public Inheritance. Private Inheritance. Protected Inheritance.
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
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
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
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; }
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);
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
Constructor And Destructor First Parent class constructor called. Second Derive class constructor is called. First derive class destructor called. Second parent class destructor called.
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();
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(); } };