Syllabus Inheritance- Defining derived classes-Visibility modes- Single , Multilevel, Multiple, Hierarchical and Hybrid inheritance Virtual base classes Abstract classes Constructors in derived classes- Nesting of classes.
What? Inheritance is the capability of one class to acquire properties and characteristics from another class. The class whose properties are inherited by other class is called the Parent or Base or Super class. And , the class which inherits properties of other class is called Child or Derived or Sub class.
Example
Adv : Reusability of code
Syntax c lass derivedclassname : visibility label baseclassname { }; Animal dog Eg : class dog : public Animal { }; Keyword p rivate/public/protected
Types C++ offers five types of Inheritance. They are: Single Inheritance Multiple Inheritance Hierarchical Inheritance Multilevel Inheritance Hybrid Inheritance (also known as Virtual Inheritance)
Single Inheritance
Multiple Inheritance
Multilevel Inheritance
Heirarchical Inheritance
Hybrid Inheritance
Visibility labels/modes – private/public Base class - demo private – a,b public- read() Derived class – demochild private – c,d public –display() Private Inheritance All public members of base class b ecome private in derived Public Inheritance All public members of base class b ecome public in derived Note:- Private members of base class not inherited – Data Hiding After Inheritance - Derived Class private – c,d,read () public –display() After Inheritance - Derived Class private – c,d public –read(), display()
Try to do- Example 1 c lass B p rivate a Public b g et_ab () g et_a () s how_a () class D p rivate c public m ul () d isplay()
Example 2 c lass B p rivate a Public b g et_ab () g et_a () s how_a () class D p rivate C public m ul () d isplay()
Making Private Member Inheritable If private data of base class need to be inherited Modifying private as public Problem:???? Data hiding violated So 3 rd visibility label – protected Limited purpose Can be accessed by base class and immediate derived class
Visibility labels/modes – private/public/protected Base class - demo private – a,b protected- c public- read() Derived class – demochild private – d,e protected- f public –display() Private Inheritance All public & protected members of base class b ecome private in derived Protected Inheritance All protected &public members of base class b ecome protected in derived Note:- Private members of base class not inherited – Data Hiding After Inheritance - Derived Class private – d,e,c,read () Protected - f public –display() After Inheritance - Derived Class private – d,e p rotected – f, c,read () public –display()
Base class - demo private – a,b protected- c public- read() Derived class – demochild private – d,e protected- f public –display() Public Inheritance All protected members of base class b ecome protected and public members become p ublic in derived After Inheritance - Derived Class private – d,e p rotected – f, c public –read(), display() Note:- Private members of base class not inherited – Data Hiding
Visibility Labels
Inheritance Visibility Mode
Single class A { }; c lass B:public A { }; Multiple Class A { }; Class B { }; Class c:public A,public B { }; Multilevel c lass A { }; c lass B:public A { }; c lass C:public B { }; Hierarchical Class A { }; Class B :public A { }; Class C:public A { }; Class D: public A { }; Hybrid Class A { }; Class B:public A { }; Class c:public A { }; Class D: public B ,public C { {;
Ambiguity Resolution in Inheritance Output Class
Virtual Base Class Hybrid Inheritance, Here all protected and public members of Grandparent class is inherited twice via Parent 1 and Parent 2 c lass to Child Class Ambiguity arises Can be avoided by making common base class as virtual base class
Example
Abstract Class Class that is not used to create objects Created to act as base class
Program Explanation Inheritance Programs
Constructors in Derived Class If any base class constructor take argmnt then there should be derived constructor Execution order Multiple Inheritance – order in declaration of derived class Multilevel Inheritance- order of Inheritance
General Form Derivedclassconstructor (arg1,arg2,… argn ): Base 1(arg1), Base 2(arg2) ……………. ……………. Base n( argn ) { } Eg : D( int a,int b, int c, int d): A ( inta , int b), B( int c) { d1=d; }