1 00:00:00,620 --> 00:00:07,130 Let's actually think of our project that we have another class that is derived from the animal class. 2 00:00:07,160 --> 00:00:15,200 For instance, Cat, we are going to make a function that will invoke the get name and make some the 3 00:00:15,200 --> 00:00:18,440 methods for both dog and cat instances. 4 00:00:18,440 --> 00:00:21,680 So in this case, we're going to also add the cat here. 5 00:00:22,940 --> 00:00:28,760 And without creating the two separate functions, we can use the templates which we're going to start 6 00:00:28,760 --> 00:00:30,570 it doing it now. 7 00:00:30,590 --> 00:00:33,620 My name is Stefan and let's get started our lecture. 8 00:00:45,660 --> 00:00:50,760 So we firstly are going to create, as you can see, this is a protected. 9 00:00:50,790 --> 00:00:54,240 Now we have a public constructors. 10 00:00:56,320 --> 00:00:59,830 After that, we have the dog public animal class. 11 00:01:01,360 --> 00:01:08,230 And we will also create a cat public animal, as we said in previous lecture and uh, earlier of this 12 00:01:08,230 --> 00:01:08,770 lecture. 13 00:01:08,770 --> 00:01:16,660 So here we're going to create a class class cat and it's going to be public animal. 14 00:01:18,060 --> 00:01:22,860 Which is going to be a public public format here. 15 00:01:23,250 --> 00:01:31,310 So inside it, we're going to forward the constructor arguments from cat or from animal to the cat here. 16 00:01:31,320 --> 00:01:35,040 So public the. 17 00:01:36,400 --> 00:01:37,210 Here. 18 00:01:38,810 --> 00:01:47,480 A cat is going to be string name and the animal is going to be name here. 19 00:01:49,450 --> 00:01:52,300 And after that, we're going to add here. 20 00:01:53,550 --> 00:02:00,400 So now we're going to copy assignment operator overloading as we did here in Doc here. 21 00:02:00,420 --> 00:02:04,680 Void operator And let's actually, we will do it like that. 22 00:02:04,710 --> 00:02:15,150 The void operator here, the const cat instead of doc we're going to write it Cat here and Operator 23 00:02:15,180 --> 00:02:16,710 D here. 24 00:02:16,710 --> 00:02:24,360 And the my name is going to be d.my name here. 25 00:02:25,640 --> 00:02:29,350 So now we also need to implement interface. 26 00:02:29,360 --> 00:02:33,410 So the interface is going to be like the void. 27 00:02:33,440 --> 00:02:35,580 No, the interface is going to be a string. 28 00:02:35,600 --> 00:02:38,660 Of course it's going to be the make sound here. 29 00:02:38,660 --> 00:02:41,590 Make sound over it, over. 30 00:02:41,590 --> 00:02:50,000 Read and return The of course, Cat is not saying woof, woof here. 31 00:02:50,000 --> 00:02:51,720 Instead, it's like saying something. 32 00:02:51,740 --> 00:02:53,120 Mew Mew here. 33 00:02:53,120 --> 00:02:55,280 So here. 34 00:02:55,280 --> 00:02:58,160 This mew mew. 35 00:03:02,160 --> 00:03:02,670 Here. 36 00:03:02,670 --> 00:03:07,200 So now it's going to be we are getting to the template parts here. 37 00:03:07,440 --> 00:03:10,380 So after that, we will create a template. 38 00:03:12,600 --> 00:03:15,270 Template Typename. 39 00:03:15,270 --> 00:03:16,110 Name T. 40 00:03:19,440 --> 00:03:20,670 And after that. 41 00:03:22,690 --> 00:03:25,200 We will create something like that. 42 00:03:25,280 --> 00:03:30,280 The function that will get name and make sound. 43 00:03:30,850 --> 00:03:31,810 So. 44 00:03:33,170 --> 00:03:34,400 The void here. 45 00:03:35,410 --> 00:03:41,230 Boyd get name and make sound here. 46 00:03:41,500 --> 00:03:45,790 The key here is the animal. 47 00:03:49,490 --> 00:03:52,250 And see out here. 48 00:03:52,340 --> 00:03:58,820 Now, we need to also add braces here so see out the animal dot getName. 49 00:03:59,580 --> 00:04:00,800 getName. 50 00:04:02,940 --> 00:04:04,470 And that's going to be. 51 00:04:05,920 --> 00:04:13,960 Go here and see out the animal that make sound. 52 00:04:14,050 --> 00: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. 53 00:04:20,500 --> 00:04:26,140 We, as you can see in this main here, we will also need to add cat. 54 00:04:26,170 --> 00:04:29,350 So cat, cat here. 55 00:04:31,280 --> 00:04:35,960 The cat is going to be, for example, the regular cat. 56 00:04:35,990 --> 00:04:39,050 Yeah, regular cat. 57 00:04:39,500 --> 00:04:47,240 And we will use the get name and make son, which is going to pass the cat here like this. 58 00:04:48,200 --> 00:04:49,220 Now we're after that. 59 00:04:49,220 --> 00:04:51,830 We have the return and. 60 00:04:52,730 --> 00:04:53,330 Here. 61 00:04:53,330 --> 00:04:54,380 We have done our code. 62 00:04:54,410 --> 00: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 63 00:05:04,340 --> 00:05:05,390 some function. 64 00:05:05,390 --> 00:05:08,960 Since we have defined the type name here. 65 00:05:08,990 --> 00:05:16,730 Type name template type name is t here before the function definition. 66 00:05:16,970 --> 00:05:23,000 So the type name is a keyword in c plus plus which use the write template. 67 00:05:24,180 --> 00:05:31,350 So the keyword is used for specifying that a symbol in a template definition or declaration is a type. 68 00:05:31,350 --> 00:05:34,740 So in this example, the symbol here is T. 69 00:05:35,280 --> 00:05:41,400 As a result, the function becomes generic and can accept various data types. 70 00:05:41,400 --> 00:05:45,630 And if we build and run this code, we will see something like this. 71 00:05:45,630 --> 00:05:51,150 Here is the G++ Main.cpp. 72 00:05:52,630 --> 00:05:55,920 And adding.here. 73 00:05:55,960 --> 00:05:58,180 Oops, we had a problem with it. 74 00:05:58,450 --> 00:05:59,230 Yeah. 75 00:05:59,740 --> 00:06:02,070 Output here we need to output. 76 00:06:02,080 --> 00:06:02,500 Yeah. 77 00:06:02,980 --> 00:06:06,730 And let's actually run adding cat dot x. 78 00:06:06,970 --> 00:06:10,030 And as you can see here, terrier is barking. 79 00:06:10,030 --> 00:06:10,560 Woof, woof. 80 00:06:10,600 --> 00:06:11,710 Terrier is barking. 81 00:06:11,710 --> 00:06:12,220 Woof, woof. 82 00:06:12,220 --> 00:06:14,890 And regular cat goes mew Mew here. 83 00:06:15,400 --> 00:06:18,560 So you will see something like that. 84 00:06:18,580 --> 00:06:25,720 So please, please ensure that the data type we pass to the generic function has the ability to do all 85 00:06:25,720 --> 00:06:28,100 the operation invoking from the generic function. 86 00:06:28,120 --> 00:06:35,950 However, the compiler will compile if the data type was we pass does not have the except operation. 87 00:06:35,950 --> 00:06:44,080 So in this function template example we need to pass a data type that is an instance of the animal class 88 00:06:44,080 --> 00:06:49,600 so we can pass either instance of the animal class or an instance of a derived class of the animal class 89 00:06:49,600 --> 00:06:50,050 here.