Last updated on:May 26th, 2018,

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

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

In this tutorial, we will learn about the followings;

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

What is Multiple inheritance in C++ OOP?

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

Syntax of multiple inheritance in C++ OOP

Serial#Code
1class child_class_name :Access_specifier 1st_parent, 

                                     Access_specifier 2nd_parent

2{
3Body of the class;
4};

Example of multiple inheritance in C++ OOP

Serial#Code
1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

#include<iostream>

using namespace std;

class sum1

{

protected:
int n1;
};
class sum2
{
protected:

int n2;
};

class show : public sum1, public sum2

{

public:

int total()

{

cout<<“enter n1″<<endl;

cin>>n1;

cout<<“enter n2″<<endl;

cin>>n2;

cout<<“sum=”<<n1+n2<<endl;

}

};

int main()

{

show myobject;

myobject.total();

}

 

Line 11:

Line 11 shows multiple inheritance where child class” show” have two parents “sum1” and “sum2”.

Child class “Show” behaviour is as follows;

  • show inherits variable n1 from  sum1
  • Show inherits variable n2 from sum2
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:
No
.

0Shares

Leave a Reply

Your email address will not be published.