WEBVTT

00:00.620 --> 00:07.130
Let's actually think of our project that we have another class that is derived from the animal class.

00:07.160 --> 00:15.200
For instance, Cat, we are going to make a function that will invoke the get name and make some the

00:15.200 --> 00:18.440
methods for both dog and cat instances.

00:18.440 --> 00:21.680
So in this case, we're going to also add the cat here.

00:22.940 --> 00:28.760
And without creating the two separate functions, we can use the templates which we're going to start

00:28.760 --> 00:30.570
it doing it now.

00:30.590 --> 00:33.620
My name is Stefan and let's get started our lecture.

00:45.660 --> 00:50.760
So we firstly are going to create, as you can see, this is a protected.

00:50.790 --> 00:54.240
Now we have a public constructors.

00:56.320 --> 00:59.830
After that, we have the dog public animal class.

01:01.360 --> 01:08.230
And we will also create a cat public animal, as we said in previous lecture and uh, earlier of this

01:08.230 --> 01:08.770
lecture.

01:08.770 --> 01:16.660
So here we're going to create a class class cat and it's going to be public animal.

01:18.060 --> 01:22.860
Which is going to be a public public format here.

01:23.250 --> 01:31.310
So inside it, we're going to forward the constructor arguments from cat or from animal to the cat here.

01:31.320 --> 01:35.040
So public the.

01:36.400 --> 01:37.210
Here.

01:38.810 --> 01:47.480
A cat is going to be string name and the animal is going to be name here.

01:49.450 --> 01:52.300
And after that, we're going to add here.

01:53.550 --> 02:00.400
So now we're going to copy assignment operator overloading as we did here in Doc here.

02:00.420 --> 02:04.680
Void operator And let's actually, we will do it like that.

02:04.710 --> 02:15.150
The void operator here, the const cat instead of doc we're going to write it Cat here and Operator

02:15.180 --> 02:16.710
D here.

02:16.710 --> 02:24.360
And the my name is going to be d.my name here.

02:25.640 --> 02:29.350
So now we also need to implement interface.

02:29.360 --> 02:33.410
So the interface is going to be like the void.

02:33.440 --> 02:35.580
No, the interface is going to be a string.

02:35.600 --> 02:38.660
Of course it's going to be the make sound here.

02:38.660 --> 02:41.590
Make sound over it, over.

02:41.590 --> 02:50.000
Read and return The of course, Cat is not saying woof, woof here.

02:50.000 --> 02:51.720
Instead, it's like saying something.

02:51.740 --> 02:53.120
Mew Mew here.

02:53.120 --> 02:55.280
So here.

02:55.280 --> 02:58.160
This mew mew.

03:02.160 --> 03:02.670
Here.

03:02.670 --> 03:07.200
So now it's going to be we are getting to the template parts here.

03:07.440 --> 03:10.380
So after that, we will create a template.

03:12.600 --> 03:15.270
Template Typename.

03:15.270 --> 03:16.110
Name T.

03:19.440 --> 03:20.670
And after that.

03:22.690 --> 03:25.200
We will create something like that.

03:25.280 --> 03:30.280
The function that will get name and make sound.

03:30.850 --> 03:31.810
So.

03:33.170 --> 03:34.400
The void here.

03:35.410 --> 03:41.230
Boyd get name and make sound here.

03:41.500 --> 03:45.790
The key here is the animal.

03:49.490 --> 03:52.250
And see out here.

03:52.340 --> 03:58.820
Now, we need to also add braces here so see out the animal dot getName.

03:59.580 --> 04:00.800
getName.

04:02.940 --> 04:04.470
And that's going to be.

04:05.920 --> 04:13.960
Go here and see out the animal that make sound.

04:14.050 --> 04:20.500
It's going to be in line here because we're going to need the new line after that and after that.

04:20.500 --> 04:26.140
We, as you can see in this main here, we will also need to add cat.

04:26.170 --> 04:29.350
So cat, cat here.

04:31.280 --> 04:35.960
The cat is going to be, for example, the regular cat.

04:35.990 --> 04:39.050
Yeah, regular cat.

04:39.500 --> 04:47.240
And we will use the get name and make son, which is going to pass the cat here like this.

04:48.200 --> 04:49.220
Now we're after that.

04:49.220 --> 04:51.830
We have the return and.

04:52.730 --> 04:53.330
Here.

04:53.330 --> 04:54.380
We have done our code.

04:54.410 --> 05:04.340
So in this code, we can see that we can pass both the dog and cat data types to the get name and make

05:04.340 --> 05:05.390
some function.

05:05.390 --> 05:08.960
Since we have defined the type name here.

05:08.990 --> 05:16.730
Type name template type name is t here before the function definition.

05:16.970 --> 05:23.000
So the type name is a keyword in c plus plus which use the write template.

05:24.180 --> 05:31.350
So the keyword is used for specifying that a symbol in a template definition or declaration is a type.

05:31.350 --> 05:34.740
So in this example, the symbol here is T.

05:35.280 --> 05:41.400
As a result, the function becomes generic and can accept various data types.

05:41.400 --> 05:45.630
And if we build and run this code, we will see something like this.

05:45.630 --> 05:51.150
Here is the G++ Main.cpp.

05:52.630 --> 05:55.920
And adding.here.

05:55.960 --> 05:58.180
Oops, we had a problem with it.

05:58.450 --> 05:59.230
Yeah.

05:59.740 --> 06:02.070
Output here we need to output.

06:02.080 --> 06:02.500
Yeah.

06:02.980 --> 06:06.730
And let's actually run adding cat dot x.

06:06.970 --> 06:10.030
And as you can see here, terrier is barking.

06:10.030 --> 06:10.560
Woof, woof.

06:10.600 --> 06:11.710
Terrier is barking.

06:11.710 --> 06:12.220
Woof, woof.

06:12.220 --> 06:14.890
And regular cat goes mew Mew here.

06:15.400 --> 06:18.560
So you will see something like that.

06:18.580 --> 06:25.720
So please, please ensure that the data type we pass to the generic function has the ability to do all

06:25.720 --> 06:28.100
the operation invoking from the generic function.

06:28.120 --> 06:35.950
However, the compiler will compile if the data type was we pass does not have the except operation.

06:35.950 --> 06:44.080
So in this function template example we need to pass a data type that is an instance of the animal class

06:44.080 --> 06:49.600
so we can pass either instance of the animal class or an instance of a derived class of the animal class

06:49.600 --> 06:50.050
here.
