Let's actually think of our project that we have another class that is derived from the animal class.
For instance, Cat, we are going to make a function that will invoke the get name and make some the
methods for both dog and cat instances.
So in this case, we're going to also add the cat here.
And without creating the two separate functions, we can use the templates which we're going to start
it doing it now.
My name is Stefan and let's get started our lecture.
So we firstly are going to create, as you can see, this is a protected.
Now we have a public constructors.
After that, we have the dog public animal class.
And we will also create a cat public animal, as we said in previous lecture and uh, earlier of this
lecture.
So here we're going to create a class class cat and it's going to be public animal.
Which is going to be a public public format here.
So inside it, we're going to forward the constructor arguments from cat or from animal to the cat here.
So public the.
Here.
A cat is going to be string name and the animal is going to be name here.
And after that, we're going to add here.
So now we're going to copy assignment operator overloading as we did here in Doc here.
Void operator And let's actually, we will do it like that.
The void operator here, the const cat instead of doc we're going to write it Cat here and Operator
D here.
And the my name is going to be d.my name here.
So now we also need to implement interface.
So the interface is going to be like the void.
No, the interface is going to be a string.
Of course it's going to be the make sound here.
Make sound over it, over.
Read and return The of course, Cat is not saying woof, woof here.
Instead, it's like saying something.
Mew Mew here.
So here.
This mew mew.
Here.
So now it's going to be we are getting to the template parts here.
So after that, we will create a template.
Template Typename.
Name T.
And after that.
We will create something like that.
The function that will get name and make sound.
So.
The void here.
Boyd get name and make sound here.
The key here is the animal.
And see out here.
Now, we need to also add braces here so see out the animal dot getName.
getName.
And that's going to be.
Go here and see out the animal that make sound.
It's going to be in line here because we're going to need the new line after that and after that.
We, as you can see in this main here, we will also need to add cat.
So cat, cat here.
The cat is going to be, for example, the regular cat.
Yeah, regular cat.
And we will use the get name and make son, which is going to pass the cat here like this.
Now we're after that.
We have the return and.
Here.
We have done our code.
So in this code, we can see that we can pass both the dog and cat data types to the get name and make
some function.
Since we have defined the type name here.
Type name template type name is t here before the function definition.
So the type name is a keyword in c plus plus which use the write template.
So the keyword is used for specifying that a symbol in a template definition or declaration is a type.
So in this example, the symbol here is T.
As a result, the function becomes generic and can accept various data types.
And if we build and run this code, we will see something like this.
Here is the G++ Main.cpp.
And adding.here.
Oops, we had a problem with it.
Yeah.
Output here we need to output.
Yeah.
And let's actually run adding cat dot x.
And as you can see here, terrier is barking.
Woof, woof.
Terrier is barking.
Woof, woof.
And regular cat goes mew Mew here.
So you will see something like that.
So please, please ensure that the data type we pass to the generic function has the ability to do all
the operation invoking from the generic function.
However, the compiler will compile if the data type was we pass does not have the except operation.
So in this function template example we need to pass a data type that is an instance of the animal class
so we can pass either instance of the animal class or an instance of a derived class of the animal class
here.