- [Instructor] The class is the building block of objects
in any object-oriented language.
In C++ classes are both simple
and incredibly powerful.
This is a brief introduction to classes.
The purpose here is to familiarize you with the concept
and basic syntax.
Classes will be covered in much greater detail
as we continue in this course and in the companion course
C++ advanced topics.
So here's class dot CPP
from chapter two of the exercise files.
This is a simple class starts here and it ends there.
This simple class it carries one integer data member,
and two function members.
The main distinction between a class and a struct
is that members default to private in a class
while they default to public in a struct.
In practice, this means that data members
are typically hidden from the interface in a class,
so this data member is private
it cannot be accessed directly,
and the function members are typically placed
in a public section.
So we have this public keyword with a colon
and then that declares a public section,
and so these function members are public.
The function members serve as the interface for interacting
with objects made from the class,
and so that allows the object to set the value
and get the value of the private data member.
So the main purpose of this class
is to encapsulate the integer.
This allows the class and its objects to manipulate
the value in unique ways without exposing
the inner workings to the interface.
Encapsulation as a primary function
of object-oriented programming.
So down here in the main function we declare an integer
and we declare an object based on the class.
So C is the class you notice up here on line five class C,
so that names this class is C
and then you can use C as a type to declare a new object.
So O one is an object of type class C,
and then we use the set value member function
to set the value of the object.
And you notice we have this same dot notation that we used
with a structure.
And then we can get the value
using the get value function member,
also with the dot notation on the object.
And so when I build and run this,
you notice it says values 47,
and that's our print F here printing the value
from the object.
So we can easily create more objects
based on the same class call them two and three,
and we can set their values and say I times two,
and I times three, and we can print all of those.
And when I build and run you see we get A.
I didn't set object two and object three, did I?
There we go, and now we get the results that we expect.
So this makes it easy for us to create several objects
of the same class and each of them carries their own data.
They're not carrying the same data,
they're carrying their own data.
So just like with a structure,
when we build an object based on a class
we're building an instance of that class,
and each of these is a separate instance,
each with its own memory space and each most importantly
with its own copy of the data member.
So classes are fundamental to the use of C++
as an object-oriented language.
This is a very brief introduction.
We'll cover classes in greater detail
as we continue with this course and even more
so in the companion course C++ advanced topics.