Presentation is loading. Please wait.

Presentation is loading. Please wait.

Inheritance. 2 The process of deriving new class from the existing one is called inheritance Then the old class is called base class and the new class.

Similar presentations


Presentation on theme: "Inheritance. 2 The process of deriving new class from the existing one is called inheritance Then the old class is called base class and the new class."— Presentation transcript:

1 Inheritance

2 2 The process of deriving new class from the existing one is called inheritance Then the old class is called base class and the new class is called derived class or sub class The derived class inherits some or all properties of base class Advantages Reusability of the code to increase the reliability of the code to add some enhancements to the base class

3 3 Inheritance Defining derived class Syn: class derived class : visibility mode base class name { }; :  indicates the derived class is derived from the base class name Visibility mode  optional by default private visibility mode specifies the features of the base class are privately derived or publicly derived or protected derived

4 4 Inheritance The derivations are three types 1public 2private 3protected

5 5 Inheritance Public inheritance If the inheritance is public then The private members are not inherited The public members of base class becomes public in derive class The protected members of base class becomes protected in derived class class A { private: int a; protected: int b; public: int c; }; Class B: public A { private: int x; };

6 6 Inheritance Private inheritance If the inheritance is private then The private members are not inherited The public members of base class becomes private in derive class The protected members of base class becomes private in derived class class A { private: int a; protected: int b; public: int c; }; class B: private A { private: int x; };

7 7 Inheritance Protected inheritance If the inheritance is protected then The private members are not inherited The public members of base class becomes protected in derive class The protected members of base class becomes protected in derived class class A { private: int a; protected: int b; public: int c; }; class B: protected A { private: int x; };

8 8 Inheritance Visibility of inherited members Base class Derived class PublicPrivateProtected Derivation PrivateNot Inherited Protected PrivateProtected Public PrivateProtected

9 9 Inheritance Class A Private Protected Public

10 10 Inheritance Class A Private Protected Public Class B : public A Private Protected Public Cannot inherit

11 11 Inheritance Class A Private Protected Public Class B : public A Private Protected Public Cannot inherit

12 12 Inheritance Class A Private Protected Public Class B : public A Private Protected Public Class C : private A Private Protected Public 12 Cannot inherit

13 13 Inheritance Class A Private Protected Public Class B : public A Private Protected Public Class C : private A Private Protected Public 13 Cannot inherit

14 14 Inheritance Class A Private Protected Public Class B : public A Private Protected Public Class C : private A Private Protected Public Class D : protected C Private Protected Public Cannot inherit

15 15 Inheritance Class A Private Protected Public Class B : public A Private Protected Public Class C : private A Private Protected Public Class D : protected C Private Protected Public Cannot inherit

16 16 Inheritance Class A Private Protected Public Class B : public A Private Protected Public Class C : private A Private Protected Public Class D : protected C Private Protected Public Class E: private B,public C Private Protected Public Cannot inherit

17 17 Inheritance Class A Private Protected Public Class B : public A Private Protected Public Class C : private A Private Protected Public Class D : protected C Private Protected Public Class E: private B,public C Private Protected Public Cannot inherit

18 18 Inheritance Types of Inheritance There are five different inheritances supported in C++: (1) Simple / Single (2) Multiple (3) Hierarchical (4) Multilevel (5) Hybrid

19 19 Inheritance Single Inheritance

20 20 Inheritance Multiple Inheritance

21 21 Inheritance Hierarchical Inheritance

22 22 Inheritance Multi level Inheritance

23 23 Inheritance Hybrid Inheritance Combination of Hierarchical & Multiple

24 24 Inheritance

25 25 Inheritance

26 26 Inheritance

27 27 Inheritance


Download ppt "Inheritance. 2 The process of deriving new class from the existing one is called inheritance Then the old class is called base class and the new class."

Similar presentations


Ads by Google