Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 What is the purpose of Inheritance? zSpecialisation Extending the functionality of an existing class zGeneralisation sharing commonality between two.

Similar presentations


Presentation on theme: "1 What is the purpose of Inheritance? zSpecialisation Extending the functionality of an existing class zGeneralisation sharing commonality between two."— Presentation transcript:

1 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 2 Inheritance types in C++ 1.Single level Inheritance 2.Multilevel Inheritance 3.Multiple Inheritance 4.Hierarchical Inheritance 5.Hybrid Inheritance

3 3 Single Inheritance zInheriting the properties of one Derived class from only one Base class is known as Single Inheritance. Base Class Derived Class

4 Single Inheritance 4 class A { // Base Class A }; class B: public A //Derived Class B { };

5 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.

6 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.

7 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

8 Multiple Inheritance class A { // Base Class A }; class B //Base Class B { }; class C: public A, B // C Derived Class from A and B { };

9 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.

10 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 { };

11 Hybrid Inheritance Applying two or more types of inheritance for designing the program is known Hybrid Inheritance.

12 CM505.4012 Single inheritance A single inheritance contains only one derived class and one base class

13 CM505.4013 Single inheritance The declaration of derived class is same as ordinary class A derived class contains of the following components

14 CM505.4014 Single inheritance The keyword class The name of the Derived class A single colon

15 CM505.4015 Single inheritance The type of derivation (private, public or protected) The name of the base or parent class The reminder of the class definition

16 CM505.4016 Single inheritance BASE CLASS DERIVED CLASS

17 CM505.4017 SYNTAX: class derived_classname:visibilitymode base_classname { …………………. members of derived class …………… ……………… }; Single inheritance

18 CM505.4018 Single inheritance : Syntax class A { ………………… members of base class A …………………. } class B: public A { ……………. members of derived class B …………… }

19 CM505.4019 Program for single inheritance class person { char name[10]; int age; char gender; public: void get data(); void display(); };

20 CM505.4020 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; }

21 CM505.4021 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; }

22 CM505.4022 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; }

23 CM505.4023 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 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 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


Download ppt "1 What is the purpose of Inheritance? zSpecialisation Extending the functionality of an existing class zGeneralisation sharing commonality between two."

Similar presentations


Ads by Google