WEBVTT

00:00.900 --> 00:02.870
Similar to the function template.

00:02.880 --> 00:08.640
The class template is used to build a generic class that can accept various datatypes.

00:08.640 --> 00:11.550
So let's refactor our function template.

00:11.550 --> 00:17.070
So in this case it's the Main.cpp code by adding a new class template.

00:17.100 --> 00:20.310
My name is Stephan and let's get started with our lecture.

00:33.390 --> 00:42.510
So here now we have class animal the protected here actually let's create a new class and delete this

00:42.510 --> 00:46.170
old X files here.

00:47.710 --> 00:56.470
And right click on this C plus plus class only the class here and class template here.

01:01.120 --> 01:04.960
So this is actually it's not not here.

01:05.830 --> 01:08.020
So let's get started.

01:08.020 --> 01:17.350
So we're going to firstly need to include the stream, of course, include the stream.

01:21.490 --> 01:25.060
Dream here and we will use the namespace STD.

01:25.420 --> 01:29.680
So first we're going to write the create class animal.

01:29.710 --> 01:31.420
Class animal.

01:31.420 --> 01:33.700
And it's going to be the protected.

01:34.390 --> 01:37.450
This string here, my name.

01:38.260 --> 01:41.620
And after that we will make a public constructor.

01:42.640 --> 01:43.690
Here are protected.

01:43.690 --> 01:47.020
My name and here.

01:48.220 --> 01:50.080
It will also create the public.

01:51.580 --> 01:58.900
Public and the public constructor is going to get the animal as a string, as a parameter string, my

01:58.900 --> 02:06.340
name or name here, just name and we will pass that to my name and as the name here.

02:07.000 --> 02:11.050
So we will get this from here and put it here.

02:12.350 --> 02:15.800
After that, this will going to be empty for now.

02:16.700 --> 02:21.350
And we need to the interface that has to implement in derived class.

02:21.350 --> 02:27.560
So in this case we're going to create a virtual string here and make sound.

02:28.510 --> 02:32.530
Here is going to null or zero here instead of null.

02:32.740 --> 02:35.560
And now we will also create a string getName.

02:35.860 --> 02:39.340
So here in this case, this is a setter.

02:40.770 --> 02:42.630
And we will also create a getter.

02:42.630 --> 02:46.170
So we created setter inside the constructor here.

02:46.170 --> 02:56.220
So string get name and the this is going to be return just my name here, which in this case the my

02:56.220 --> 02:58.590
name is this.

02:59.180 --> 03:06.110
So this will return the my name here and we are done with the animal class.

03:06.110 --> 03:16.490
So now create the dog class here that inherits from the animal class here, class dog here and public

03:17.000 --> 03:17.780
animal.

03:20.390 --> 03:25.250
And we're going to make the public constructor public.

03:26.150 --> 03:29.270
Here we will forward the constructor arguments.

03:29.630 --> 03:30.650
Doc here.

03:30.950 --> 03:34.400
String name and here.

03:34.400 --> 03:36.260
Animal name.

03:37.990 --> 03:38.830
Here.

03:39.430 --> 03:44.470
So we will get the dog here and put it in the animal here.

03:44.470 --> 03:47.170
And this is going to be right this.

03:48.450 --> 03:49.680
And after that.

03:49.770 --> 03:52.380
So we forwarded the constructor arguments.

03:52.410 --> 03:55.460
Now we need to copy assignment operator overloading.

03:55.470 --> 03:58.680
So in order to do that after the public.

03:59.940 --> 04:00.420
Uh, the.

04:00.420 --> 04:02.790
We're gonna create the void operator.

04:03.030 --> 04:05.010
Void operator.

04:05.010 --> 04:12.840
And it's going to be a constant dog here and operator Dog here and.

04:12.840 --> 04:16.080
Operator And the or instead dog here.

04:16.080 --> 04:17.100
Let's make it the.

04:19.320 --> 04:21.720
Uh, so we created the void operator here.

04:21.720 --> 04:26.010
And yeah, we also need to assign it to something here.

04:26.010 --> 04:28.570
So we need to name my name.

04:28.590 --> 04:34.260
It's equals d.my name here, as you can see here.

04:34.710 --> 04:37.740
And after that we need to implement the interface.

04:37.740 --> 04:47.010
So in this case it's going to be the Makesound and we will override this Makesound here and we will

04:47.010 --> 04:48.300
implement the interface.

04:48.300 --> 04:50.730
So this is the interface actually not here.

04:50.940 --> 04:57.670
String, make sound, make sound here and we will override.

04:58.910 --> 05:01.400
And here return here.

05:01.400 --> 05:03.870
In this case, the return is woof, woof.

05:03.950 --> 05:04.340
Here.

05:06.070 --> 05:06.490
Narrator.

05:07.420 --> 05:08.740
NARRATOR Heading level one.

05:08.770 --> 05:09.250
Exiting.

05:09.250 --> 05:09.880
Narrator.

05:10.840 --> 05:11.380
Here.

05:12.640 --> 05:13.780
Uh, now we're going to.

05:14.990 --> 05:15.320
Here.

05:15.320 --> 05:16.820
We have done with it.

05:16.850 --> 05:22.880
Void operator is yes here now we have done with the dog class.

05:22.880 --> 05:26.610
Now we need to also create the cat class.

05:26.660 --> 05:32.870
So in this case, we're going to create a class cat here and it's going to be public.

05:33.800 --> 05:35.510
We will get the animal here.

05:36.320 --> 05:43.700
So the public is going to be we will also forward the constructor arguments, as we did earlier with

05:43.700 --> 05:45.560
the dog example here.

05:45.860 --> 05:48.170
So now we're going to Cat.

05:49.440 --> 05:52.680
String name and animal.

05:53.370 --> 05:55.470
Animal name here.

05:59.570 --> 06:03.920
After that, we need to also do the operator here, copy assignment here.

06:04.760 --> 06:06.320
So in this case, it's going to be.

06:08.110 --> 06:09.160
Void Operator.

06:09.190 --> 06:10.740
Void operator.

06:11.120 --> 06:14.260
Operator equals.

06:20.710 --> 06:22.360
Equals the const.

06:23.300 --> 06:24.410
Constant.

06:27.380 --> 06:29.180
The cat and the.

06:30.810 --> 06:31.710
Here.

06:31.740 --> 06:41.550
We will also need to add braces and we will assign my name to the my name here that we have inside the.my

06:41.550 --> 06:41.850
name.

06:42.510 --> 06:48.780
And now we need to implement the interface here that we created the Makesound interface.

06:49.260 --> 06:50.250
So.

06:51.460 --> 06:52.090
Here.

06:52.090 --> 06:55.930
The string here string makes sound.

06:57.770 --> 06:59.590
Here and over it.

07:01.900 --> 07:02.740
Return.

07:03.010 --> 07:05.950
What the cat makes the mew mew hear.

07:06.130 --> 07:06.610
Yeah.

07:06.640 --> 07:09.700
Mew Mew here.

07:10.060 --> 07:12.520
And we will also create a template here.

07:13.990 --> 07:16.570
The temp actually spiked here.

07:16.570 --> 07:20.110
So we need to create a template after this.

07:21.170 --> 07:26.090
So it's going to be like, uh, the class We created three class here.

07:33.920 --> 07:35.660
Make sound here.

07:43.020 --> 07:45.480
Here, make this look nice.

07:45.750 --> 07:48.660
And after that, we will create a template, as I said.

07:49.410 --> 07:52.290
Pamphlet is going to be the type name.

07:53.400 --> 07:53.820
He.

07:55.560 --> 07:55.830
Here.

07:55.830 --> 07:57.930
And we will also create a new void.

07:58.110 --> 08:03.690
We will, as we did in previous lecture, we will create a new void named getName and make.

08:04.380 --> 08:04.800
Here.

08:04.800 --> 08:06.390
Actually, we don't need the.

08:07.270 --> 08:07.930
Here.

08:08.020 --> 08:20.470
So the get name and make sound here, it's going to be the T or T and operator here and we're going

08:20.470 --> 08:22.060
to make it the animal.

08:22.890 --> 08:25.290
Animal or just an.

08:27.210 --> 08:28.290
Our animal.

08:29.070 --> 08:29.810
Yeah.

08:29.820 --> 08:31.190
The animal is okay.

08:31.200 --> 08:33.030
The animal.

08:34.440 --> 08:35.760
And here.

08:39.260 --> 08:39.800
The animal.

08:39.800 --> 08:44.720
We will see out the animal Dot get name.

08:45.610 --> 08:48.520
And goes here.

08:49.840 --> 08:54.220
See out the animal that makes sound.

08:54.250 --> 08:55.320
Makes sound.

08:55.720 --> 08:58.750
Is the get name and make.

08:58.750 --> 09:04.480
So we will also need the get name and make sound here and line.

09:04.480 --> 09:10.340
And after that we will also create a new template here that of course the type name is going to be also

09:10.410 --> 09:14.020
T here template.

09:15.340 --> 09:16.720
Template.

09:16.720 --> 09:17.890
The type name.

09:17.920 --> 09:20.830
Type name is going to be here.

09:21.400 --> 09:22.750
After that.

09:26.770 --> 09:32.530
And we will also create an animal template class animal template.

09:33.430 --> 09:42.190
Here we will first assign private that T animal or T my animal.

09:42.910 --> 09:50.590
And after that we will also create something public that we will make the constructor here, the private

09:50.590 --> 09:51.880
public here.

09:55.230 --> 09:56.060
T my animal.

09:56.250 --> 09:57.510
The public is going to be.

09:58.530 --> 10:02.280
Uh, the animal template public is going to be animal.

10:03.190 --> 10:04.300
Animal testing.

10:04.330 --> 10:07.210
This will get t dot animal.

10:07.660 --> 10:11.560
And here we will pass this to my animal.

10:11.590 --> 10:13.960
Here, my animal.

10:13.960 --> 10:14.860
And we will.

10:16.420 --> 10:16.810
Yeah.

10:18.590 --> 10:19.130
Here.

10:19.670 --> 10:30.140
We will pass the animal here to this oops, this animal to here, as we did in the earlier animal here.

10:31.510 --> 10:32.710
And after that.

10:34.380 --> 10:34.710
Here.

10:34.710 --> 10:37.350
We will open and close the curly braces.

10:39.500 --> 10:44.450
After that, we will create a getName and make sound here.

10:44.540 --> 10:45.980
So void.

10:47.990 --> 10:51.680
Get name and make sound here.

10:54.850 --> 10:59.110
The crowd is going to be not the animal in this case.

10:59.110 --> 11:03.970
It's going to be my animal, my animal dot get name.

11:05.900 --> 11:07.380
Yeah, they get name.

11:10.280 --> 11:15.110
Yes yeah my animal dot get name and.

11:17.190 --> 11:18.000
Glacier.

11:19.630 --> 11:20.770
Our animal name.

11:20.770 --> 11:29.530
It goes like, see out my animal, but make sound.

11:31.610 --> 11:33.020
Here and in line.

11:33.020 --> 11:36.620
So first we will print the our animal name.

11:38.450 --> 11:43.040
So animal in this case, the dog here, dog goes here.

11:43.460 --> 11:44.240
Woof, woof here.

11:44.240 --> 11:45.800
And then we will end the line.

11:45.800 --> 11:49.550
And this is the new we will click new line for us.

11:50.000 --> 11:53.810
And now let's start creating our main function.

11:53.810 --> 11:55.220
But first, let's check the lecture time.

11:55.220 --> 11:55.790
Yes.

11:55.850 --> 11:58.880
So let's create our main function now.

12:00.540 --> 12:03.090
After the closing the animal template.

12:03.120 --> 12:06.900
Now we will clear the integer main integer main function.

12:07.050 --> 12:09.600
And this going to be the dog here.

12:09.630 --> 12:10.140
Dog.

12:10.140 --> 12:11.220
It's dog.

12:11.220 --> 12:19.770
In this case, let's actually make this Labrador here and animal template template.

12:21.580 --> 12:23.680
Uh, dog is going to be.

12:25.210 --> 12:27.100
Bulk is going to be a doc template.

12:27.130 --> 12:28.870
Doc Template.

12:28.990 --> 12:29.770
Doc.

12:33.060 --> 12:35.130
Now the dog template will.

12:35.980 --> 12:38.230
Look template dot get.

12:39.860 --> 12:40.760
Get.

12:41.790 --> 12:46.020
And get name and make sound here so get.

12:46.840 --> 12:47.700
Don't talk to me.

12:47.710 --> 12:54.220
Get name and make sound after will also apply the same for the.

12:55.200 --> 12:56.280
Kat Von here.

12:56.280 --> 12:57.510
So, Kat.

12:57.540 --> 12:58.560
Kat here.

12:59.010 --> 13:00.090
New Kat Instance.

13:00.090 --> 13:00.480
Kat.

13:00.510 --> 13:01.170
Kat.

13:01.200 --> 13:01.740
Kat.

13:01.740 --> 13:02.240
Here in this.

13:02.520 --> 13:03.450
Estonian.

13:03.570 --> 13:04.890
Estonian.

13:05.040 --> 13:05.880
Kat.

13:06.910 --> 13:10.360
Here and this is going to be an animal template.

13:11.080 --> 13:12.490
The cat.

13:13.930 --> 13:16.300
Is going to be make this cat template.

13:16.310 --> 13:20.380
Template here and cat will pass the cat here.

13:21.490 --> 13:25.930
Now the cat template dot get name and make sense.

13:25.960 --> 13:27.280
We will use this function.

13:27.400 --> 13:34.690
So as you can see in this code, we have a new class named the Animal template.

13:36.090 --> 13:36.480
Here.

13:38.340 --> 13:43.290
So it's a template class and it can be used by any data types.

13:43.290 --> 13:47.640
However, we have to define the data type and in the angle braces here.

13:47.640 --> 13:54.870
So as we can see in this code, when we use the instances, the doc template and the cat template here,

13:54.900 --> 14:02.760
the doc template and cat template here, um, they use this instances.

14:04.290 --> 14:06.380
And we will use the angle braces here.

14:06.390 --> 14:12.030
So if you build and run the code, we will get the output as we did in previous lectures.

14:12.030 --> 14:15.860
But in this case we use the classes to make this real.

14:15.870 --> 14:18.150
So now we're going to open this.

14:18.150 --> 14:20.790
Actually, let me open the CMD here.

14:21.150 --> 14:21.780
Here.

14:21.780 --> 14:23.700
Let's actually look what we have here.

14:23.700 --> 14:27.270
And this is our CPP file.

14:28.980 --> 14:31.140
And this is our main.cpp file.

14:31.140 --> 14:41.180
So here this class template dot cpp file is this the newly created and the main is this main.cpp here.

14:41.190 --> 14:48.990
So now we just going to compile the class template dot cpp g++ class template dot cpp here.

14:48.990 --> 14:52.500
And as you can see here, it compiled without an error.

14:52.500 --> 14:57.180
And there here and as you can see here, we just create a new file here.

14:58.230 --> 14:59.980
So let's actually run this.

15:00.000 --> 15:06.960
And as you can see here, the Labrador goes woof, woof and Estonian cause cat goes moo moo here.

15:06.960 --> 15:10.440
So that was our example with the class template.

15:10.440 --> 15:11.970
So I'm waiting you in next lecture.
