Inheritance Inheritance The process of deriving new class



























- Slides: 27
Inheritance
Inheritance 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 2
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 3
Inheritance The derivations are three types 1 public 2 private 3 protected 4
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 A { private: int a; protected: int b; public: int c; }; Class B: public A { private: int x; }; The member accessible in derive class B are b protected c public x private 5
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 A { private: int a; protected: int b; public: int c; }; class B: private A { private: int x; }; The member accessible in derive class B are b private c private x private 6
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 A { private: int a; protected: int b; public: int c; }; class B: protected A { private: int x; }; The member accessible in derive class B are b protected c protected x private 7
Inheritance Visibility of inherited members Derived class Base class Private Protected Public Private Protected Derivation Not Inherited Protected Private Protected Public Private Protected 8
Inheritance Class A Private Protected Public 9
Inheritance Cannot inherit Class A Private Protected Public Cannot inherit Class B : public A Private Protected Public 10
Inheritance Cannot inherit Class A Private Protected Public Cannot inherit Class B : public A Private Protected Public 11
Inheritance Cannot inherit Class C : private A Private Protected Public Class A Private Protected Public Cannot inherit Class B : public A Private Protected Public 12
Inheritance Cannot inherit Class C : private A Private Protected Public Class A Private Protected Public Cannot inherit Class B : public A Private Protected Public 13
Inheritance Cannot inherit Class A Private Protected Public Class C : private A Private Protected Public Cannot inherit Class B : public A Private Protected Public Class D : protected C Private Protected Public 14
Inheritance Cannot inherit Class A Private Protected Public Class C : private A Private Protected Public Cannot inherit Class B : public A Private Protected Public Class D : protected C Private Protected Public 15
Inheritance Cannot inherit Class A Private Protected Public Class C : private A Private Protected Public Class D : protected C Private Protected Public Cannot inherit Class B : public A Private Protected Public Class E: private B, public C Private Protected Public 16
Inheritance Cannot inherit Class A Private Protected Public Class C : private A Private Protected Public Class D : protected C Private Protected Public Cannot inherit Class B : public A Private Protected Public Class E: private B, public C Private Protected Public 17
Inheritance Types of Inheritance There are five different inheritances supported in C++: (1) Simple / Single (2) Multiple (3) Hierarchical (4) Multilevel (5) Hybrid 18
Inheritance Single Inheritance A B 19
Inheritance Multiple Inheritance A B C 20
Inheritance Hierarchical Inheritance A B C 21
Inheritance Multi level Inheritance A B C 22
Inheritance Hybrid Inheritance Combination of Hierarchical & Multiple A B C D 23
Inheritance 24
Inheritance 25
Inheritance 26
Inheritance 27