Inheritance types in C++

125 views

In this tutorial, we will learn about different Inheritance types in C++ programming: Single, Multiple, Multilevel and Hierarchical inheritance with examples.

Types Of Inheritance

C++ supports five types of inheritance:

  • Single inheritance
  • Multiple inheritance
  • Hierarchical inheritance
  • Multilevel inheritance
  • Hybrid inheritance

C++ Single Inheritance

Single inheritance is defined as the inheritance in which a derived class is inherited from the only one base class.

class A { 
... .. ... 
};
class B: public A {
... .. ...
};

EXAMPLE : C++ Single Level Inheritance

When one class inherits another class, it is known as single level inheritance. Let’s see the example of single level inheritance which inherits the fields only.

#include <iostream> using namespace std; class Account { public: float salary = 60000; }; class Programmer: public Account { public: float bonus = 5000; }; int main(void) { Programmer p1; cout<<"Salary: "<<p1.salary<<endl; cout<<"Bonus: "<<p1.bonus<<endl; return 0; }

OUTPUT

Salary: 60000
Bonus: 5000

In the above example, Employee is the base class and Programmer is the derived class.

C++ Multilevel Inheritance

When one class inherits another class which is further inherited by another class, it is known as multi level inheritance in C++. Inheritance is transitive so the last derived class acquires all the members of all its base classes.

class A { 
... .. ... 
};
class B: public A {
... .. ...
};
class C: public B {
... ... ...
};

Here, class B is derived from the base class A and the class C is derived from the derived class B.

Let’s see the example of multi level inheritance in C++.

Example 1: C++ Multilevel Inheritance

#include <iostream> using namespace std; class A { public: void display() { cout<<"Base class content."; } }; class B : public A {}; class C : public B {}; int main() { C obj; obj.display(); return 0; }#include <iostream> using namespace std; class Animal { public: void eat() { cout<<"Eating..."<<endl; } }; class Dog: public Animal { public: void bark(){ cout<<"Barking..."<<endl; } }; class BabyDog: public Dog { public: void weep() { cout<<"Weeping..."; } }; int main(void) { BabyDog d1; d1.eat(); d1.bark(); d1.weep(); return 0; } #include <iostream> using namespace std; class A { public: void display() { cout<<"Base class content."; } }; class B : public A {}; class C : public B {}; int main() { C obj; obj.display(); return 0; }

Output

Eating... 
Barking... 
Weeping..

C++ Multiple Inheritance

In C++ programming, Multiple inheritance is the process of deriving a new class that inherits the attributes from two or more classes.

Syntax of Multiple Inheritance

class A { 
... .. ... 
};
class B { 
... .. ... 
};
class C: public A , {
... .. ...
};

Let’s see the example of multiple inheritance in C++.

Example 2: Multiple Inheritance in C++ Programming

#include <iostream> using namespace std; class A { protected: int a; public: void get_a(int n) { a = n; } }; class B { protected: int b; public: void get_b(int n) { b = n; } }; class C : public A,public B { public: void display() { std::cout << "The value of a is : " <<a<< std::endl; std::cout << "The value of b is : " <<b<< std::endl; cout<<"Addition of a and b is : "<<a+b; } }; int main() { C c; c.get_a(20); c.get_b(20); c.display(); return 0; }

Output

The value of a is : 20 
The value of b is : 20 
Addition of a and b is : 40

Ambiguity in Multiple Inheritance

The most obvious problem with multiple inheritance occurs during function overriding.

Suppose, two base classes have a same function which is not overridden in derived class.

If you try to call the function using the object of the derived class, compiler shows error. It’s because compiler doesn’t know which function to call. For example,

class base1 { public: void someFunction( ) {....} }; class base2 { void someFunction( ) {....} }; class derived : public base1, public base2 {}; int main() { derived obj; obj.someFunction() // Error! }

This problem can be solved using the scope resolution function to specify which function to class either base1or base2

int main() {
    obj.base1::someFunction( );  // Function of base1 class is called
    obj.base2::someFunction();   // Function of base2 class is called.
}

C++ Hierarchical Inheritance

Hybrid Inheritance is implemented by combining more than one type of inheritance. i.e. Combining Hierarchical inheritance and Multiple Inheritance. 

If more than one class is inherited from the base class, it’s known as hierarchical inheritance. In hierarchical inheritance, all features that are common in child classes are included in the base class.

For example, Physics, Chemistry, Biology are derived from Science class. Similarly, Dog, Cat, Horse are derived from Animal class.

Syntax of Hierarchical Inheritance

class base_class {
     ... .. ...
}

class first_derived_class: public base_class {
     ... .. ...
}

class second_derived_class: public base_class {
     ... .. ...
}

class third_derived_class: public base_class {
     ... .. ...
}

Let’s see the example of Hierarchical Inheritance inheritance in C++.

Example: Hierarchical Inheritance in C++ Programming

// C++ program to demonstrate hierarchical inheritance #include <iostream> using namespace std; // base class class Animal { public: void info() { cout << "I am an animal." << endl; } }; // derived class 1 class Dog : public Animal { public: void bark() { cout << "I am a Dog. Woof woof." << endl; } }; // derived class 2 class Cat : public Animal { public: void meow() { cout << "I am a Cat. Meow." << endl; } }; int main() { // Create object of Dog class Dog dog1; cout << "Dog Class:" << endl; dog1.info(); // Parent Class function dog1.bark(); // Create object of Cat class Cat cat1; cout << "\nCat Class:" << endl; cat1.info(); // Parent Class function cat1.meow(); return 0; }

Output

Dog Class:
I am an animal.
I am a Dog. Woof woof.

Cat Class:
I am an animal.
I am a Cat. Meow.

Here, both the Dog and Cat classes are derived from the Animal class. As such, both the derived classes can access the info() function belonging to the Animal class.

You may also like

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More

Enable Notifications    OK No thanks