1 00:00:01,120 --> 00:00:04,390 Classes are containers for variables and operations. 2 00:00:04,390 --> 00:00:12,730 So that will affect the variables, as we talked about earlier, as ADTs implement encapsulation techniques 3 00:00:12,730 --> 00:00:20,650 for grouping all similar data and functions into group, the classes can also be applied to group them. 4 00:00:20,980 --> 00:00:29,050 A class has three access control sections for wrapping the data and methods and they are here. 5 00:00:29,050 --> 00:00:31,510 Let's actually this is the access. 6 00:00:33,030 --> 00:00:34,040 Controls. 7 00:00:36,260 --> 00:00:37,010 Here. 8 00:00:37,010 --> 00:00:43,390 So there there are three access controls for types and cast everything here. 9 00:00:43,400 --> 00:00:45,200 So we have the public. 10 00:00:45,530 --> 00:00:46,400 Public. 11 00:00:50,560 --> 00:00:51,430 Public. 12 00:00:51,430 --> 00:01:00,010 So public are the data and methods can be accessed by any user of the class. 13 00:01:00,040 --> 00:01:03,070 We also have the protected. 14 00:01:04,970 --> 00:01:06,470 Protect. 15 00:01:07,410 --> 00:01:07,950 That. 16 00:01:10,610 --> 00:01:18,590 So the protected they are the data and the methods can only be accessed by class methods and the derived 17 00:01:18,590 --> 00:01:20,300 classes and friends. 18 00:01:20,330 --> 00:01:23,030 We also have the private. 19 00:01:28,050 --> 00:01:36,140 The private is the data and methods can only be accessed by class methods and friends. 20 00:01:36,150 --> 00:01:41,640 So let's go back to the definition of abstraction and information hiding in the previous lecture so 21 00:01:41,640 --> 00:01:53,190 we can implement abstraction by using protected or private or not, or here private so we can use protected 22 00:01:53,190 --> 00:01:55,560 and private for implementing abstraction. 23 00:01:55,560 --> 00:02:03,690 So we can also use the implement abstraction by using private and protected keywords to hide methods 24 00:02:03,690 --> 00:02:09,750 from outside the class and implement the information hiding by using a protected or private keyword 25 00:02:09,750 --> 00:02:12,780 to hide the data from the outside the class. 26 00:02:12,780 --> 00:02:17,660 So now let's build a simple application or simple class named animal. 27 00:02:17,670 --> 00:02:24,780 So here let's right click on the project Cplusplus class animal namespace is okay. 28 00:02:24,780 --> 00:02:25,890 So yeah, here. 29 00:02:25,890 --> 00:02:27,760 So Cplusplus class here. 30 00:02:27,760 --> 00:02:30,730 Let's actually delete this definitions here. 31 00:02:36,050 --> 00:02:38,720 So this is our animal class. 32 00:02:39,050 --> 00:02:45,140 So we will create inside this animal class, we will create a private string and we will name it my 33 00:02:45,140 --> 00:02:48,380 name, private string. 34 00:02:48,380 --> 00:02:50,510 Or we will do it like that. 35 00:02:50,510 --> 00:02:51,260 Private. 36 00:02:53,100 --> 00:02:55,380 String is going to be my name. 37 00:02:55,380 --> 00:02:59,850 And also let's actually we need to import string in order to use it. 38 00:02:59,850 --> 00:03:07,140 So we will also using namespace using namespace std and import. 39 00:03:07,990 --> 00:03:09,220 Or include. 40 00:03:09,640 --> 00:03:10,840 Include. 41 00:03:12,460 --> 00:03:18,310 Include string and include the stream. 42 00:03:19,770 --> 00:03:20,210 Here. 43 00:03:20,220 --> 00:03:22,470 So this is a string my name. 44 00:03:22,470 --> 00:03:30,960 And we will also define some public variables, which is the here the public public is going to be. 45 00:03:31,590 --> 00:03:33,960 The public is going to be void here. 46 00:03:34,770 --> 00:03:38,970 Give name and this is going to be string name. 47 00:03:40,520 --> 00:03:42,680 And this is going to be my name. 48 00:03:42,680 --> 00:03:44,060 Equals name. 49 00:03:44,090 --> 00:03:45,990 So this is the, uh. 50 00:03:46,010 --> 00:03:47,090 This is the. 51 00:03:49,020 --> 00:03:51,180 Yeah, this is the function now. 52 00:03:51,630 --> 00:03:58,770 And we will also create a new function named getName, which is going to be of course, give a return 53 00:03:58,770 --> 00:04:02,740 a string since the name is itself is a string string. 54 00:04:03,060 --> 00:04:06,530 Get get name and this name. 55 00:04:06,770 --> 00:04:10,950 Name is going to just return string and will not take any parameters. 56 00:04:10,950 --> 00:04:12,360 Just return the string. 57 00:04:12,360 --> 00:04:14,880 So return my name. 58 00:04:14,880 --> 00:04:20,280 So we have the setter and getter here. 59 00:04:22,520 --> 00:04:32,510 So as we see in this course here, we cannot access the M variable directly since we access and we assigned 60 00:04:32,510 --> 00:04:35,290 it as private here. 61 00:04:35,300 --> 00:04:37,130 This is the private variable. 62 00:04:37,990 --> 00:04:44,050 So however, we have two public methods to access variables from the inside class. 63 00:04:44,050 --> 00:04:49,210 So the give name methods will modify the value here. 64 00:04:49,240 --> 00:04:52,390 The given modify will modify the. 65 00:04:53,020 --> 00:04:59,710 My name my name value and the get name get name. 66 00:04:59,710 --> 00:05:04,090 Here methods will return the my name value. 67 00:05:04,090 --> 00:05:13,360 So the here actually let's created some code snippet here and let's actually turn this and let's delete 68 00:05:13,360 --> 00:05:14,110 this now. 69 00:05:15,870 --> 00:05:18,990 And make it and also can delete the animal class here. 70 00:05:18,990 --> 00:05:25,860 And we will just create this animal class inside our the main CP here. 71 00:05:28,100 --> 00:05:29,490 And include. 72 00:05:29,510 --> 00:05:30,600 Include. 73 00:05:30,690 --> 00:05:31,730 String. 74 00:05:33,140 --> 00:05:35,120 And include. 75 00:05:35,960 --> 00:05:36,710 Include. 76 00:05:36,710 --> 00:05:37,130 Yeah. 77 00:05:37,370 --> 00:05:41,630 Stream is already included and using namespace STD. 78 00:05:43,910 --> 00:05:45,170 So here. 79 00:05:45,170 --> 00:05:48,520 Now let's actually try something in the main method here. 80 00:05:48,530 --> 00:05:52,570 So animal dog here, animal here. 81 00:05:52,580 --> 00:05:56,180 So as you know, the dog is animal here, dog here. 82 00:05:56,180 --> 00:05:58,400 We will give dog to the same. 83 00:05:59,240 --> 00:06:00,050 To name here. 84 00:06:00,050 --> 00:06:01,940 So give name. 85 00:06:02,330 --> 00:06:06,380 It's going to be let's actually name our dog Oxley. 86 00:06:06,670 --> 00:06:07,310 Yeah. 87 00:06:07,340 --> 00:06:08,930 Name our dog Oxley here. 88 00:06:08,930 --> 00:06:12,710 And we will print something C out high. 89 00:06:14,060 --> 00:06:14,750 I. 90 00:06:15,560 --> 00:06:17,410 I am at. 91 00:06:17,660 --> 00:06:18,890 I am here. 92 00:06:18,890 --> 00:06:25,310 We will use a dog dot, get name, get name and end line. 93 00:06:26,710 --> 00:06:30,400 So here, let's run our program here. 94 00:06:31,950 --> 00:06:33,090 It's close. 95 00:06:36,280 --> 00:06:41,600 A g++ main.cpp a dot exe. 96 00:06:42,520 --> 00:06:46,360 So here I am, a Oxley. 97 00:06:46,360 --> 00:06:48,630 So our dog name is Oxley here. 98 00:06:48,640 --> 00:06:54,100 So in this code we created a variable named dog of the type of animal. 99 00:06:54,100 --> 00:07:03,040 Since the dog has the ability that animal has, such as invoking the given name and get name methods 100 00:07:03,040 --> 00:07:03,670 here. 101 00:07:04,700 --> 00:07:07,850 This is the output you should get here. 102 00:07:07,910 --> 00:07:10,250 Hi my, I'm a auxiliar here. 103 00:07:10,280 --> 00:07:10,940 Hi. 104 00:07:10,940 --> 00:07:11,160 I'm. 105 00:07:11,180 --> 00:07:13,300 And let's actually change the dog name here. 106 00:07:13,310 --> 00:07:14,090 Hi. 107 00:07:14,290 --> 00:07:16,520 I'm a sea lion. 108 00:07:16,520 --> 00:07:19,640 Or I am CP dog. 109 00:07:19,880 --> 00:07:21,950 Yeah, that's acceptable. 110 00:07:21,950 --> 00:07:24,590 Here and here. 111 00:07:24,590 --> 00:07:27,440 And after that let's run a here. 112 00:07:27,440 --> 00:07:29,570 Hi, I'm a CP dog here. 113 00:07:30,430 --> 00:07:38,920 Now we can say that Animal ADT Abstract data type has two functions and they are give name. 114 00:07:40,070 --> 00:07:42,380 The given name. 115 00:07:43,410 --> 00:07:45,870 And the get name. 116 00:07:46,880 --> 00:07:53,570 So after talking about the Simple class, you might see that there's a similarity between struct and 117 00:07:53,570 --> 00:07:53,930 classes. 118 00:07:53,930 --> 00:07:56,780 So they both actually have similar behaviors. 119 00:07:56,780 --> 00:08:03,890 So the differences are however that struct have the default public members while classes have the default 120 00:08:03,890 --> 00:08:04,880 private members. 121 00:08:04,880 --> 00:08:11,960 So I personally recommend using struct as a data structures only so they don't have any methods in them 122 00:08:11,960 --> 00:08:15,140 and using classes to build abstract data types. 123 00:08:15,170 --> 00:08:18,770 ADTs So as you can see in this code here. 124 00:08:19,700 --> 00:08:25,920 The we assign the variable to the instance of the animal class by using its constructor. 125 00:08:25,940 --> 00:08:27,250 Here, this is he. 126 00:08:27,260 --> 00:08:29,420 This is animal class constructor here. 127 00:08:30,550 --> 00:08:35,000 So which is as you can see, this is the our animal class constructor. 128 00:08:35,020 --> 00:08:40,150 However, we can initialize a class data member by using a class constructor. 129 00:08:40,180 --> 00:08:46,630 The constructor name is the same as the class name, so let's refactor our receiving animal class so 130 00:08:46,630 --> 00:08:47,560 it has a constructor. 131 00:08:47,560 --> 00:08:49,480 So the refactored code here. 132 00:08:49,960 --> 00:08:55,190 Let's actually go to animal class here and let's actually refactor our class. 133 00:08:55,210 --> 00:08:57,340 Yeah, the animal class here. 134 00:08:57,370 --> 00:08:59,020 We're going to have the. 135 00:09:00,090 --> 00:09:01,590 Inside the public. 136 00:09:03,010 --> 00:09:03,730 We will also. 137 00:09:04,840 --> 00:09:05,860 Add some methods here. 138 00:09:05,860 --> 00:09:08,110 So in public we're going to animal here. 139 00:09:08,110 --> 00:09:09,340 This is the constructor. 140 00:09:09,550 --> 00:09:12,940 String name will get animal and. 141 00:09:13,850 --> 00:09:17,120 My name is going to be Name. 142 00:09:19,870 --> 00:09:22,270 And here, this is our constructor here. 143 00:09:23,290 --> 00:09:27,880 And here we don't need this our give name method here. 144 00:09:28,240 --> 00:09:30,250 Anyway, so let's get rid of them. 145 00:09:30,250 --> 00:09:31,240 And here we can. 146 00:09:31,240 --> 00:09:34,900 We will pass the Oxley here as our dog name. 147 00:09:35,290 --> 00:09:36,730 And here. 148 00:09:38,500 --> 00:09:41,230 It will work the same, but the less code. 149 00:09:44,930 --> 00:09:48,700 That's a dot x and x, as you can see here. 150 00:09:48,710 --> 00:09:50,200 I'm I'm actually. 151 00:09:50,390 --> 00:09:58,670 So as we can see in this code, we when we define the dog variable, we also initialize the my my name 152 00:09:58,670 --> 00:10:00,230 private variable of the class. 153 00:10:00,230 --> 00:10:06,710 So we don't need the get give name or we actually need the get name method for now, but we don't need 154 00:10:06,710 --> 00:10:09,710 the give name method anymore. 155 00:10:09,710 --> 00:10:11,450 So to assign the private variable. 156 00:10:11,450 --> 00:10:18,650 So indeed we will get same output again if I build and run presenting code as we did here. 157 00:10:18,650 --> 00:10:23,510 So in this code we assign dog as the animal type. 158 00:10:23,510 --> 00:10:27,530 So however we can also derive a new class based on the base class. 159 00:10:27,530 --> 00:10:34,820 So by deriving from the base class, the derived class will also have the behavior that the base class 160 00:10:34,820 --> 00:10:35,330 has. 161 00:10:35,330 --> 00:10:38,060 So let's refactor the animal class again. 162 00:10:38,060 --> 00:10:43,640 And in next lecture we will add virtual method named Makesound here. 163 00:10:43,640 --> 00:10:46,410 So I'm waiting you in next lecture.