C++: Inheritance

div { width: 100%; } .success { background-color: #ccffcc; } Reusability is an  important feature of OOP. It saves our time and increase reliability. C++ strongly supports the concept of reusability.
Types of Inheritance:
1) Single Inheritance
2) Multiple Inheritance
3) Hierarchical  Inheritance
4) Multilevel Inheritance
5) Hybrid Inheritance
__________________________________________
Defining Derived Class:
  class derived-class-name: visibility-mode base-class-name
  {
       ..........//members of derived class
   };
The colon indicates that the derived-class-name is derived from the base-class-name.
The default visibility mode is private. Visibility mode specifies whether the features of the base class are privately derived or publicly derived.
When a base class is privately inherited by a derived class, 'public members' of the base class become 'private member' of the derived class and therefore the public members of the base class can only be accessed by the member functions of the derived class.
Thus no member of the base class is accessible to the objects of the derived class.
The private member of the base class will never become the member of its derived class.
We can also add our own data and member functions and thus extend the functionality of the base class.
_________________________________________
Making A Private Member Inheritable:
C++ provides a third visibility modofier protected.
A member declared as protected is accessible by the member functions within its class and any class immediately derived from it. It cannot be accessed by the functions outside these two classes.
   class demo{
     public:
        ..........
     protected:
        ..........
     private:
        .........
     };
When a protected member is inherited in public mode, it become protected in the derived class too.
A protected member, inherited in the private mode, becomes private in the derived class. Although it is available to the member functions of the derived class, it is not available for the further inheritance.
In protected derivation, both the public and protected members of the base class become protected member of the derived class.
__________________________________________
Multiple Inheritance:
   class D: visibility B-1, visibility B-2...
   {
    .....(Body of D)
   };
Ambiguity Resolution in Multiple Inheritance
  class M
  {
    public:
       void display(){
                    cout<<" Class M"<<endl;
                    }
  );
  class N
  {
    public:
       void display(){
                    cout<<" Class N"<<endl;
                    }
   };
   class P: public M, public N
   {
     public :
            void display(){ //overrides display() of M and N
                   M::display();
                 }
   };
   int main(){
            P p;
            p.display();
        }
Ambiguity Resolution in Single Inheritance
 
  class A
   {
     public :
             void display(){
                  cout<<"  A"<<endl;
               }
   };
   class B: public A
    {
      public:
             void display(){
                    cout<<"B"<<endl;
                  }
    };
   int main(){
       B b; //derived class object
       b.display(); //invokes display() in B
       b.A::display(); //invokes display() in A
       b.B::display(); //invokes display() in B
      
       return 0;
     }
__________________________________________
Virtual Base Classes:
When a derived class has two or more than two direct base classes which themselves have a common base class. Thus the derived class would have duplicate sets of members.
The duplication of inherited members due to these multiple paths can be avoided by making the common base class(ancestor class) as virtual base class.
   class A{ //grandparent
    .........
     };
   class B1:virtual public A{ //parent1
    .........
    };
   class B2:public virtual A{ //parent2
    .........
    };
   class C:public B1,public B2{ //child
    ......... //only one copoy of A will be inherited
   };
__________________________________________
Abstract Classes:
An abstract class is one that is not used to create objects. An abstract class is designed only to act as a base class.
__________________________________________
Constructors in Derived Classes:
As long as no base class constructors takes any arguments ,the derived class need not to have a constructor function.
If any base class constructor contains a constructor with one or more arguments,then it is compulsory for the derived class to have a constructor and pass the arguments to the base class constructor.
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.
In case of multiple inheritance, the base classes are constructed in the order in which they appear in the declaration of the derived class.
Similarly, in a multilevel inheritance, the constructor will be executed in the order of inheritance.
C++ supports another method of initializing the class objects. This method uses what is known as initialization  list in the constructor function.
  
   constructor(arglist):intialization-section
    {
         assignment-section
    }
The assignment-section of the constructor function is used to assign initial values to its data members.
Initialization section is used to provide initial values to the base constructors and also to initialize its own class members.
The initialization section basically contains a list of initialization separated by commas.
   class demo{
       int a;
       int b;
   public:
       demo(int i,int j):a(i),b(2*j){ }
   };
  main(){
      demo x(2,3);// a will be initialize to 2 and b to 6
  }
demo(int i,int j):b(i),a(i+j){ } //a will be initialized to 5 and b to 2
The data members are initialized in the order of declaration, independent of the order in the initialization list.
demo(int i,int j):a(i),b(a*j){ } //a is initialized to 2 and b to 6
demo(int i,int j):b(i),a(b*j) //this will nort work since value of b is not available to a which is to be initialized first.
__________________________________________
         < Happy Coding>
Reference:
OOP by BALAGURUSWAMY
If you want to practice more problem you can visit websites like
Geeksforgeeks
Read articals from it
spoj
In Association with Amazon.in Start practicing from basic problems

Popular Posts

Top 10 Web Sites Every Programmer Should Visit Daily

C++: Huffman Coding using STL

What is "namespace" and why do we use it?