1 00:00:00,900 --> 00:00:02,870 Similar to the function template. 2 00:00:02,880 --> 00:00:08,640 The class template is used to build a generic class that can accept various datatypes. 3 00:00:08,640 --> 00:00:11,550 So let's refactor our function template. 4 00:00:11,550 --> 00:00:17,070 So in this case it's the Main.cpp code by adding a new class template. 5 00:00:17,100 --> 00:00:20,310 My name is Stephan and let's get started with our lecture. 6 00:00:33,390 --> 00:00:42,510 So here now we have class animal the protected here actually let's create a new class and delete this 7 00:00:42,510 --> 00:00:46,170 old X files here. 8 00:00:47,710 --> 00:00:56,470 And right click on this C plus plus class only the class here and class template here. 9 00:01:01,120 --> 00:01:04,960 So this is actually it's not not here. 10 00:01:05,830 --> 00:01:08,020 So let's get started. 11 00:01:08,020 --> 00:01:17,350 So we're going to firstly need to include the stream, of course, include the stream. 12 00:01:21,490 --> 00:01:25,060 Dream here and we will use the namespace STD. 13 00:01:25,420 --> 00:01:29,680 So first we're going to write the create class animal. 14 00:01:29,710 --> 00:01:31,420 Class animal. 15 00:01:31,420 --> 00:01:33,700 And it's going to be the protected. 16 00:01:34,390 --> 00:01:37,450 This string here, my name. 17 00:01:38,260 --> 00:01:41,620 And after that we will make a public constructor. 18 00:01:42,640 --> 00:01:43,690 Here are protected. 19 00:01:43,690 --> 00:01:47,020 My name and here. 20 00:01:48,220 --> 00:01:50,080 It will also create the public. 21 00:01:51,580 --> 00:01:58,900 Public and the public constructor is going to get the animal as a string, as a parameter string, my 22 00:01:58,900 --> 00:02:06,340 name or name here, just name and we will pass that to my name and as the name here. 23 00:02:07,000 --> 00:02:11,050 So we will get this from here and put it here. 24 00:02:12,350 --> 00:02:15,800 After that, this will going to be empty for now. 25 00:02:16,700 --> 00:02:21,350 And we need to the interface that has to implement in derived class. 26 00:02:21,350 --> 00:02:27,560 So in this case we're going to create a virtual string here and make sound. 27 00:02:28,510 --> 00:02:32,530 Here is going to null or zero here instead of null. 28 00:02:32,740 --> 00:02:35,560 And now we will also create a string getName. 29 00:02:35,860 --> 00:02:39,340 So here in this case, this is a setter. 30 00:02:40,770 --> 00:02:42,630 And we will also create a getter. 31 00:02:42,630 --> 00:02:46,170 So we created setter inside the constructor here. 32 00:02:46,170 --> 00: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 33 00:02:56,220 --> 00:02:58,590 name is this. 34 00:02:59,180 --> 00:03:06,110 So this will return the my name here and we are done with the animal class. 35 00:03:06,110 --> 00:03:16,490 So now create the dog class here that inherits from the animal class here, class dog here and public 36 00:03:17,000 --> 00:03:17,780 animal. 37 00:03:20,390 --> 00:03:25,250 And we're going to make the public constructor public. 38 00:03:26,150 --> 00:03:29,270 Here we will forward the constructor arguments. 39 00:03:29,630 --> 00:03:30,650 Doc here. 40 00:03:30,950 --> 00:03:34,400 String name and here. 41 00:03:34,400 --> 00:03:36,260 Animal name. 42 00:03:37,990 --> 00:03:38,830 Here. 43 00:03:39,430 --> 00:03:44,470 So we will get the dog here and put it in the animal here. 44 00:03:44,470 --> 00:03:47,170 And this is going to be right this. 45 00:03:48,450 --> 00:03:49,680 And after that. 46 00:03:49,770 --> 00:03:52,380 So we forwarded the constructor arguments. 47 00:03:52,410 --> 00:03:55,460 Now we need to copy assignment operator overloading. 48 00:03:55,470 --> 00:03:58,680 So in order to do that after the public. 49 00:03:59,940 --> 00:04:00,420 Uh, the. 50 00:04:00,420 --> 00:04:02,790 We're gonna create the void operator. 51 00:04:03,030 --> 00:04:05,010 Void operator. 52 00:04:05,010 --> 00:04:12,840 And it's going to be a constant dog here and operator Dog here and. 53 00:04:12,840 --> 00:04:16,080 Operator And the or instead dog here. 54 00:04:16,080 --> 00:04:17,100 Let's make it the. 55 00:04:19,320 --> 00:04:21,720 Uh, so we created the void operator here. 56 00:04:21,720 --> 00:04:26,010 And yeah, we also need to assign it to something here. 57 00:04:26,010 --> 00:04:28,570 So we need to name my name. 58 00:04:28,590 --> 00:04:34,260 It's equals d.my name here, as you can see here. 59 00:04:34,710 --> 00:04:37,740 And after that we need to implement the interface. 60 00:04:37,740 --> 00:04:47,010 So in this case it's going to be the Makesound and we will override this Makesound here and we will 61 00:04:47,010 --> 00:04:48,300 implement the interface. 62 00:04:48,300 --> 00:04:50,730 So this is the interface actually not here. 63 00:04:50,940 --> 00:04:57,670 String, make sound, make sound here and we will override. 64 00:04:58,910 --> 00:05:01,400 And here return here. 65 00:05:01,400 --> 00:05:03,870 In this case, the return is woof, woof. 66 00:05:03,950 --> 00:05:04,340 Here. 67 00:05:06,070 --> 00:05:06,490 Narrator. 68 00:05:07,420 --> 00:05:08,740 NARRATOR Heading level one. 69 00:05:08,770 --> 00:05:09,250 Exiting. 70 00:05:09,250 --> 00:05:09,880 Narrator. 71 00:05:10,840 --> 00:05:11,380 Here. 72 00:05:12,640 --> 00:05:13,780 Uh, now we're going to. 73 00:05:14,990 --> 00:05:15,320 Here. 74 00:05:15,320 --> 00:05:16,820 We have done with it. 75 00:05:16,850 --> 00:05:22,880 Void operator is yes here now we have done with the dog class. 76 00:05:22,880 --> 00:05:26,610 Now we need to also create the cat class. 77 00:05:26,660 --> 00:05:32,870 So in this case, we're going to create a class cat here and it's going to be public. 78 00:05:33,800 --> 00:05:35,510 We will get the animal here. 79 00:05:36,320 --> 00:05:43,700 So the public is going to be we will also forward the constructor arguments, as we did earlier with 80 00:05:43,700 --> 00:05:45,560 the dog example here. 81 00:05:45,860 --> 00:05:48,170 So now we're going to Cat. 82 00:05:49,440 --> 00:05:52,680 String name and animal. 83 00:05:53,370 --> 00:05:55,470 Animal name here. 84 00:05:59,570 --> 00:06:03,920 After that, we need to also do the operator here, copy assignment here. 85 00:06:04,760 --> 00:06:06,320 So in this case, it's going to be. 86 00:06:08,110 --> 00:06:09,160 Void Operator. 87 00:06:09,190 --> 00:06:10,740 Void operator. 88 00:06:11,120 --> 00:06:14,260 Operator equals. 89 00:06:20,710 --> 00:06:22,360 Equals the const. 90 00:06:23,300 --> 00:06:24,410 Constant. 91 00:06:27,380 --> 00:06:29,180 The cat and the. 92 00:06:30,810 --> 00:06:31,710 Here. 93 00:06:31,740 --> 00: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 94 00:06:41,550 --> 00:06:41,850 name. 95 00:06:42,510 --> 00:06:48,780 And now we need to implement the interface here that we created the Makesound interface. 96 00:06:49,260 --> 00:06:50,250 So. 97 00:06:51,460 --> 00:06:52,090 Here. 98 00:06:52,090 --> 00:06:55,930 The string here string makes sound. 99 00:06:57,770 --> 00:06:59,590 Here and over it. 100 00:07:01,900 --> 00:07:02,740 Return. 101 00:07:03,010 --> 00:07:05,950 What the cat makes the mew mew hear. 102 00:07:06,130 --> 00:07:06,610 Yeah. 103 00:07:06,640 --> 00:07:09,700 Mew Mew here. 104 00:07:10,060 --> 00:07:12,520 And we will also create a template here. 105 00:07:13,990 --> 00:07:16,570 The temp actually spiked here. 106 00:07:16,570 --> 00:07:20,110 So we need to create a template after this. 107 00:07:21,170 --> 00:07:26,090 So it's going to be like, uh, the class We created three class here. 108 00:07:33,920 --> 00:07:35,660 Make sound here. 109 00:07:43,020 --> 00:07:45,480 Here, make this look nice. 110 00:07:45,750 --> 00:07:48,660 And after that, we will create a template, as I said. 111 00:07:49,410 --> 00:07:52,290 Pamphlet is going to be the type name. 112 00:07:53,400 --> 00:07:53,820 He. 113 00:07:55,560 --> 00:07:55,830 Here. 114 00:07:55,830 --> 00:07:57,930 And we will also create a new void. 115 00:07:58,110 --> 00:08:03,690 We will, as we did in previous lecture, we will create a new void named getName and make. 116 00:08:04,380 --> 00:08:04,800 Here. 117 00:08:04,800 --> 00:08:06,390 Actually, we don't need the. 118 00:08:07,270 --> 00:08:07,930 Here. 119 00:08:08,020 --> 00: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 120 00:08:20,470 --> 00:08:22,060 to make it the animal. 121 00:08:22,890 --> 00:08:25,290 Animal or just an. 122 00:08:27,210 --> 00:08:28,290 Our animal. 123 00:08:29,070 --> 00:08:29,810 Yeah. 124 00:08:29,820 --> 00:08:31,190 The animal is okay. 125 00:08:31,200 --> 00:08:33,030 The animal. 126 00:08:34,440 --> 00:08:35,760 And here. 127 00:08:39,260 --> 00:08:39,800 The animal. 128 00:08:39,800 --> 00:08:44,720 We will see out the animal Dot get name. 129 00:08:45,610 --> 00:08:48,520 And goes here. 130 00:08:49,840 --> 00:08:54,220 See out the animal that makes sound. 131 00:08:54,250 --> 00:08:55,320 Makes sound. 132 00:08:55,720 --> 00:08:58,750 Is the get name and make. 133 00:08:58,750 --> 00:09:04,480 So we will also need the get name and make sound here and line. 134 00:09:04,480 --> 00: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 135 00:09:10,410 --> 00:09:14,020 T here template. 136 00:09:15,340 --> 00:09:16,720 Template. 137 00:09:16,720 --> 00:09:17,890 The type name. 138 00:09:17,920 --> 00:09:20,830 Type name is going to be here. 139 00:09:21,400 --> 00:09:22,750 After that. 140 00:09:26,770 --> 00:09:32,530 And we will also create an animal template class animal template. 141 00:09:33,430 --> 00:09:42,190 Here we will first assign private that T animal or T my animal. 142 00:09:42,910 --> 00:09:50,590 And after that we will also create something public that we will make the constructor here, the private 143 00:09:50,590 --> 00:09:51,880 public here. 144 00:09:55,230 --> 00:09:56,060 T my animal. 145 00:09:56,250 --> 00:09:57,510 The public is going to be. 146 00:09:58,530 --> 00:10:02,280 Uh, the animal template public is going to be animal. 147 00:10:03,190 --> 00:10:04,300 Animal testing. 148 00:10:04,330 --> 00:10:07,210 This will get t dot animal. 149 00:10:07,660 --> 00:10:11,560 And here we will pass this to my animal. 150 00:10:11,590 --> 00:10:13,960 Here, my animal. 151 00:10:13,960 --> 00:10:14,860 And we will. 152 00:10:16,420 --> 00:10:16,810 Yeah. 153 00:10:18,590 --> 00:10:19,130 Here. 154 00:10:19,670 --> 00:10:30,140 We will pass the animal here to this oops, this animal to here, as we did in the earlier animal here. 155 00:10:31,510 --> 00:10:32,710 And after that. 156 00:10:34,380 --> 00:10:34,710 Here. 157 00:10:34,710 --> 00:10:37,350 We will open and close the curly braces. 158 00:10:39,500 --> 00:10:44,450 After that, we will create a getName and make sound here. 159 00:10:44,540 --> 00:10:45,980 So void. 160 00:10:47,990 --> 00:10:51,680 Get name and make sound here. 161 00:10:54,850 --> 00:10:59,110 The crowd is going to be not the animal in this case. 162 00:10:59,110 --> 00:11:03,970 It's going to be my animal, my animal dot get name. 163 00:11:05,900 --> 00:11:07,380 Yeah, they get name. 164 00:11:10,280 --> 00:11:15,110 Yes yeah my animal dot get name and. 165 00:11:17,190 --> 00:11:18,000 Glacier. 166 00:11:19,630 --> 00:11:20,770 Our animal name. 167 00:11:20,770 --> 00:11:29,530 It goes like, see out my animal, but make sound. 168 00:11:31,610 --> 00:11:33,020 Here and in line. 169 00:11:33,020 --> 00:11:36,620 So first we will print the our animal name. 170 00:11:38,450 --> 00:11:43,040 So animal in this case, the dog here, dog goes here. 171 00:11:43,460 --> 00:11:44,240 Woof, woof here. 172 00:11:44,240 --> 00:11:45,800 And then we will end the line. 173 00:11:45,800 --> 00:11:49,550 And this is the new we will click new line for us. 174 00:11:50,000 --> 00:11:53,810 And now let's start creating our main function. 175 00:11:53,810 --> 00:11:55,220 But first, let's check the lecture time. 176 00:11:55,220 --> 00:11:55,790 Yes. 177 00:11:55,850 --> 00:11:58,880 So let's create our main function now. 178 00:12:00,540 --> 00:12:03,090 After the closing the animal template. 179 00:12:03,120 --> 00:12:06,900 Now we will clear the integer main integer main function. 180 00:12:07,050 --> 00:12:09,600 And this going to be the dog here. 181 00:12:09,630 --> 00:12:10,140 Dog. 182 00:12:10,140 --> 00:12:11,220 It's dog. 183 00:12:11,220 --> 00:12:19,770 In this case, let's actually make this Labrador here and animal template template. 184 00:12:21,580 --> 00:12:23,680 Uh, dog is going to be. 185 00:12:25,210 --> 00:12:27,100 Bulk is going to be a doc template. 186 00:12:27,130 --> 00:12:28,870 Doc Template. 187 00:12:28,990 --> 00:12:29,770 Doc. 188 00:12:33,060 --> 00:12:35,130 Now the dog template will. 189 00:12:35,980 --> 00:12:38,230 Look template dot get. 190 00:12:39,860 --> 00:12:40,760 Get. 191 00:12:41,790 --> 00:12:46,020 And get name and make sound here so get. 192 00:12:46,840 --> 00:12:47,700 Don't talk to me. 193 00:12:47,710 --> 00:12:54,220 Get name and make sound after will also apply the same for the. 194 00:12:55,200 --> 00:12:56,280 Kat Von here. 195 00:12:56,280 --> 00:12:57,510 So, Kat. 196 00:12:57,540 --> 00:12:58,560 Kat here. 197 00:12:59,010 --> 00:13:00,090 New Kat Instance. 198 00:13:00,090 --> 00:13:00,480 Kat. 199 00:13:00,510 --> 00:13:01,170 Kat. 200 00:13:01,200 --> 00:13:01,740 Kat. 201 00:13:01,740 --> 00:13:02,240 Here in this. 202 00:13:02,520 --> 00:13:03,450 Estonian. 203 00:13:03,570 --> 00:13:04,890 Estonian. 204 00:13:05,040 --> 00:13:05,880 Kat. 205 00:13:06,910 --> 00:13:10,360 Here and this is going to be an animal template. 206 00:13:11,080 --> 00:13:12,490 The cat. 207 00:13:13,930 --> 00:13:16,300 Is going to be make this cat template. 208 00:13:16,310 --> 00:13:20,380 Template here and cat will pass the cat here. 209 00:13:21,490 --> 00:13:25,930 Now the cat template dot get name and make sense. 210 00:13:25,960 --> 00:13:27,280 We will use this function. 211 00:13:27,400 --> 00:13:34,690 So as you can see in this code, we have a new class named the Animal template. 212 00:13:36,090 --> 00:13:36,480 Here. 213 00:13:38,340 --> 00:13:43,290 So it's a template class and it can be used by any data types. 214 00:13:43,290 --> 00:13:47,640 However, we have to define the data type and in the angle braces here. 215 00:13:47,640 --> 00:13:54,870 So as we can see in this code, when we use the instances, the doc template and the cat template here, 216 00:13:54,900 --> 00:14:02,760 the doc template and cat template here, um, they use this instances. 217 00:14:04,290 --> 00:14:06,380 And we will use the angle braces here. 218 00:14:06,390 --> 00:14:12,030 So if you build and run the code, we will get the output as we did in previous lectures. 219 00:14:12,030 --> 00:14:15,860 But in this case we use the classes to make this real. 220 00:14:15,870 --> 00:14:18,150 So now we're going to open this. 221 00:14:18,150 --> 00:14:20,790 Actually, let me open the CMD here. 222 00:14:21,150 --> 00:14:21,780 Here. 223 00:14:21,780 --> 00:14:23,700 Let's actually look what we have here. 224 00:14:23,700 --> 00:14:27,270 And this is our CPP file. 225 00:14:28,980 --> 00:14:31,140 And this is our main.cpp file. 226 00:14:31,140 --> 00:14:41,180 So here this class template dot cpp file is this the newly created and the main is this main.cpp here. 227 00:14:41,190 --> 00:14:48,990 So now we just going to compile the class template dot cpp g++ class template dot cpp here. 228 00:14:48,990 --> 00:14:52,500 And as you can see here, it compiled without an error. 229 00:14:52,500 --> 00:14:57,180 And there here and as you can see here, we just create a new file here. 230 00:14:58,230 --> 00:14:59,980 So let's actually run this. 231 00:15:00,000 --> 00:15:06,960 And as you can see here, the Labrador goes woof, woof and Estonian cause cat goes moo moo here. 232 00:15:06,960 --> 00:15:10,440 So that was our example with the class template. 233 00:15:10,440 --> 00:15:11,970 So I'm waiting you in next lecture.