Hello everyone.
So in the last video we discussed about different types of inheritance.
Okay.
In this video we will discuss about the fifth type.
So in this ruling we will discuss about the fifth type of inheritance which is the hybrid inheritance.
So what is the meaning of hybrid.
Hybrid means mix.
OK.
So in order to achieve hybrid inheritance what we will do we will mix that for types of inheritance
that we want to distribute.
Let us take an example.
So in we are Duplessis let's say see that it would even be now at this time I am using multiple in that
it is now be inherited classy.
So I am using my beloved inheritance here and now I am using hierarchal inheritance.
OK.
Let's take one more example.
So suppose I have a class A now B and C both class in that it's from Class A.
Okay.
And now we have a class B which inherits from both Class B and Class C.
OK so what will happen here is simple.
So what over the structure of the guess over the list trophy the structural failure will be very simple.
It will have only a specific properties then since being named from classy it will have a some B specific
properties.
So this is B.
And the structure of C will be some C specific properties and a so where the structure of the the structure
of the will be very simple.
So this is D day inherits from Class B and C.
So this is B B inherits from Class A and B specific properties.
Similarly c c inherits from Class A C specific properties and then we will have some D specific properties.
Okay now what will happen since Class D has two copies of glass.
Okay.
Glasby has two copies of glossy.
So what it will do in future it will create ambiguity.
How do we create ambiguity.
Suppose this class he has a brain function inside public section.
Okay so what will happen here we have a brain function.
And similarly this is also a spring function.
Now if we want to call print function that compiler will get confused which print function to call okay.
So it will create ambiguity.
Now let us take an example.
So suppose we have a weekly class now both the classes God and truck they inherited from this working
class.
Okay so this is truck class and now bus class inherits from both car and truck.
Okay.
So this type of structure this is called a diamond problem and regret and be used in future.
So this diamond problem it is a very well known problem.
Okay so what will happen.
So as we can see above.
So this is also a diamond problem.
So in Diamond problem we have ambiguity also.
We have multiple copies Okay so due to multiple copies we will have ambiguity.
Okay now our aim is to avoid multiple copy our aim is to avoid ambiguity.
Okay now let's see in code how we can achieve the diamond problem and how we can resolve the diamond
problem.
Okay so let's say in called So this is our code we have of the class and inside the working class we
have a default constructor a paramedic's constructor print function and a destructor Okay so we have
a class card which inherits from the vehicle class.
So we have a default constructor and a destructor.
Okay.
Similarly we have a class truck this also inherits from the working class and we have a constructor.
Now here comes class bus it generates from class truck and also it generates from class got and it also
inherits from class truck.
And this is simple a constructor okay.
So now our domain column is ready.
And now let us first create the object of bus class okay so must be.
Now what will happen if I add on this file.
What would be the order of the constructor call let us try to understand
so we have the class both the God and the truck class they inherited from the class and similarly class
was inherited from God and truck.
Okay so I am creating an object Busby.
So what will happen first of all what was the order of the constructor call.
So here we will write our order.
So what will happen before.
As soon as we create an object of must be we will go to God class to call it to call its constructor
first by car class first because I have written car first and then truck.
Okay so if we all know that before the bus class construct every bit called its parent class constructor
will be called since there are two buildings car and truck.
So first car class constructively record by car class because I have written God first.
So God bless constructively required we will reach the car class and the parent of quiet classes vehicle.
So we will go head and then the vehicle left constructor will call it first.
So first of all we could constructively be called then we will reach to God again.
Now the car at last constructed will be called then we will reach the bus and then we will each truck.
So before calling their top class our class constructor truck will call its parent class constructor
which is we call.
So now that we like construct it will be called then we will come back to Truck.
So now the top class constructor will be called.
And now finally the best class constructor will be called okay.
So this will be the order of the cost of protocol.
Okay.
So we can see here the working class constructor will be called twice.
Okay so let us try to verify our output.
So let's send this file
so this is the order.
So this is the order the first vehicle then God.
Then again vehicle then truck and then bus.
OK.
So do not look like the order of the destructor.
Why.
Because we have not created district for every class.
Okay.
But we know the order of district will be just the opposite of the Order of the inheritance.
Okay so the order of destructor will be the opposite of this.
Okay.
Now let's do one more thing.
Let us try to call brain function.
Okay.
So be dot
surveys the brain function so bus Gus doesn't have any print function proc doesn't have brain function.
Similarly car doesn't have brain function.
Actually it has print function but we have commanded it out so we could gloss has the print function
since the print function is public it is accessible to every derived class.
Okay.
So be has access to the print function.
So that means we will not get an error due to accessibility.
Let's see whether we will get in that error or not.
So we will get we will definitely get an error and that it will be due to ambiguity.
So what is happening what is the structure of bus class.
So the structure of bus class is quite simple
so this is our bus class.
So bus class inherits from car and truck.
So this is car and this is truck so inside the car plus I have the class we can.
And similarly to class inherits from the class.
So this vehicle class has brain function and this working class is also high Sprint function.
Basically both the print function are exactly the same.
Okay.
So when will run the file.
We will get in the red line number 17 that print function is present in multiple base classes.
Okay.
There will be ambiguity problem.
I'll get compiler will get confused.
Which print function to call.
Although both the print function are exactly the same.
But compiler will get confused.
Okay so now let's send this file to see so we are getting an error and that is due to ambiguity.
You can see that and also so print is ambiguous.
Okay.
Now how can we resolve this header.
So there are two ways to resolve this error.
First way is to add a print function inside the bus glass.
Okay.
So as soon as we will as soon as we write a print function inside the bus glass and beauty problem will
be resolved widely as all because in the last session because in the last radio we have seen that whenever
a whenever a class so in the last few days we have seen that whenever a class as a function of its own
then first of all we will check the bus class only since the bus class has the print function.
We will not go to its parent class.
OK.
So there will be no ambiguity.
So when I call this function without print this brain function will be called.
Okay.
And our output will be buzz.
Okay so let's send this file
so I would output this bus.
Okay.
Now that and other way to solve this problem is to specify the class.
Okay.
We will specify the class since both.
So what is the structure.
So let us write the structure here.
Okay so let's write the structure.
So we have.
So we have this class bus this bus glass inherits from God and from truck and this is the class.
So this is also a vehicle.
This will go class has a print function.
This will allow us all to this brain function.
And now let us comment out the brain function of the bus class.
Okay now bus class doesn't have in the print function.
So when I write B dot print what will happen.
Obviously we will get an error because two print functions are already there.
Okay.
So to resolve this error the second way was to specify the class name.
So either we have to specify the car or we have to specify the truck.
We cannot specify the vehicle because both car and truck as we call it will not so on what ambiguity
problem.
So we have to specify our data card or either truck.
Okay.
So let us specify the car.
So we will write we dart card and then scope resolution operator okay.
So let's send this file so now I would put this vehicle.
Okay so our problem is resolved.
Now what if
notify and comment the print function of the car plus.
So what will happen now.
Okay.
So what happened is we have this glass cut this glass also has a brain function of its own.
Now what will happen if I write.
So if I write B dot God it's called resolution or greater print.
What will happen.
So when I write B dot God scope resolution operator and then the print function and if I had eight B
dart print and if I had eight B dart school resolution operator and print.
So what will the output of all these three lines.
Okay so first of all world output of this line.
So first of all this line will throw us at it via debt because inside the bus function we do not have
any print function since we do not have brain function.
We will go to its parent class which is God in print and now there will be ambiguity why ambiguity because
there are now three print functions okay.
That t print functions now.
So there will be ambiguity.
So obviously this will throw us at it.
Okay.
Now let's see this beat out God school resolution operator and print.
Now what do we do it very clearly specifies go to go of class.
And since the kind class has print function of its own.
So this print function will be called.
Okay.
And B Da plUG Scott resolution operator print will happen.
We will go to class.
Class do not have print function of its own.
So it will go to its parent class parent class s print function.
So print this time this print function will be called.
Okay.
So it will call this one and it will call this one.
Okay let's send this file so what we are output now this brain function will be called Card print function.
Okay let's send this file so we can see here num dials num gives.
Okay.
So now I'm biased num gives and colors.
So God brain function is being getting call okay.
Now we have cleared them beauty but still there is one big problem.
Okay.
So what is the big problem.
So ambiguities is old.
Now we all know ambiguities is all but there is still one very big problem that we need to handle.
And the very big problem is that there are two copies clause D will have two copies of glass.
Okay so this is glass D and inside the glass V I have glass B glass B has a and similarly class C has
a.
Okay so here we have two copies of glass a.
Now we have to solve this problem.
Okay.
I definitely we want only one copy.
We need only one copy of glass a.
Okay.
So we have to avoid this copying how can we avoid this copying.
We can use the keyboard what tool.
Okay.
So there is a keyword.
What tool.
Now let's see.
Well we have to write a tool to solve this copy problem so glass card before writing the public.
We will write virtual and similarly for track class we have to write what tool
okay.
Now what this virtual will do.
So this virtual will ensure that class D will now have only one copy of class A.
Okay so let's come in this out.
So now we are creating an object of bus B.
So if I will run this file what with the order of the constructor call let us try to understand
so I have this working class then car and truck so guide classes in editing class classical but we have
a keyboard virtual okay.
We are in editing the class but we have a keyword virtual.
So what what should we do.
Let's try to understand and we have bus here.
Okay.
So C must be.
So what will happen so first of all where the best class will do it will go to its parent class to call
its default constructor so will reach GA class since the Cairo class is virtually inheriting from the
working class.
So what will happen.
Bus class will call the vehicle class bus class will call the constructor their default constructor
of the vehicle class so the order of the the order of the constructor call will be first this vehicle
glass constructor will be called and who will call boss class constructor will be called.
This cannot call that God cannot call because they are inheriting the vehicle glass of water surely
so they cannot call the constructor.
Okay so first it will be called victory in this construct and will be called then God class and then
track class and then finally the best class constructor will be called.
So as we can see here the class construct will be called only once okay and who will call but will call
neither God nor truck.
Okay so let's let's see the.
Now let's run this file so as you can see first recalled car truck and then bus.
OK so because construct based call only once not twice alert was called place.
OK.
But here it will recall only once.
And who will call boss class will call.
OK so what is the concept of a virtual based class.
OK.
So how we're actually helping here.
What is the purpose of like what is the meaning of this virtual based class.
Okay.
So earlier what was happening this car plus
has object as object of virtual class.
So this was really a situation before using the virtual keyboard.
Now we are using what tool show vehicle glasses now a virtual based class.
So what will happen now.
So this is got a glass This is glass.
So earlier the gardener truck glass they have the actual vehicle object but now they will have a pointer.
Similarly truck will have a pointer to the vehicle object they now have a pointer to difficult object
earlier they have the vehicle object but now they have a pointer to the vehicle object.
Okay.
And really the structure of the bus so Elliot bus only has got an object but no bus will have vehicle
object it will have cart object and it will have object.
So that's why a bus will call the vehicle's last constructor no ID card neither car neither truck.
OK so bus will be able to call the world class constructor working class constructor and God and I cannot
call the vehicle class constructor because they only have pointer and not the actual object.
Okay so now we are avoiding the copy.
Okay.
So now we had a wedding copy we only have one copy.
There are not multiple copies.
Okay.
Now let's see one more thing.
Okay
okay.
So now let's come in out the cost of the default construct of the class.
Now what will happen.
Okay.
So since we have better merge based constructor so the default construct better if we could class is
not visible to us okay.
So what will happen.
So now we have to call the Better based constructor.
OK so since the default constructor weaker class is not visible.
So now we have to call this better my place to construct explicitly because implicitly a child class
can only call the default constructor of its parent class.
Okay.
So if you will run this file must be let us on this file so we are getting an error and let's see that.
What is that it so the error is so bad it is Truck Bus and God so we are getting an error in all the
three classes got bus and wave struck.
So this is truck.
Okay so we are getting an error in all the three classes.
So what is happening here is so in the last three videos I told you that a class can only call the constructor
of its immediate parent.
But here the bus class is calling construct of its grandparent.
Okay so this is a special case.
Okay.
I'm repeating myself in their libidos I told you that a class can only call the concept of profits immediate
parent.
But here the bus glass is quoting the construct proffered grandparent.
Okay but only in this case because we are using the virtual keyword.
Okay.
We are making the weekend class as virtual base class.
Okay so only in this case class can call the constructor of its grandparent.
Okay so this type of inheritance is also known as what actually inheritance.
Okay.
So this type of an identity also known as virtual inheritance.
Now let's solve this problem.
So how can we solve this problem since we do not have any default constructor.
We have to call it bottom at best constructor explicitly so class got so with the help of initialization
list we can let's say three okay.
So let's spend the value of Z here
so I am bringing the value of Z also.
Okay.
Now I am bringing the value of Z.
You will understand in some time so.
Truck
the call and let's say the value is for we are calling the paramedics construct.
Okay.
And similarly we have to do for bus also because bus also has the object of because class.
Okay.
This is a special case we had a we had a class can call the concept of its grandparent Okay and let's
say the value is
and let's say the value is five.
Okay.
So value 3.
Value for value 4 and value 5.
Okay.
And I am bringing the value of Z here Okay let's send this file
so you can see what is the value getting printed 5.
This is the proof variable used 5.
Here okay.
So here we are using 5.
So this is the proof.
Okay.
So we have proved that bus glasses is calling the victim class constructor and not the car in the truck.
Okay so I hope now you understand where this diamond problem and how we can use virtual keyword and
how to resolve ambiguity and how to avoid copy.
Okay.
Thank you.