Multiple inheritance in C++

Multiple inheritance

The type of inheritance in which there are multiple base classes are called multiple inheritance.

Though multiple inheritance has multiple base classes, it has only single derived class.

 

I will explain this again.

 

(say with me)

A multiple  base class is such a class where there are multiple base classes but a single derived class.

 

We will write a program for this.

 

 

How we write any program related to multiple inheritance? The basic concept is very important.

 

 

There are tons of websites that just vomit out the source code.

 

There are tons of another types of websites that vomits the source code with unrelated explanation.

 

I don’t want to be any of them. My mission is simple. My readers should not feel scammed after reading my blogs. My readers should not feel regrets after reading this. My readers should not need to go to visit another blogs after reading this.

 

We need multiple(=say 2 in this case) base classes.

Plus, we need only 1 derived class.

 

A basic code outlook will look like this.

 

Class base1

{

}

Class base2

{

}

Class derived: public base1,public base2

{

}

int main()

{

Derived d;

}

 

 

 

Now things look simpler than before.

 

 

We see the full code as below.

 

 

Code-:

Its output comes as follows-:

enter the value to x of a base1 1
enter the value to y of a base 2 2
enter the value to z of a derived class 3
the sum=6

 

Program makes sense-:

we called that multiple inheritance meant multiple base classes and single derived class.

here the multiple base classes are the classes base1 and base2.

likewise, we have only derived class “derived”.

but here is some difference-:

in the single inheritance, we had bring employee::input function and employee::output() function to display overriden members{ using derived classs object for accessing overriden members of a base class).

http://www.scitechtop.com/single-inheritance-in-c-types-of-inheritance/

 

in the above case the function name were same for manager and employee so the case of “accessing overridden members” came.

but in our case, we have used three different function get_x,y and z. so, that case doesn’t comes up.

Is shorter blog posts working fine for your learning style? Or, is your learning style in trouble because of this new short blog posts?

 

I did this for 2 main reasons-:

  • The shorter the blog posts, the more posts I can write in less time.
  • The shorter the post, the easier it becomes for you to read the blog post. And since most of you come here to solve some problem, I think shorter blog posts significantly help for problem solvers.

 

If you have any dissatisfaction with blog, comment below.

 

But I know that not all are comment friendly people.

So email me at-: scitechtop@gmail.com

 

Leave a Comment

Your email address will not be published. Required fields are marked *

Do NOT follow this link or you will be banned from the site! Scroll to Top