Inheritance in C++




Inheritance in C++

Inheritance is the property by virtue of which one class passes over its properties to another class without necessitating modification in the main class.  The class that inherits its properties to other class is called the base class whereas the class that receives the properties of the base class is called derived class.  Inheritance supports extensibility of classes, since new features may be added to existing classes without modifying them, by inheriting them to a new derived class, where derived class is defined to contain new additional features, other than the base class.  Inheritance types may be-

  • Single Inheritance
  • Multilevel Inheritance
  • Multiple Inheritance
  • Hierarchical Inheritance
  • Hybrid Inheritance

1.  Single Inheritance:  In single inheritance, a base class inherits its features to a derived class.

In the above figure the base class inherits its features to the derived class.  C++ code for single inheritance is as follows:

class Base
{int x;
public:
void getx();
void putx();
};

class Derived : public Base
{int y;
public:
void gety();
void puty();
};

void main()
{Derived dobj;
dobj.getx();
dobj.gety();
dobj.putx();
dobj.puty();
}

Inheritance type may be public, private or protected.  Private members of a class are never inherited.  When inheritance type is public, public members of the base class are inherited as public and protected members are inherited as protected.  When inheritance type is private, public members as well as protected members are inherited as private.  When inheritance type is protected, public members as well as protected members are inherited as protected.

2.  Multilevel Inheritance:  If a base class inherits its features to a derived class and the derived class further inherits its own properties and those inherited from base to another derived, the type of inheritance is called multilevel inheritance.

For example: 


class grandfather
{ int x;
public:
void getx();
void putx();
};

class father : public grandfather
{int y;
public:
void gety();
void puty();
};

class son : public father
{int z;
public:
void getz();
void putz();
};

void main()
{son s;
s.getx();
s.gety();
s.getz();
s.putx();
s.puty();
s.putz();
}

3. Multiple Inheritance:  If a derived class attains properties from more than one base class, then the type of inheritance is called multiple inheritance.

For example:


class base1

{ int x;
public:
void getx();
void putx();
};

class base2
{int y;
public:
void gety();
void puty();
};

class derived : public base1, public base2
{int z;
public:
void getz();
void putz();
};

void main()
{derived s;
s.getx();
s.gety();
s.getz();
s.putx();
s.puty();
s.putz();
}

4. Hierarchical Inheritance:  If inheritance forms a hierarchy, such that a class may inherit its properties at many levels as defined in the hierarchy, the type of inheritance is called hierarchical inheritance.

For example:


 

5. Hybrid Inheritance:  Two or more inheritance types combine together to result in hybrid inheritance.  Some situations may arise when two or more types are required together to define solution to a given problem, such a situation results in hybrid inheritance.  An example of hybrid inheritance is demonstrated in the following figure:

The above figure is a combined representation of multiple inheritance and multilevel inheritance.  For the base classes base1 and base2 inheriting to derived1, the inheritance type is multiple inheritance, but for the remaining part is multilevel inheritance.  This is called hybrid inheritance.