Hello again!
In this video, we are going to look at virtual inheritance.
One of the problems which can arise in multiple inheritance is if the parent classes both inherit from
the same base class.
As an example, we could have an Employee class which has, let's say, a name, a job description
and a salary.
A child class, SalesEmployee.
This is an Employee who also has an expense account, and gets paid a commission.
We have another child class, Manager.
This is an Employee, who manages other Employees.
Then we have a SalesManager class which inherits from both these.
So a SalesManager is an Employee, who has an expense account, and gets paid commission, and also has other
Employees working for them.
So you can see that the SalesManager inherits from Employee twice. once through the SalesEmployee
class, and once through the Manager class.
If we create a SalesManager object, this will contain objects of both the parents, so it will contain
a SalesEmployee object, and a Manager object.
Each of those objects will contain an object of their own parent class, so they will each contain
a separate Employee object.
And this means that the SalesManager object contains two Employee objects.
And this is not desirable, because the SalesManager is only one Employee.
If our program results in the company paying the SalesManager two salaries, then the company is not
going to be very pleased with our work!
So let's look at the problem.
We have "skeletons" for all these classes.
We have a member function which will print out the address of the Employee object.
This is not virtual, so it will only be called for the Employee object.
This will be inherited by all the child functions, but not overridden.
We have the SalesEmployee and the Manager class, which inherits from this Employee. And then the
SalesManager inherits from both those.
And I am afraid I forgot this in the last video.
If you want public multiple inheritance, you have to put the public keyword before both the parents.
And I forgot that. But somehow my code still worked, so apologies if that caused any problems.
In the main function, we have a SalesManager object.
This will contain a SalesEmployee object and the Manager object.
We are actually allowed to access those inner objects through pointers, so we can take the address of the
SalesManager, cast it to SalesEmployee, and that will give us a pointer to the SalesEmployee part
of the SalesManager.
And we can also do the same thing, to get a pointer to the Manager part of the object.
And then we call the address() member function. And that will display the address of the Employee part
of the SalesEmployee and the Manager.
So this is the address of the Employee inside the SalesEmployee part.
And this is the address of the Employee object inside to the Manager part.
And you can see they are different.
So we do have two different objects.
The way to solve this is to use what is called "virtual inheritance".
So we now say that SalesEmployee uses public virtual inheritance from Employee, and Manager uses public
virtual inheritance from Employee.
This makes the Employee class a "virtual base" of SalesEmployee and Manager.
And this means that they can both share an Employee object.
And we do not need to do anything unusual for the SalesManager.
So when we create a SalesManager object, this will still contain a SalesEmployee and Manager objects.
But those were both the same Employee base class object.
So we now have an object hierarchy that looks like this.
We have our SalesManager which inherits from SalesEmployee and Manager. And contains those objects.
But the SalesEmployee object and the Manager object share the same Employee.
If I change this so we now use virtual inheritance for the SalesEmployee and also for the Manager.
The rest of the code stays the same.
So when we now make this SalesManager object, the SalesEmployee part and the Manager part will
be showing the same Employee object, and we should get the same addresses.
And there we are.
So we now have just one Employee object, and the SalesManager only gets paid one salary!
Okay.
So that is it for this video.
I will see you next time.
But until then, keep coding!