INHERITANCE By Muhammad Waris Zargar Inheritance Introduction Advantages

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 ◦ 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 ◦ Single inheritance Parent Child Multiple Inheritance Parent 1 Parent 2 Child

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

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: Waris(){cout <<“I am in parent class”; } Int main(){ 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(){ Haris h(2, 3); } Waris(int a){cout <<“I am in parent class”; } }; 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. squre(); ◦ } obj. show(); obj. tell();

Function Overriding ◦ 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 The derive class can access the Publicmember. The derive class can access the Protected member. The derive class cannot access the Private member. In Main Body The derive class object can access the Publicmember. The derive class object cannot access the member. Protected The derive class object cannot access the Privatemember.

Protected Inheritance In Derive Class The derive class can access the Publicmember. The derive class can access the Protected member. The derive class cannot access the Private member. In Main Body The derive class object cannot access the Publicmember. The derive class object cannot access the member. Protected The derive class object cannot access the Privatemember.

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

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 : : fun 2; B : : fun 3; }

Multi level inheritance with parameter ◦ Class A{ Void in 1(int a){} }; Class B : public A{ Void in(int a , int b){ A : : in 1(a); } }; Class c : public B{ Void in 3(int a , int , b , int c){ B : : in(a , b); } };

Multiple Inheritance ◦ If a derive class has more than one parent class is called multiple inheritance. Class A Class B 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();

Container. Ship 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(); };
- Slides: 24