Reusability is one of the essential characteristics of Object Oriented Programming (OOP). Instead of writing programs repeatedly, using existing code is a good practice for the programmer to reduce development time and avoid mistakes. In C++, reusability is possible by using Inheritance.



What is Inheritance?

Classes can be reused in many ways in C++. The technique of deriving a new class from an old class is called inheritance. The old class is referred to as the base class, and the new class is referred to as the derived class or subclass. Inheritance allows programmers to define a class in terms of another class, making it more accessible to building and maintaining applications. When writing a new class, instead of rewriting new data members and member functions, programmers can make a bonding of the new class with the old class so that the new class inherits the members of the existing class. A class can get derived from one or more classes, which means it can inherit data and functions from multiple base classes.

Here is the syntax of inheritance in C++:

class derived-class: visibility-mode base-class

Visibility mode is used in the Inheritance of C++ to show or relate how base classes are viewed concerning the derived class. When one class gets inherited from another, visibility mode inherits all the public and protected members of the base class. Private members never get inherited and hence do not take part in visibility. By default, visibility mode remains "private".

What Are Base Class and Derived Class?

The existing class from which the derived class gets inherited is known as the base class. It acts as a parent for its child's class and all its properties, i.e., public and protected members, get inherited by its derived class. A derived class can be defined by specifying its relationship with the base class in addition to its own details, i.e., members.

The general form of defining a derived class is:

class derived-class_name : visivility-mode base-class_name
{
. . . .  // members of the derived class
. . . .
};

Forms of Inheritance

C++ provides five types of inheritance. they are:
  • Single Inheritance
  • Multiple Inheritance
  • Hierarchical Inheritance
  • Multilevel Inheritance
  • Hybrid Inheritance (also known as Virtual Inheritance)

inheritanc-cpp

Single Inheritance

In single Inheritance, there is only one base class and one derived class. The Derived class gets inherited from its base class. This is the simplest form of Inheritance.

Multiple Inheritance

In Multiple Inheritance, a single derived class may inherit from two or more base classes.

Program for Multiple Inheritance

Example:

#include <iostream>
using namespace std;

class stud {
protected:
    int roll, m1, m2;

public:
    void get()
    {
        cout << "Enter the Roll No.: "; cin >> roll;
        cout << "Enter the two highest marks: "; cin >> m1 >> m2;
    }
};
class extracurriculam {
protected:
    int xm;

public:
    void getsm()
    {
        cout << "\nEnter the mark for Extra Curriculam Activities: "; cin >> xm;
    }
};
class output : public stud, public extracurriculam {
    int tot, avg;

public:
    void display()
    {
        tot = (m1 + m2 + xm);
        avg = tot / 3;
        cout << "\n\n\tRoll No    : " << roll << "\n\tTotal      : " << tot;
        cout << "\n\tAverage    : " << avg;
    }
};
int main()
{
    output O;
    O.get();
    O.getsm();
    O.display();
}

Output:

multiple inheritance

Hierarchical Inheritance

In Hierarchical Inheritance, multiple derived classes get inherited from a single base class.

Syntax:

class base_classname {
    properties;
    methods;
};
class derived_class1 : visibility_mode base_classname {
    properties;
    methods;
};
class derived_class2 : visibility_mode base_classname {
    properties;
    methods;
};
... ... ...
... ... ...
class derived_classN : visibility_mode base_classname {
    properties;
    methods;
};

Program for Hierarchical Inheritance

Example:

#include <iostream>
#include <string.h>
using namespace std;

class member {
    char gender[10];
    int age;

public:
    void get()
    {
        cout << "Age: "; cin >> age;
        cout << "Gender: "; cin >> gender;
    }
    void disp()
    {
        cout << "Age: " << age << endl;
        cout << "Gender: " << gender << endl;
    }
};
class stud : public member {
    char level[20];

public:
    void getdata()
    {
        member::get();
        cout << "Class: "; cin >> level;
    }
    void disp2()
    {
        member::disp();
        cout << "Level: " << level << endl;
    }
};
class staff : public member {
    float salary;

public:
    void getdata()
    {
        member::get();
        cout << "Salary: Rs."; cin >> salary;
    }
    void disp3()
    {
        member::disp();
        cout << "Salary: Rs." << salary << endl;
    }
};
int main()
{
    member M;
    staff S;
    stud s;
    cout << "Student" << endl;
    cout << "Enter data" << endl;
    s.getdata();
    cout << endl
    << "Displaying data" << endl;
    s.disp();
    cout << endl
    << "Staff Data" << endl;
    cout << "Enter data" << endl;
    S.getdata();
    cout << endl
    << "Displaying data" << endl;
    S.disp();
}

Output:

Hierarchical Inheritance

Multilevel Inheritance

The classes can also be derived from the classes that are already derived. This type of Inheritance is called multilevel Inheritance.

Program for Multilevel Inheritance

Example:

#include <iostream>
using namespace std;

class base {
public:
    void display1()
    {
        cout << "\nBase class content.";
    }
};
class derived : public base {
public:
    void display2()
    {
        cout << "1st derived class content.";
    }
};

class derived2 : public derived {
    void display3()
    {
        cout << "\n2nd Derived class content.";
    }
};

int main()
{
    derived2 D;
    //D.display3();
    D.display2();
    D.display1();
}

Output:

Multilevel Inheritance

Hybrid Inheritance

Hybrid inheritance is a mixture of two or more inheritances; in this inheritance, code can have two or three types of inheritance.



Found This Page Useful? Share It!
Get the Latest Tutorials and Updates
Join us on Telegram