Multiple inheritance in C++ with Syntax and Examples

Multiple inheritance in C++ OOP, Example and syntax of multiple inheritances

In this tutorial, we will learn about the followings;

  1. Multiple inheritances in C++ OOP
  2. Example Multiple inheritances in C++ OOP 
  3. The syntax of multiple inheritance

What is Multiple inheritance in C++ OOP?

In multiple inheritance, the child class is derived from more than one parent class.

Multiple inheritance

Syntax of multiple inheritance in C++ OOP

class child_class_name : Access_specifier 1st_parent,                                      Access_specifier 2nd_parent{Body of the class;

};

#include<iostream>Example of multiple inheritance in C++ OOP

Output

enter n1?

3

enter n2?

4

sum=7

Ambiguity in Multiple Inheritance in C++ is the most common problem.

Difference between multiple and single Inheritance in C++

Here, i am showing you a comparison of multiple and single inheritance in C++.

MULTIPLE INHERITANCE SINGLE INHERITANCE
In Multiple inheritance derived class have more than one base class. In Single inheritance derived class have only single base class.
More overhead in Multiple inheritance and  requires more run time as compared to single inheritance. Less overhead in single inheritance and  requires small run time as compared to multiple inheritance.
Multiple inheritance is a lot of close to generalization. Single inheritance is a lot of close to specialization.
Syntax of multiple inheritance is

Class DerivedClass_name : access_specifier Base_Class1, access_specifier Base_Class2, ….{}; .

Syntax of single inheritance is

Class DerivedClass_name : access_specifier Base_Class{};.

Multiple inheritance is more complex as compared to the single inheritance. Single inheritance is simple as compared to the multiple inheritance.
Multiple inheritance is supported by C++ but Multiple inheritance can’t be implemented in most of other programming languages e.g, C# and Java doesn’t support multiple inheritance through classes. Mostly all of the programming languages supports Single inheritance.

Difference between multiple and multi level Inheritance in C++

Here, i am showing you a comparison of multiple and multi level inheritance in C++.

Multiple Inheritance Multilevel Inheritance
In Multiple Inheritance a class inherits from more than one parent classes. In Multilevel Inheritance parent shares inherits with child and then child becomes parent of other class and share the resources of parent to its child.

Class Levels

Multiple Inheritance has two class levels namely, base class and derived class. Multilevel Inheritance has three class levels namely, base class, intermediate class and derived class.

 Usage

Multiple Inheritance is complex as compared to multi level inheritance and not widely used. Multilevel Inheritance is widely used as compared to multiple inheritance.

Exercise and Solution Multiple inheritance

  1. WAP in C++ to display such a pattern for n number of rows using a number which will start with the number 1 and the first and the last number of each row will be 1 with the help of multiple inheritance.
  2. C++ program to print a hollow square or rectangle star pattern with the support of Multiple inheritance.
  3. Multiple inheritance C++ program to display the pattern like a pyramid.
  4. Multiple inheritance C++ program to show the sum of an A.P. series.
  5. Multiple inheritance C++ program to print the string in reverse order.
  6. Multiple inheritance C+ Program Armstrong number of n digits.
  7. Multiple inheritances C++ program to display a pattern like the right angle.
  8. Multiple inheritances C++ program to display the cube of the number up to a given integer.
  9. Multiple inheritance C++ Program to convert a decimal number into binary.
  10. Develop a program in C++ to display the pattern like a pyramid using an asterisk * and each row contains an odd number of asterisks by using the multiple inheritances.
  11. Multiple inheritance C++ program to convert a decimal number into binary.
  12. Write a C++ program to print the rhombus star pattern of N rows using Multiple Inheritance.
  13. Write a program in C++ to convert a decimal number to hexadecimal using the Multiple inheritances in object-oriented programming (OOP).
  14. Write a program in C++ to convert a decimal number into octal without using an array using Multiple inheritances in object-oriented programming(OOP).
  15. C++ program to display Pascal’s triangle using the Multiple inheritances.
  16. Multiple inheritances C++ program to print hollow rhombus, parallelogram star pattern.
  17. Multiple inheritance C++ program pattern like a pyramid repeat.
  18. Multiple Inheritance C++ program to print mirrored right triangle star pattern.
  19. Multiple inheritance C++ Program to convert an octal number into binary.
  20. Multiple inheritances Floyd Triangle C++.
  21. C++ Program to display the sum of the series using Multiple inheritances.
  22. Multiple Inheritance C++ program to find Strong Numbers within a range of numbers.
  23. Multiple inheritances C++ to display the n terms of odd natural number and their sum.
Test Your Understandings

1.class show : public sum1, public sum2, here sum1 and sum2 are child classes of class sum1? YES / NO

Answer - Click Here:
NO

2.Multiple inheritance means one class having more than one parents? YES / NO

Answer - Click Here:
Yes

Topic Covered

Multiple inheritance  in C++ OOP, Example and syntax of multiple inheritance.