C++ Inheritance in OOP ~ Types of Inheritance in OOP with Examples ~ Inheritance in C++

C++ Inheritance in OOP | Inheritance in C++ |Types of Inheritance in OOP with Examples | Advantage and Disadvantages of using Inheritance |OOP Tutorial in C++

In this tutorial, we are going to learn about the C++  Inheritance in OOP ,  Inheritance in C++, Types of Inheritance in OOP, Advantage and Disadvantages of using Inheritance, OOP Tutorial in C++

What is Inheritance in C++?

Inheritance is one of the key features of Object-oriented programming in C++. Inheritance is the capability of one class to acquire properties and characteristics from another class. Inheritance makes the code reusable.


We group the "inheritance concept" into two categories:


1. derived class ( child or sub) - the class that inherits from another class

2. base class ( parent or super ) - the class being inherited from another class


To inherit from a class, use the : symbol.

Purpose of Inheritance in C++:

1. Code Reusability

2. Method Overriding (Hence, Runtime Polymorphism.)

3. Use of Virtual Keyword

Basic Syntax of Inheritance:

class Subclass_name : access_mode Superclass_name

is-a relationship ?

Inheritance is an is-a relationship. We use inheritance only if an is-a relationship is present between the two classes.

Here are some examples:
  • A car is a vehicle 
  • Orange is a fruit
  • A surgeon is a doctor
  • A dog is an animal

class Animal {

    // eat() function

    // sleep() function

};

class Dog : public Animal {

    // bark() function

};

Here, the Dog class is derived from the Animal class. Since Dog is derived from Animal, members of Animal are accessible to Dog.

Example:

// Base class
class Vehicle {
  public:
    string brand = "Ford";
    void honk() {
      cout << "Tuut, tuut! \n" ;
    }
};

// Derived class
class Car: public Vehicle {
  public:
    string model = "Mustang";
};

int main() {
  Car myCar;
  myCar.honk();
  cout << myCar.brand + " " + myCar.model;
  return 0;
}

Output:

Tuut, tuut!
Ford Mustang

Types Of Inheritance in OOP:

C++ supports five types of inheritance:

Single inheritance

Multiple inheritance

Hierarchical inheritance

Multilevel inheritance

Hybrid inheritance ( also known as Virtual )

C++ Single Inheritance in OOP:

Single inheritance is defined as the inheritance in which a derived class is inherited from the only one base class. It is the most simplest form of Inheritance.


 Where 'A' is the base class, and 'B' is the derived class.

Example of Single Inheritance in C++:

#include <iostream>
using namespace std;

class Mec {
   public:
    void cse() {
        cout << "Welcome To CSE Department!!" << endl;
    }
    void eee() {
        cout << "Welcome To EEE Department!!" << endl;
    }
};

class Department : public Mec {
   public:
    void civil() {
        cout << "Welcome To CIVIL Department!!" << endl;
    }
};

int main() {
    Department name;
    name.cse();
    name.eee();
    name.civil();
    return 0;
}

Output:

Welcome To CSE Department!!

Welcome To EEE Department!!

Welcome To CIVIL Department!!

C++ Multilevel Inheritance in OOP:

Multilevel inheritance is a process of deriving a class from another derived class.


Example of Multilevel Inheritance in C++:

#include <iostream>
using namespace std;
class Mec {
   public:
    void cse() {
        cout << "Welcome To CSE Department!!" << endl;
    }
    void eee() {
        cout << "Welcome To EEE Department!!" << endl;
    }
};

class Civil : public Mec {
   public:
    void civil() {
        cout << "Welcome To CIVIL Department!!" << endl;
    }
};

class Department : public Civil {
};

int main() {
    Department name;
    name.cse();
    name.eee();
    name.civil();
    return 0;
}

Output:

Welcome To CSE Department!!

Welcome To EEE Department!!

Welcome To CIVIL Department!!


C++ Multiple Inheritance in OOP:

Multiple inheritance is the process of deriving a new class that inherits the attributes from two or more classes.

 

Syntax of the Derived class:

1. class D : visibility B-1, visibility B-2, ?  

2. {  

3.     // Body of the class;  

4. }


Example of Multiple Inheritance in C++:

#include <iostream>
using namespace std;

class Mec {
   public:
    void cse() {
        cout << "Welcome To CSE Department!!" << endl;
    }
    void eee() {
        cout << "Welcome To EEE Department!!" << endl;
    }
};

class Civil {
   public:
    void civil() {
        cout << "Welcome To CIVIL Department!!" << endl;
    }
};

class Department : public Civil,public Mec{
};

int main() {
    Department name;
    name.cse();
    name.eee();
    name.civil();
    return 0;
}

Output:

Welcome To CSE Department!!

Welcome To EEE Department!!

Welcome To CIVIL Department!!

C++ Hybrid Inheritance in OOP:

Hybrid inheritance is a combination of more than one type of inheritance. Hybrid Inheritance is combination of Hierarchical and Multi-level Inheritance.

 


Example of Hybrid Inheritance in C++:

// C++ Program To Demonstrate Hybrid Inheritance #include <iostream> using namespace std; class Mec { public: void mec() { cout<<"Welcome To MEC!!"<<endl <<"List of Departments :"<<endl; } }; class Civil : public Mec { public: void civil() { cout << " 1. CIVIL Department" << endl; } }; class Eee : public Civil { public: void eee() { cout << "2. EEE Department" << endl; } }; class Cse { public: void cse() { cout << "3. CSE Department" << endl; } }; class Department : public Cse,public Eee { public: void dept() { mec(); civil(); eee(); cse(); } }; int main() { Department name; name.dept(); return 0; }

Output:

Welcome To MEC!!

List of Departments :

1.CIVIL Department

2.EEE Department

3.CSE Department

C++ Hierarchical Inheritance in OOP :

The hierarchical inheritance is defined as the process of deriving of several classes from a base class.


 

Syntax of Hierarchical Inheritance:

1. class A  

2. {  

3.     // body of the class A.  

4. }    

5. class B : public A   

6. {  

7.     // body of class B.  

8. }  

9. class C : public A  

10. {  

11.     // body of class C.  

12. }   

13. class D : public A  

14. {  

15.     // body of class D.  

16. }   

Example of Hierarchical Inheritance in C++:

#include <iostream>
using namespace std;

class Department {
   public:
    void cse() {
        cout << "Welcome To CSE Department!!" << endl;
    }
};

class Civil : public Department {
    public:
     void civil() {
        cout << "Welcome To CIVIL Department!!"<<endl;
     }
};

class Eee : public Department {
    public:
     void eee() {
        cout << "Welcome To EEE Department!!" << endl;
    }
};

int main() {
    Eee info;
    Civil info2;
    info.cse();
    info.eee();
    info2.civil();
    return 0;
}

Output:

Welcome To CSE Department!!

Welcome To EEE Department!!

Welcome To CIVIL Department!!

C++ Ambiguity Resolution in Inheritance:

By using the multiple inheritance when a function with the same name occurs in more than one base class, ambiguity will be occurred.

What are the pros and cons of using inheritance? 

Disadvantages of Inheritance in OOP :- 

1. Inherited functions work slower than normal function as there is indirection.
2. Improper use of inheritance may lead to wrong solutions.
3. Often, base class data members remain unused, which can lead to memory leaks. 
4.The coupling between base class and derived class is increased by inheritance A change in base class will affect all the child classes.

Benefits of inheritance in OOP:

1. The inheritance promotes reuse.
2. When a class inherited or drifts from another class, it can access all the features of the inherited class. 
3. Reusability enhanced reliability.
4. The base class code will already be tested and debug. 
5. As the existing code is reused, it leads to less development and maintenance costs. 
6. Inheritance makes the sub classes follow a standard interface. 
7. Inheritance helps to reduce code redundancy and supports code extensibility. 

8. Inheritance facilitates creation of class libraries.

Previous Post: C++ Object Pointers & Object Reference in OOP | Reference vs Pointer



1 Response to C++ Inheritance in OOP ~ Types of Inheritance in OOP with Examples ~ Inheritance in C++

  1. Thanks for this great article. Keep it up.
    I learned many things about the C++ Inheritance in OOP , Inheritance in C++, Types of Inheritance in OOP, Advantage and Disadvantages of using Inheritance, Inheritance in C++ with example program

    ReplyDelete