1 What is the purpose of Inheritance? zSpecialisation Extending the functionality of an existing class zGeneralisation sharing commonality between two or more classes zImproved efficiency and greater robustness zPlays a major part in polymorphism
2 Inheritance types in C++ 1.Single level Inheritance 2.Multilevel Inheritance 3.Multiple Inheritance 4.Hierarchical Inheritance 5.Hybrid Inheritance
3 Single Inheritance zInheriting the properties of one Derived class from only one Base class is known as Single Inheritance. Base Class Derived Class
Single Inheritance 4 class A { // Base Class A }; class B: public A //Derived Class B { };
Multilevel Inheritance 5 zWhen Single level inheritance is extended to more levels then it is called multilevel inheritance. zIn this inheritance one class is derived from another derived class and the level of derivation can be extended to any number of levels.
Multilevel Inheritance 6 Base Class Intermediate Base Class Derived Class class A { // Base Class A }; class B: public A //Intermediate Base Class B { }; class C: public B // C Derived Class from B { }; The class B is known as intermediate base class since it provides a link for the inheritance between A and C. The chain ABC is known as inheritance path.
Multiple Inheritance Inheriting the properties of the Derived class from more than one Base Class is known as Multiple Inheritance. Derived Class Base Clas 1 Base Class 2 Base Class n
Multiple Inheritance class A { // Base Class A }; class B //Base Class B { }; class C: public A, B // C Derived Class from A and B { };
Hierarchical Inheritance In Hierarchical Inheritance, many sub classes inherit the properties from a single base class. The base class will include all the features that are common to the subclasses. A subclass can be constructed by inheriting the all or some properties of the base class. A subclass can serve as a base class for the lower level classes and so on.
Hierarchical Inheritance Base Class Sub Class Sub class class A { // Base Class A }; class B: public A // Derived Class B { }; class C: public A // C Derived Class { };
Hybrid Inheritance Applying two or more types of inheritance for designing the program is known Hybrid Inheritance.
CM Single inheritance A single inheritance contains only one derived class and one base class
CM Single inheritance The declaration of derived class is same as ordinary class A derived class contains of the following components
CM Single inheritance The keyword class The name of the Derived class A single colon
CM Single inheritance The type of derivation (private, public or protected) The name of the base or parent class The reminder of the class definition
CM Single inheritance BASE CLASS DERIVED CLASS
CM SYNTAX: class derived_classname:visibilitymode base_classname { …………………. members of derived class …………… ……………… }; Single inheritance
CM Single inheritance : Syntax class A { ………………… members of base class A …………………. } class B: public A { ……………. members of derived class B …………… }
CM Program for single inheritance class person { char name[10]; int age; char gender; public: void get data(); void display(); };
CM Program for single inheritance void person ::get data() { cout<<“enter the name”; cin>>name; cout<<“enter the age”; cin>>age; cout<<“enter the gender”; cin>>gender; }
CM Program for single inheritance void person :: display() { cout<<“the name of the student is “<<name; cout<<“the age of the student is “<<age; cout<<“the gender of the student is “<<gender; }
CM Program for single inheritance class student : public person { int roll, no; char branch[10]; public: void get data() { person :: getdata(); cout<<enter the roll no”; cin>>roll,no; cout<<“enter branch”; cin>>branch; }
CM Program for single inheritance void display() { person :: display(); cout<<“the roll no is”<<roll, no; cout<<“the branch is “<<branch; } }; void main() { student s; s.getdata(); s.display(); }
24 Summary: Inheritance zInheriting from a class doesn’t affect the integrity of that class - objects of the original base class can still be created zGeneralisation allows removal of redundancy and duplication among classes zSome base classes are abstract - they are not specific enough to be instantiated but act as holders for common attributes and methods of derived classes
25 Summary: Inheritance zIn C++ the protected keyword allows methods of a derived class access to its inherited attributes zBase class constructor methods are automatically called by the constructors of derived classes but argument lists must be compatible zDestructors are called in the reverse order of constructors