WEBVTT

00:00.150 --> 00:00.630
Hello again!

00:00.900 --> 00:03.540
In this video, we are going to look at virtual inheritance.

00:06.820 --> 00:13.330
One of the problems which can arise in multiple inheritance is if the parent classes both inherit from

00:13.330 --> 00:14.560
the same base class.

00:18.120 --> 00:22.950
As an example, we could have an Employee class which has, let's say, a name, a job description

00:22.950 --> 00:23.850
and a salary.

00:25.170 --> 00:27.270
A child class, SalesEmployee.

00:27.780 --> 00:32.400
This is an Employee who also has an expense account, and gets paid a commission.

00:33.570 --> 00:35.220
We have another child class, Manager.

00:35.640 --> 00:38.400
This is an Employee, who manages other Employees.

00:39.570 --> 00:43.020
Then we have a SalesManager class which inherits from both these.

00:43.530 --> 00:50.010
So a SalesManager is an Employee, who has an expense account, and gets paid commission, and also has other

00:50.190 --> 00:51.630
Employees working for them.

00:52.620 --> 00:58.170
So you can see that the SalesManager inherits from Employee twice. once through the SalesEmployee

00:58.170 --> 01:01.140
class, and once through the Manager class.

01:04.110 --> 01:10.170
If we create a SalesManager object, this will contain objects of both the parents, so it will contain

01:10.170 --> 01:13.410
a SalesEmployee object, and a Manager object.

01:14.400 --> 01:19.380
Each of those objects will contain an object of their own parent class, so they will each contain

01:19.380 --> 01:21.030
a separate Employee object.

01:21.690 --> 01:26.790
And this means that the SalesManager object contains two Employee objects.

01:27.540 --> 01:31.860
And this is not desirable, because the SalesManager is only one Employee.

01:32.880 --> 01:38.190
If our program results in the company paying the SalesManager two salaries, then the company is not

01:38.190 --> 01:39.680
going to be very pleased with our work!

01:42.530 --> 01:43.850
So let's look at the problem.

01:44.360 --> 01:47.180
We have "skeletons" for all these classes.

01:48.230 --> 01:52.640
We have a member function which will print out the address of the Employee object.

01:53.330 --> 01:56.810
This is not virtual, so it will only be called for the Employee object.

01:57.440 --> 02:00.830
This will be inherited by all the child functions, but not overridden.

02:02.400 --> 02:08.580
We have the SalesEmployee and the Manager class, which inherits from this Employee. And then the

02:08.580 --> 02:10.860
SalesManager inherits from both those.

02:11.550 --> 02:13.980
And I am afraid I forgot this in the last video.

02:14.310 --> 02:19.740
If you want public multiple inheritance, you have to put the public keyword before both the parents.

02:20.640 --> 02:27.660
And I forgot that. But somehow my code still worked, so apologies if that caused any problems.

02:28.260 --> 02:31.080
In the main function, we have a SalesManager object.

02:31.380 --> 02:34.830
This will contain a SalesEmployee object and the Manager object.

02:36.430 --> 02:42.190
We are actually allowed to access those inner objects through pointers, so we can take the address of the

02:42.190 --> 02:49.030
SalesManager, cast it to SalesEmployee, and that will give us a pointer to the SalesEmployee part

02:49.030 --> 02:50.410
of the SalesManager.

02:51.370 --> 02:54.940
And we can also do the same thing, to get a pointer to the Manager part of the object.

02:55.690 --> 03:00.400
And then we call the address() member function. And that will display the address of the Employee part

03:00.880 --> 03:03.610
of the SalesEmployee and the Manager.

03:06.930 --> 03:10.680
So this is the address of the Employee inside the SalesEmployee part.

03:11.340 --> 03:15.510
And this is the address of the Employee object inside to the Manager part.

03:16.050 --> 03:17.190
And you can see they are different.

03:17.580 --> 03:19.560
So we do have two different objects.

03:21.600 --> 03:24.720
The way to solve this is to use what is called "virtual inheritance".

03:25.380 --> 03:33.930
So we now say that SalesEmployee uses public virtual inheritance from Employee, and Manager uses public

03:33.930 --> 03:36.120
virtual inheritance from Employee.

03:36.990 --> 03:42.150
This makes the Employee class a "virtual base" of SalesEmployee and Manager.

03:42.720 --> 03:46.170
And this means that they can both share an Employee object.

03:47.340 --> 03:50.490
And we do not need to do anything unusual for the SalesManager.

03:51.330 --> 03:57.660
So when we create a SalesManager object, this will still contain a SalesEmployee and Manager objects.

03:57.900 --> 04:01.680
But those were both the same Employee base class object.

04:03.910 --> 04:06.540
So we now have an object hierarchy that looks like this.

04:06.550 --> 04:12.580
We have our SalesManager which inherits from SalesEmployee and Manager. And contains those objects.

04:13.030 --> 04:18.160
But the SalesEmployee object and the Manager object share the same Employee.

04:19.090 --> 04:25.060
If I change this so we now use virtual inheritance for the SalesEmployee and also for the Manager.

04:25.900 --> 04:27.670
The rest of the code stays the same.

04:28.990 --> 04:35.560
So when we now make this SalesManager object, the SalesEmployee part and the Manager part will

04:35.560 --> 04:39.580
be showing the same Employee object, and we should get the same addresses.

04:41.440 --> 04:42.010
And there we are.

04:42.460 --> 04:47.470
So we now have just one Employee object, and the SalesManager only gets paid one salary!

04:48.640 --> 04:48.970
Okay.

04:48.970 --> 04:50.050
So that is it for this video.

04:50.560 --> 04:51.400
I will see you next time.

04:51.400 --> 04:53.740
But until then, keep coding!
