Inheritance in C++ || All type of Inheritance || OOPs Concept || C++

We all do have some similarities with our parents, which we inherit from them, this is known as Inheritance in real life.

What is Inheritance in C++ ?

The ability of a class to derive properties and characteristics from another class is called Inheritance. It is a very important aspect of OOP's (Object Oriented Programming). In this give access of all members of parent class to a child class, with this the Child class has the access to all data members and members functions of Parent class. 

Below are some terms which are most commonly used in inheritance:- 
Base Class (Parent Class) - It is the class which is being inherited
Derived Class (Child Class) - It is the class which is inheriting the base class

Types of Inheritance in C++:

1. Single Inheritance


In this type of Inheritance, one derived class is inherited from a single base class. This allows to inherit from only one base class.

Syntax:
// Single Inheritance
class class_name : access_specifier base_class_name
{
    // class body
};

// Example:
class Derived: public Base
{
    // class body
};

Example:

#include <iostream>
using namespace std;

class GetFirstName
{
    public:
    string FirstName;
    void getFN()
    {
        cout << "Enter the First Name: ";
        cin >> FirstName;
    }
};

class Name : public GetFirstName
{
    public:
    string LastName, FullName;
    void getLN()
    {
        cout << "Enter the Last Name: ";
        cin >> LastName;
    }
    void Print()
    {
        FullName = FirstName + " " + LastName;
        cout << "Full name: " << FullName;
    }
};

int main()
{
    Name obj1;
    obj1.getFN();
    obj1.getLN();
    obj1.Print();
    return 0;
}
Output for example of Single Inheritance:


2. Multiple Inheritance


In this type of Inheritance, one derived class is inherited from two or more base class. This allows to inherit from two or more base class.

Syntax:
// Multiple Inheritance
class class_name : access_specifier base_class_name1, access_speccifier base_class_name2
{
    // class body
};

// Example:
class Derived: public Base1, public Base2
{
    // class body
};
  

Example:

#include <iostream>
using namespace std;

class first_name
{
    public:
    string first;
    void getfn()
    {
        cout << "Enter First name: ";
        cin >> first;
    }
};

class middle_name
{
    public:
    string middle;
    void getmn()
    {
        cout << "Enter Middle name: ";
        cin >> middle;
    }
};

class last_name
{
    public:
    string last;
    void getln()
    {
        cout << "Enter Last name: ";
        cin >> last;
    }
};

class full_name : public first_name, public middle_name, public last_name
{
    public:
    string full;
    void print()
    {
        full = first + " " + middle + " " + last;
        cout << "Full name is: " << full << endl;
    }
};

int main()
{
    full_name obj1;
    obj1.getfn();
    obj1.getmn();
    obj1.getln();
    obj1.print();

    return 0;
}
Output for example of Multiple Inheritance:


3. Multilevel Inheritance


In this type of Inheritance, derived class is inherited from other derived class which is inherited from a base class.

Syntax:
// Multilevel Inheritance
class class_name
{
    // class body
};

class class_name1 : access_specifier class_name
{
    // class body
};

class class_name2 : access_specifier class_name1
{
    // class body
};

// Example:
class Base1
{
    // Class Body
};

class Base2 : public Base1
{
    // Class Body
};

class Derived : public Base2
{
    // Class Body
};
  

Example:

#include <iostream>
using namespace std;

class GetFirstName
{
    public:
    string FirstName;
    void getFN()
    {
        cout << "Enter first name: ";
        cin >> FirstName;
    }
};

class Name : public GetFirstName
{
    public:
    string LastName, FullName;
    void getLN()
    {
        cout << "Enter last name: ";
        cin >> LastName;
    }
};

class Print : public Name
{
    public:
    void print()
    {
        FullName = FirstName + " " + LastName;
        cout << "Full name: " << FullName;
    }
};

int main()
{
    Print obj1;
    obj1.getFN();
    obj1.getLN();
    obj1.print();
    return 0;
}
Output for example of Multilevel Inheritance:


4. Hierarchical Inheritance


In this type of Inheritance, more than one derived class is inherited from from a single base class. This means more than one child class is created from a single parent class.

Syntax:
// Hierarchical Inheritance
class class_name
{
    // class body
};

class class_name2 : access_specifier class_name
{
    // class body
};

class class_name3 : access_specifier class_name
{
    // class body
};

// Example:
class Base
{
    // Class Body
};

class Dericed1 : public Base
{
    // Class Body
};

class Derived2 : public Base
{
    // Class Body
};
  

Example:

#include <iostream>
using namespace std;

class A 
{
    public:
    int x, y;
    void getdata()
    {
        cout << "\n\nEnter value of x and y:\n";
        cin >> x >> y;
    }
};

class B : public A 
{
    public:
    void product()
    {
        cout << "Product= " << x * y;
    }
};

class C : public A
{
    public:
    void sum()
    {
        cout << "Sum= " << x + y << endl;
    }
};

int main()
{
    B obj1;
    C obj2;
    obj1.getdata();
    obj1.product();
    obj2.getdata();
    obj2.sum();
    return 0;
}
Output for example of Hierarchical Inheritance:


5. Hybrid Inheritance


The combination of more than one type of Inheritance is known as Hybrid Inheritance.

Example:

#include <iostream>
using namespace std;

class Add
{
    public:
    int num1, num2;
    int addition(int num1, int num2)
    {
        return num1 + num2;
    }
};

class Product
{
    public:
    int a, b;
    int product(int a, int b)
    {
        return a * b;
    }
};

class Average : public Add
{
    public:
    int c, d;
    void avg()
    {
        cout << "\nAverage of the 2 numbers = " << (float)Add::addition(c, d) / 2;
    }
};

class Print : public Add, public Product
{
    public:
    int e, f;
    void print()
    {
        cout << "Sum of the 2 numbers = " << Add::addition(e, f);
        cout << "\nProduct of the 2 numbers = " << Product::product(e, f);
    }
};

int main()
{
    Print obj1;
    Average obj2;
    cout << "Enter 2 numbers: \n";
    cin >> obj1.e >> obj1.f;
    obj1.print();
    obj2.c = obj1.e;
    obj2.d = obj1.f;
    obj2.avg();
    return 0;
}
Output for example of Hybrid Inheritance:



#ENJOY CODING

Post a Comment

FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP

Previous Post Next Post