WEBVTT

00:01.120 --> 00:04.390
Classes are containers for variables and operations.

00:04.390 --> 00:12.730
So that will affect the variables, as we talked about earlier, as ADTs implement encapsulation techniques

00:12.730 --> 00:20.650
for grouping all similar data and functions into group, the classes can also be applied to group them.

00:20.980 --> 00:29.050
A class has three access control sections for wrapping the data and methods and they are here.

00:29.050 --> 00:31.510
Let's actually this is the access.

00:33.030 --> 00:34.040
Controls.

00:36.260 --> 00:37.010
Here.

00:37.010 --> 00:43.390
So there there are three access controls for types and cast everything here.

00:43.400 --> 00:45.200
So we have the public.

00:45.530 --> 00:46.400
Public.

00:50.560 --> 00:51.430
Public.

00:51.430 --> 01:00.010
So public are the data and methods can be accessed by any user of the class.

01:00.040 --> 01:03.070
We also have the protected.

01:04.970 --> 01:06.470
Protect.

01:07.410 --> 01:07.950
That.

01:10.610 --> 01:18.590
So the protected they are the data and the methods can only be accessed by class methods and the derived

01:18.590 --> 01:20.300
classes and friends.

01:20.330 --> 01:23.030
We also have the private.

01:28.050 --> 01:36.140
The private is the data and methods can only be accessed by class methods and friends.

01:36.150 --> 01:41.640
So let's go back to the definition of abstraction and information hiding in the previous lecture so

01:41.640 --> 01:53.190
we can implement abstraction by using protected or private or not, or here private so we can use protected

01:53.190 --> 01:55.560
and private for implementing abstraction.

01:55.560 --> 02:03.690
So we can also use the implement abstraction by using private and protected keywords to hide methods

02:03.690 --> 02:09.750
from outside the class and implement the information hiding by using a protected or private keyword

02:09.750 --> 02:12.780
to hide the data from the outside the class.

02:12.780 --> 02:17.660
So now let's build a simple application or simple class named animal.

02:17.670 --> 02:24.780
So here let's right click on the project Cplusplus class animal namespace is okay.

02:24.780 --> 02:25.890
So yeah, here.

02:25.890 --> 02:27.760
So Cplusplus class here.

02:27.760 --> 02:30.730
Let's actually delete this definitions here.

02:36.050 --> 02:38.720
So this is our animal class.

02:39.050 --> 02:45.140
So we will create inside this animal class, we will create a private string and we will name it my

02:45.140 --> 02:48.380
name, private string.

02:48.380 --> 02:50.510
Or we will do it like that.

02:50.510 --> 02:51.260
Private.

02:53.100 --> 02:55.380
String is going to be my name.

02:55.380 --> 02:59.850
And also let's actually we need to import string in order to use it.

02:59.850 --> 03:07.140
So we will also using namespace using namespace std and import.

03:07.990 --> 03:09.220
Or include.

03:09.640 --> 03:10.840
Include.

03:12.460 --> 03:18.310
Include string and include the stream.

03:19.770 --> 03:20.210
Here.

03:20.220 --> 03:22.470
So this is a string my name.

03:22.470 --> 03:30.960
And we will also define some public variables, which is the here the public public is going to be.

03:31.590 --> 03:33.960
The public is going to be void here.

03:34.770 --> 03:38.970
Give name and this is going to be string name.

03:40.520 --> 03:42.680
And this is going to be my name.

03:42.680 --> 03:44.060
Equals name.

03:44.090 --> 03:45.990
So this is the, uh.

03:46.010 --> 03:47.090
This is the.

03:49.020 --> 03:51.180
Yeah, this is the function now.

03:51.630 --> 03:58.770
And we will also create a new function named getName, which is going to be of course, give a return

03:58.770 --> 04:02.740
a string since the name is itself is a string string.

04:03.060 --> 04:06.530
Get get name and this name.

04:06.770 --> 04:10.950
Name is going to just return string and will not take any parameters.

04:10.950 --> 04:12.360
Just return the string.

04:12.360 --> 04:14.880
So return my name.

04:14.880 --> 04:20.280
So we have the setter and getter here.

04:22.520 --> 04:32.510
So as we see in this course here, we cannot access the M variable directly since we access and we assigned

04:32.510 --> 04:35.290
it as private here.

04:35.300 --> 04:37.130
This is the private variable.

04:37.990 --> 04:44.050
So however, we have two public methods to access variables from the inside class.

04:44.050 --> 04:49.210
So the give name methods will modify the value here.

04:49.240 --> 04:52.390
The given modify will modify the.

04:53.020 --> 04:59.710
My name my name value and the get name get name.

04:59.710 --> 05:04.090
Here methods will return the my name value.

05:04.090 --> 05:13.360
So the here actually let's created some code snippet here and let's actually turn this and let's delete

05:13.360 --> 05:14.110
this now.

05:15.870 --> 05:18.990
And make it and also can delete the animal class here.

05:18.990 --> 05:25.860
And we will just create this animal class inside our the main CP here.

05:28.100 --> 05:29.490
And include.

05:29.510 --> 05:30.600
Include.

05:30.690 --> 05:31.730
String.

05:33.140 --> 05:35.120
And include.

05:35.960 --> 05:36.710
Include.

05:36.710 --> 05:37.130
Yeah.

05:37.370 --> 05:41.630
Stream is already included and using namespace STD.

05:43.910 --> 05:45.170
So here.

05:45.170 --> 05:48.520
Now let's actually try something in the main method here.

05:48.530 --> 05:52.570
So animal dog here, animal here.

05:52.580 --> 05:56.180
So as you know, the dog is animal here, dog here.

05:56.180 --> 05:58.400
We will give dog to the same.

05:59.240 --> 06:00.050
To name here.

06:00.050 --> 06:01.940
So give name.

06:02.330 --> 06:06.380
It's going to be let's actually name our dog Oxley.

06:06.670 --> 06:07.310
Yeah.

06:07.340 --> 06:08.930
Name our dog Oxley here.

06:08.930 --> 06:12.710
And we will print something C out high.

06:14.060 --> 06:14.750
I.

06:15.560 --> 06:17.410
I am at.

06:17.660 --> 06:18.890
I am here.

06:18.890 --> 06:25.310
We will use a dog dot, get name, get name and end line.

06:26.710 --> 06:30.400
So here, let's run our program here.

06:31.950 --> 06:33.090
It's close.

06:36.280 --> 06:41.600
A g++ main.cpp a dot exe.

06:42.520 --> 06:46.360
So here I am, a Oxley.

06:46.360 --> 06:48.630
So our dog name is Oxley here.

06:48.640 --> 06:54.100
So in this code we created a variable named dog of the type of animal.

06:54.100 --> 07:03.040
Since the dog has the ability that animal has, such as invoking the given name and get name methods

07:03.040 --> 07:03.670
here.

07:04.700 --> 07:07.850
This is the output you should get here.

07:07.910 --> 07:10.250
Hi my, I'm a auxiliar here.

07:10.280 --> 07:10.940
Hi.

07:10.940 --> 07:11.160
I'm.

07:11.180 --> 07:13.300
And let's actually change the dog name here.

07:13.310 --> 07:14.090
Hi.

07:14.290 --> 07:16.520
I'm a sea lion.

07:16.520 --> 07:19.640
Or I am CP dog.

07:19.880 --> 07:21.950
Yeah, that's acceptable.

07:21.950 --> 07:24.590
Here and here.

07:24.590 --> 07:27.440
And after that let's run a here.

07:27.440 --> 07:29.570
Hi, I'm a CP dog here.

07:30.430 --> 07:38.920
Now we can say that Animal ADT Abstract data type has two functions and they are give name.

07:40.070 --> 07:42.380
The given name.

07:43.410 --> 07:45.870
And the get name.

07:46.880 --> 07:53.570
So after talking about the Simple class, you might see that there's a similarity between struct and

07:53.570 --> 07:53.930
classes.

07:53.930 --> 07:56.780
So they both actually have similar behaviors.

07:56.780 --> 08:03.890
So the differences are however that struct have the default public members while classes have the default

08:03.890 --> 08:04.880
private members.

08:04.880 --> 08:11.960
So I personally recommend using struct as a data structures only so they don't have any methods in them

08:11.960 --> 08:15.140
and using classes to build abstract data types.

08:15.170 --> 08:18.770
ADTs So as you can see in this code here.

08:19.700 --> 08:25.920
The we assign the variable to the instance of the animal class by using its constructor.

08:25.940 --> 08:27.250
Here, this is he.

08:27.260 --> 08:29.420
This is animal class constructor here.

08:30.550 --> 08:35.000
So which is as you can see, this is the our animal class constructor.

08:35.020 --> 08:40.150
However, we can initialize a class data member by using a class constructor.

08:40.180 --> 08:46.630
The constructor name is the same as the class name, so let's refactor our receiving animal class so

08:46.630 --> 08:47.560
it has a constructor.

08:47.560 --> 08:49.480
So the refactored code here.

08:49.960 --> 08:55.190
Let's actually go to animal class here and let's actually refactor our class.

08:55.210 --> 08:57.340
Yeah, the animal class here.

08:57.370 --> 08:59.020
We're going to have the.

09:00.090 --> 09:01.590
Inside the public.

09:03.010 --> 09:03.730
We will also.

09:04.840 --> 09:05.860
Add some methods here.

09:05.860 --> 09:08.110
So in public we're going to animal here.

09:08.110 --> 09:09.340
This is the constructor.

09:09.550 --> 09:12.940
String name will get animal and.

09:13.850 --> 09:17.120
My name is going to be Name.

09:19.870 --> 09:22.270
And here, this is our constructor here.

09:23.290 --> 09:27.880
And here we don't need this our give name method here.

09:28.240 --> 09:30.250
Anyway, so let's get rid of them.

09:30.250 --> 09:31.240
And here we can.

09:31.240 --> 09:34.900
We will pass the Oxley here as our dog name.

09:35.290 --> 09:36.730
And here.

09:38.500 --> 09:41.230
It will work the same, but the less code.

09:44.930 --> 09:48.700
That's a dot x and x, as you can see here.

09:48.710 --> 09:50.200
I'm I'm actually.

09:50.390 --> 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

09:58.670 --> 10:00.230
private variable of the class.

10:00.230 --> 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

10:06.710 --> 10:09.710
the give name method anymore.

10:09.710 --> 10:11.450
So to assign the private variable.

10:11.450 --> 10:18.650
So indeed we will get same output again if I build and run presenting code as we did here.

10:18.650 --> 10:23.510
So in this code we assign dog as the animal type.

10:23.510 --> 10:27.530
So however we can also derive a new class based on the base class.

10:27.530 --> 10:34.820
So by deriving from the base class, the derived class will also have the behavior that the base class

10:34.820 --> 10:35.330
has.

10:35.330 --> 10:38.060
So let's refactor the animal class again.

10:38.060 --> 10:43.640
And in next lecture we will add virtual method named Makesound here.

10:43.640 --> 10:46.410
So I'm waiting you in next lecture.
