Classes are containers for variables and operations.
So that will affect the variables, as we talked about earlier, as ADTs implement encapsulation techniques
for grouping all similar data and functions into group, the classes can also be applied to group them.
A class has three access control sections for wrapping the data and methods and they are here.
Let's actually this is the access.
Controls.
Here.
So there there are three access controls for types and cast everything here.
So we have the public.
Public.
So public are the data and methods can be accessed by any user of the class.
We also have the protected.
Protect.
That.
So the protected they are the data and the methods can only be accessed by class methods and the derived
classes and friends.
We also have the private.
The private is the data and methods can only be accessed by class methods and friends.
So let's go back to the definition of abstraction and information hiding in the previous lecture so
we can implement abstraction by using protected or private or not, or here private so we can use protected
and private for implementing abstraction.
So we can also use the implement abstraction by using private and protected keywords to hide methods
from outside the class and implement the information hiding by using a protected or private keyword
to hide the data from the outside the class.
So now let's build a simple application or simple class named animal.
So here let's right click on the project Cplusplus class animal namespace is okay.
So yeah, here.
So Cplusplus class here.
Let's actually delete this definitions here.
So this is our animal class.
So we will create inside this animal class, we will create a private string and we will name it my
name, private string.
Or we will do it like that.
Private.
String is going to be my name.
And also let's actually we need to import string in order to use it.
So we will also using namespace using namespace std and import.
Or include.
Include.
Include string and include the stream.
Here.
So this is a string my name.
And we will also define some public variables, which is the here the public public is going to be.
The public is going to be void here.
Give name and this is going to be string name.
And this is going to be my name.
Equals name.
So this is the, uh.
This is the.
Yeah, this is the function now.
And we will also create a new function named getName, which is going to be of course, give a return
a string since the name is itself is a string string.
Get get name and this name.
Name is going to just return string and will not take any parameters.
Just return the string.
So return my name.
So we have the setter and getter here.
So as we see in this course here, we cannot access the M variable directly since we access and we assigned
it as private here.
This is the private variable.
So however, we have two public methods to access variables from the inside class.
So the give name methods will modify the value here.
The given modify will modify the.
My name my name value and the get name get name.
Here methods will return the my name value.
So the here actually let's created some code snippet here and let's actually turn this and let's delete
this now.
And make it and also can delete the animal class here.
And we will just create this animal class inside our the main CP here.
And include.
Include.
String.
And include.
Include.
Yeah.
Stream is already included and using namespace STD.
So here.
Now let's actually try something in the main method here.
So animal dog here, animal here.
So as you know, the dog is animal here, dog here.
We will give dog to the same.
To name here.
So give name.
It's going to be let's actually name our dog Oxley.
Yeah.
Name our dog Oxley here.
And we will print something C out high.
I.
I am at.
I am here.
We will use a dog dot, get name, get name and end line.
So here, let's run our program here.
It's close.
A g++ main.cpp a dot exe.
So here I am, a Oxley.
So our dog name is Oxley here.
So in this code we created a variable named dog of the type of animal.
Since the dog has the ability that animal has, such as invoking the given name and get name methods
here.
This is the output you should get here.
Hi my, I'm a auxiliar here.
Hi.
I'm.
And let's actually change the dog name here.
Hi.
I'm a sea lion.
Or I am CP dog.
Yeah, that's acceptable.
Here and here.
And after that let's run a here.
Hi, I'm a CP dog here.
Now we can say that Animal ADT Abstract data type has two functions and they are give name.
The given name.
And the get name.
So after talking about the Simple class, you might see that there's a similarity between struct and
classes.
So they both actually have similar behaviors.
So the differences are however that struct have the default public members while classes have the default
private members.
So I personally recommend using struct as a data structures only so they don't have any methods in them
and using classes to build abstract data types.
ADTs So as you can see in this code here.
The we assign the variable to the instance of the animal class by using its constructor.
Here, this is he.
This is animal class constructor here.
So which is as you can see, this is the our animal class constructor.
However, we can initialize a class data member by using a class constructor.
The constructor name is the same as the class name, so let's refactor our receiving animal class so
it has a constructor.
So the refactored code here.
Let's actually go to animal class here and let's actually refactor our class.
Yeah, the animal class here.
We're going to have the.
Inside the public.
We will also.
Add some methods here.
So in public we're going to animal here.
This is the constructor.
String name will get animal and.
My name is going to be Name.
And here, this is our constructor here.
And here we don't need this our give name method here.
Anyway, so let's get rid of them.
And here we can.
We will pass the Oxley here as our dog name.
And here.
It will work the same, but the less code.
That's a dot x and x, as you can see here.
I'm I'm actually.
So as we can see in this code, we when we define the dog variable, we also initialize the my my name
private variable of the class.
So we don't need the get give name or we actually need the get name method for now, but we don't need
the give name method anymore.
So to assign the private variable.
So indeed we will get same output again if I build and run presenting code as we did here.
So in this code we assign dog as the animal type.
So however we can also derive a new class based on the base class.
So by deriving from the base class, the derived class will also have the behavior that the base class
has.
So let's refactor the animal class again.
And in next lecture we will add virtual method named Makesound here.
So I'm waiting you in next lecture.