-->

Inheritance


INHERITANCE

1.             What is inheritance? How does it enable reusability?

v The mechanism of deriving a new class from an old one is called inheritance (or derivation).
v The old class is referred to as the ‘base class’ and new one is called the ‘derived class’ or ‘subclass’.
v C++ strongly supports the concepts of reusability, and it is achieved using ‘inheritance’.
v The derived class inherits some or all of the traits from the base class.
v A class can also inherit properties from more than one class or from more than one level.
v In inheritance derived class inherits all/some properties of base class and this enable code reusability. So we do not need to define the properties in derived class as in base class again.

2.            What are the different forms of Inheritance? Give an example for each? 
v There are five types of inheritance.
a) Single Inheritance: A derived class which has only one base class is called single inheritance.
ü Example:

class A                            // base class
{  
     // class body
};
class B : public A           // derived class
{
     // class body
};
b)   Multiple Inheritances: A derived class with more than one base class is known as multiple inheritances.
ü Example:

class A                            // base class1
{
     // class body
};
class B                            //base class2
{
     // class body
}; 
class C : public A, public B             //derived class1
{
              // class body
}; 
c) Hierarchical Inheritance: One class is derived by more than one class is known as hierarchical inheritance.
ü Example:
class A                            // base class
{
     // class body
};
class B : public A           // derived class1
{
// class body
};
class C : public A           // derived class2
{
     // class body
};

d)   Multilevel Inheritance: The mechanism of deriving a class form another ‘derived class’ is known as multilevel inheritance.
ü Example:
class A                   //super base class (grandparent)
{
     // class body
};
class B : public A           // middle level base class (parent)
{
     // class body
};
class C : public B            //derived class (child)
{
     // class body
};
e) Hybrid Inheritance: A combination of multiple and hierarchical inheritance is known as hybrid inheritance.
ü Example:
class A                   //super base class (grandparent)
{
     // class body
};
class B1 : public A         // middle level base class (parent1)
{
     // class body
};
class B2 : public A         // middle level base class (parent2)
{
     // class body
};
class C : public B1, public B2         //derived class (child)
{
     // class body
};
       
4.             In what order are the class constructors called when a derived class objet is created?
v If no base class constructor takes any arguments, the derived class needs not to have a constructor function.
v  When both the derived and base classes contain constructors, the base constructor is executed first and then the constructor in the derived class is executed.
v In a multilevel inheritance, the constructors will be executed in the order of inheritance.
v The constructor of the derived class receives the entire list of values as its arguments and passes them on to the base constructors in the order in which they are declared in the derived class.
v The base constructors are called and executed before executing the statements in the body of the derived constructor.
v The constructors for virtual base classes are invoked before any non-virtual base classes.
v If there are multiple virtual base classes, they are invoked in the order in which they are declared. 

6.             When do we use the protected visibility mode to a class member?
v C++ provides a third visibility modifier, which serves a limited purpose in inheritance.
v A member declared as protected is accessible by the member functions within its class and by the any class immediately derived from it.
v When a protected member is inherited in public mode, protected data becomes protected in the derived class too and therefore is accessible by the member functions of the derive class.
v A protected member, inherited in the private mode derivation, becomes private in the derived class. Although it is available for the member functions of the derived class, it is not available for further inheritance.