Inheritance
Inheritance
One of the most important concepts in object-oriented
programming is that of inheritance. Inheritance allows us
to define a class in terms of another class, which makes it
easier to create and maintain an application. This also
provides an opportunity to reuse the code functionality
and fast implementation time.
When creating a class, instead of writing completely new
data members and member functions, the programmer
can designate that the new class should inherit the
members of an existing class. This existing class is called
the base class, and the new class is referred to as
the derived class.
                   Inheritance
Inheritance is the process by which new classes called
derived classes are created from existing classes called
base classes.
The derived classes have all the features of the base
class and the programmer can choose to add new
features specific to the newly created derived class.
                     Inheritance
General Format for implementing the concept of
Inheritance:
       class derived_classname: access specifier
                    baseclassname
For example, if the base class is MyClass and the derived
class is sample it is specified as:
              class sample: public MyClass
                   Inheritance
     public, private and protected access specifiers:
1 If a member or variables defined in a class is private, then
they are accessible by members of the same class only and
cannot be accessed from outside the class.
2 Public members and variables are accessible from outside
the class.
3 Protected access specifier is a stage between private and
public. If a member functions or variables defined in a class
are protected, then they cannot be accessed from outside
the class but can be accessed from the derived class.
           Types of Inheritance
1. Single class Inheritance:
Single inheritance is the one where you have a
single base class and a single derived class.
       Class Employee   It is a Base class (super)
        Class Manager   it is a sub class (derived)
              Types of Inheritance
Syntax Single Class Inheritance
class BaseClass {
// Body of the class
};
class DerivedClass : access_modifier BaseClass
{
// Body of the class
};
            Types of Inheritance
2. Multilevel Inheritance:
In Multi level inheritance, a class inherits its
properties from another derived class.
           Class A      it is a Base class (super) of B
           Class B      it is a sub class (derived) of A
                         and base class of class C
           Class C      derived class(sub) of class B
              Types of Inheritance
Syntax Multilevel Inheritance
class B1 {
// Base class 1 members
};
class B2 : access_modifer B1 {
// Base class 2 members
};
class DerivedClass : access_modifier B2 {
// Derived class members
};
             Types of Inheritance
3. Multiple Inheritances:
In Multiple inheritances, a derived class inherits
from multiple base classes. It has properties of
both the base classes.
          Class A          Class B           Base class
                 Class C     Derived class
              Types of Inheritance
Syntax Multiple Inheritance
class B1 {
// Base class members
};
class B2 {
// Base class members
};
class DerivedClass : access_modifier B1, access_modifier B2 {
// Derived class members
};
              Types of Inheritance
4. Hierarchical Inheritance:
In hierarchical Inheritance, it's like an inverted tree.
So multiple classes inherit from a single base
class.
                     Class A
    Class B          Class D              Class C
             Types of Inheritance
Syntax Hierarchical Inheritance
 class B {
 // body of the class
 }
 class D1 : access_modifier B {
 // body of the class
 }
 class D2 : access_modifier B {
 // body of the class
 }
 class D3 : access_modifier B {
 // body of the class
 }
                   Types of Inheritance
5. Hybrid Inheritance:
Hybrid inheritance is a combination of more than one inheritance type
within the same program. This is also called virtual inheritance. For
example, a complex hierarchy involving multiple types of inheritance.
Since it is a combination of more than one inheritance, there is no fixed
syntax for it. So, let's take a look at an example to get a better
understanding of this concept.
            Types of Inheritance
5. Hybrid Inheritance:
                 Class A
  Class B                     Class C
                  Class D