C++ | Inheritance | what is inheritance? | Types of inheritance

1. What is inheritance?
Ans: Inheritance is the most powerful feature in Object oriented programming.  Inheritance is the process by which one object can acquire the properties of  another object.

2.Types of inheritance:
     a. Single inheritance
     b. Multilevel inheritance
     c. Multiple inheritance
     d. Hierarchical inheritance
     e. Hybrid inheritance

3. Mode of inheritance:

     1. Private mode: in private mode the protected and public members of the base class will be private in the derived class.
     2. Protected Mode: in protected mode the protected and public  members of the base class will be protected in the derived class.
     3. Public Mode: in public mode the protected member of base class  will be protected in the derived class and the public members of the base class will be public in the derived classs.

Derivation of class:
  •   Syntax->  class derivedclass: [visibility mode] baseclass
    {
         ----   ----- ----
         ----- ----- ------
     }

  • Here visibility mode can be private and public; and by default it is private
  • When a base class is privately inherited by a derived class, public member of base class become private member of derived class.
  •  When a base class is publically inherited, public members of base class become public member of the derived class.

Single inheritance:
    Here a single derived class inherit a single base class
.
Example :
#include
Class B
{
    Int a;         //private; not inheritable
    Public: 
    Int b;         //public; ready for inheritance
    Void get_ab();
    Int get_a(void);
    Void show_a();.
};
Class D: public B      //public derivation
{
    Int c;
    Public:
    Void mul(void);
    Void display(void);
}
Void B:: get_ab(void)
{
    A=5;
    B=10;
}
Int B:: get_a()
{
    Return a;
}
Void:: show_a()
{
    Cout<<”a=” << a <<’\n’;
}
Void:: mul()
{
    C=b* get_a();     //c=b*a  
}
Void D:: display()
{
    Cout<<”a”<< get_a() <<”\n”;
    Cout<< “b=” << b << “\n”;
    Cout<< “c=” << c << “\n”;
}
//the main function
Int main()
{
    D d;    //obj
    d.get_ab();
    d.mul();
    d.show_a();
    d.display();
    d.b=20;
    d.mul();
    d.display();
    return 0;
}

Output of the above program: 
      a=5
      a=5
      b=10
      c=50
      a=5
      b=20
      c=100


For more information please search our blog or contact us.

0 comments:

Post a Comment

 
Design by Wordpress Theme | Bloggerized by Free Blogger Templates | coupon codes