Similar to the function template.
The class template is used to build a generic class that can accept various datatypes.
So let's refactor our function template.
So in this case it's the Main.cpp code by adding a new class template.
My name is Stephan and let's get started with our lecture.
So here now we have class animal the protected here actually let's create a new class and delete this
old X files here.
And right click on this C plus plus class only the class here and class template here.
So this is actually it's not not here.
So let's get started.
So we're going to firstly need to include the stream, of course, include the stream.
Dream here and we will use the namespace STD.
So first we're going to write the create class animal.
Class animal.
And it's going to be the protected.
This string here, my name.
And after that we will make a public constructor.
Here are protected.
My name and here.
It will also create the public.
Public and the public constructor is going to get the animal as a string, as a parameter string, my
name or name here, just name and we will pass that to my name and as the name here.
So we will get this from here and put it here.
After that, this will going to be empty for now.
And we need to the interface that has to implement in derived class.
So in this case we're going to create a virtual string here and make sound.
Here is going to null or zero here instead of null.
And now we will also create a string getName.
So here in this case, this is a setter.
And we will also create a getter.
So we created setter inside the constructor here.
So string get name and the this is going to be return just my name here, which in this case the my
name is this.
So this will return the my name here and we are done with the animal class.
So now create the dog class here that inherits from the animal class here, class dog here and public
animal.
And we're going to make the public constructor public.
Here we will forward the constructor arguments.
Doc here.
String name and here.
Animal name.
Here.
So we will get the dog here and put it in the animal here.
And this is going to be right this.
And after that.
So we forwarded the constructor arguments.
Now we need to copy assignment operator overloading.
So in order to do that after the public.
Uh, the.
We're gonna create the void operator.
Void operator.
And it's going to be a constant dog here and operator Dog here and.
Operator And the or instead dog here.
Let's make it the.
Uh, so we created the void operator here.
And yeah, we also need to assign it to something here.
So we need to name my name.
It's equals d.my name here, as you can see here.
And after that we need to implement the interface.
So in this case it's going to be the Makesound and we will override this Makesound here and we will
implement the interface.
So this is the interface actually not here.
String, make sound, make sound here and we will override.
And here return here.
In this case, the return is woof, woof.
Here.
Narrator.
NARRATOR Heading level one.
Exiting.
Narrator.
Here.
Uh, now we're going to.
Here.
We have done with it.
Void operator is yes here now we have done with the dog class.
Now we need to also create the cat class.
So in this case, we're going to create a class cat here and it's going to be public.
We will get the animal here.
So the public is going to be we will also forward the constructor arguments, as we did earlier with
the dog example here.
So now we're going to Cat.
String name and animal.
Animal name here.
After that, we need to also do the operator here, copy assignment here.
So in this case, it's going to be.
Void Operator.
Void operator.
Operator equals.
Equals the const.
Constant.
The cat and the.
Here.
We will also need to add braces and we will assign my name to the my name here that we have inside the.my
name.
And now we need to implement the interface here that we created the Makesound interface.
So.
Here.
The string here string makes sound.
Here and over it.
Return.
What the cat makes the mew mew hear.
Yeah.
Mew Mew here.
And we will also create a template here.
The temp actually spiked here.
So we need to create a template after this.
So it's going to be like, uh, the class We created three class here.
Make sound here.
Here, make this look nice.
And after that, we will create a template, as I said.
Pamphlet is going to be the type name.
He.
Here.
And we will also create a new void.
We will, as we did in previous lecture, we will create a new void named getName and make.
Here.
Actually, we don't need the.
Here.
So the get name and make sound here, it's going to be the T or T and operator here and we're going
to make it the animal.
Animal or just an.
Our animal.
Yeah.
The animal is okay.
The animal.
And here.
The animal.
We will see out the animal Dot get name.
And goes here.
See out the animal that makes sound.
Makes sound.
Is the get name and make.
So we will also need the get name and make sound here and line.
And after that we will also create a new template here that of course the type name is going to be also
T here template.
Template.
The type name.
Type name is going to be here.
After that.
And we will also create an animal template class animal template.
Here we will first assign private that T animal or T my animal.
And after that we will also create something public that we will make the constructor here, the private
public here.
T my animal.
The public is going to be.
Uh, the animal template public is going to be animal.
Animal testing.
This will get t dot animal.
And here we will pass this to my animal.
Here, my animal.
And we will.
Yeah.
Here.
We will pass the animal here to this oops, this animal to here, as we did in the earlier animal here.
And after that.
Here.
We will open and close the curly braces.
After that, we will create a getName and make sound here.
So void.
Get name and make sound here.
The crowd is going to be not the animal in this case.
It's going to be my animal, my animal dot get name.
Yeah, they get name.
Yes yeah my animal dot get name and.
Glacier.
Our animal name.
It goes like, see out my animal, but make sound.
Here and in line.
So first we will print the our animal name.
So animal in this case, the dog here, dog goes here.
Woof, woof here.
And then we will end the line.
And this is the new we will click new line for us.
And now let's start creating our main function.
But first, let's check the lecture time.
Yes.
So let's create our main function now.
After the closing the animal template.
Now we will clear the integer main integer main function.
And this going to be the dog here.
Dog.
It's dog.
In this case, let's actually make this Labrador here and animal template template.
Uh, dog is going to be.
Bulk is going to be a doc template.
Doc Template.
Doc.
Now the dog template will.
Look template dot get.
Get.
And get name and make sound here so get.
Don't talk to me.
Get name and make sound after will also apply the same for the.
Kat Von here.
So, Kat.
Kat here.
New Kat Instance.
Kat.
Here in this.
Estonian.
Kat.
Here and this is going to be an animal template.
The cat.
Is going to be make this cat template.
Template here and cat will pass the cat here.
Now the cat template dot get name and make sense.
We will use this function.
So as you can see in this code, we have a new class named the Animal template.
Here.
So it's a template class and it can be used by any data types.
However, we have to define the data type and in the angle braces here.
So as we can see in this code, when we use the instances, the doc template and the cat template here,
the doc template and cat template here, um, they use this instances.
And we will use the angle braces here.
So if you build and run the code, we will get the output as we did in previous lectures.
But in this case we use the classes to make this real.
So now we're going to open this.
Actually, let me open the CMD here.
Here.
Let's actually look what we have here.
And this is our CPP file.
And this is our main.cpp file.
So here this class template dot cpp file is this the newly created and the main is this main.cpp here.
So now we just going to compile the class template dot cpp g++ class template dot cpp here.
And as you can see here, it compiled without an error.
And there here and as you can see here, we just create a new file here.
So let's actually run this.
And as you can see here, the Labrador goes woof, woof and Estonian cause cat goes moo moo here.
So that was our example with the class template.
So I'm waiting you in next lecture.