Inheritance

Introduction : Using data members and member functions of one class in another class is known as inheritance. The class which is inherited is called base class and the class which inherits base class is known as derived class. Derived class has all the features of base class and also its own new features. Inheritance uses the feature of reusability.

Forms of Inheritance : There are five forms of inheritance. Those are as follows.

  1. Single Inheritance : When only one base class is inherited into another class i.e. derived class. This type of inheritance takes place at one level only.
    Syntax class  derived-class-name : visibility-mode  base-class-name
    {};
  2. Multiple Inheritance :In this type one derived class inherits two or more classes(base classes).
    Syntax
    class derived-class-name : visibility-mode  base_1, base_2,......
    {};
  3. Hierarical Inheritance : In this type, one class is inherited into different number of derived classes.
    Syntax
    class derived-class-name  : visibility  base-class
    {};
    class derived_2 : visibility  base class
    {};
  4. Multi-level Inheritance  : In this type, one class in inherited to another whch inturn is inherited to third class and so on. For example : class A is inherited to class B which inturn is inherited to class C. Therefore, the features of class A are inherited to class B and in class C there are the the features of both class A and class B.
  5. Hybrid Inheritance : It is the combination of both hierarical and multiple type of inheritance.
Visibility Modes : There are three types of visiblty modes. These are public, protected and private.


  • Public : In public derivation, the public data members of base class becomes the public data members of derived class and the protected members of base class becomes protected members of derived class i.e the access specifier for inherited members do not change in the derived class. The private data members are not inherited.
    Example
    class base
    (
    private :
    int x;
    void check();
    protected :
    int y;
    void display();
    public :
    int z;
    void getval();
    };

    class derived : public base
    {
    private :
    int a;
    void enter();
    protected :
    int b;
    void read();
    public :
    int c;
    void write();
    };
  • Protected : In protected derivation, the public and protected data members of the base class becomes the protected members of the inherited class and the private data members of base class are not inherited.
    Example
    class base
    {
    // statements
    };

    class derived : protected base
    {
    // statements
    };
  • Private : In private derivation, the public and protected members of base class becomes private members of the derived class and private members of base class are not inherited.
    Example
    class base
    {
    // statements
    };

    class derived
    {
    // statements
    };

Problem with Multiple Inheritance
The problem faced with multiple type of inheritance is when function with same name is present in both parent/base class as well as derived class.
Solution This problem can be solved by writing class name while calling.
Syntax object name . class name :: function name();
Example
class M
{
protected :
int m;
public :
void get_m(int);
};
 
class N
{
protected :
int n;
public :
void get_n(int);
};
 
class P : public M, public N
{
public :
void display (void);
};
 
void main () 
{
P.obj p;
obj p.get_m(10);
obj p.get_n(20);
obj p.display();
M.obj m;
obj m.get_m(10);
N.obj n;
obj n.get_n(20);
}


Virtual base class

In Hybrid type of inheritance, suppose, class A is inherited in class B and class C (hierarical) which in turn is inherited in class D (multiple). Then by this, class D will inherit the features of class A twice, once through class B and secondly through class C.
To avoid this duplication, class A is made virtual base class. This will ensure that only one copy is passed to derived class, no matter how many times it is inherited.

No comments:

Post a Comment