- Classes in C++ are very powerful and flexible.
This is the fundamental unit of object oriented programming.
Here is class.cpp from chapter six of the exercise files,
this is an example of a very simple class.
It holds a simple value and provides an interface
to set and get that value.
So here we have the class definition.
You'll notice the class keyword is used to introduce
the definition of a class, much like the struct keyword
is used to introduce the definition of a structure.
And then we have the name of the class
and inside the curly braces,
we have the class definition itself,
sometimes called the class interface
and that is terminated with a semi-colon.
Inside the class definition we have a data member
and classes unlike structures, default to private.
And so this data member here is not visible
to the public interface.
So when I'm using an object created with this class
I will not be able to access directly this data member.
Instead there's a public section introduced
with the public keyword and a colon
and this is where the actual interface happens
and here we have two function members,
one for setvalue and one for getvalue.
And you notice that they're very simple functions.
The setvalue will take an integer parameter and it will set
our data member because these private members are accessible
from the interface, from the functions that you'll find
as part of the class, even in the public section.
And so this is how I actually interface
with the private data members of the class
and so here I set it and here I get it with a return.
Down here in main, we define an object
or instantiate an object with this class
just as we would any type, we use the name of the class
and the place of the type
and we can create an object called o1.
We access the members or the public members rather
of the class through the object using the dot notation
just as we would with a structure.
So here we call the setvalue function member to set a value
and here we call the getvalue function member to get a value
again with the dot notation.
And when I build and run this you see, we set the value,
we get the value and report that the value is 47.
Now, normally it's considered best practice
to separate the interface and the implementation.
Oftentimes with very simple functions like this
we'll go ahead and define the functions in line like this
but if we wanted to separate them out,
we'd simply take the body of the function
and replace it with a semi-colon here
and we can do that for both of these.
And then we can define the actual function separately
using notation like this, I say, the void is the return type
and then the name of the function has the C1 with two colons
and then the name of the function.
And I'll say int value
and I'll say, i equals value.
And now I have my setter.
The setvalue is often called a setter
and the getvalue is often called a getter.
Setters and getters are generic terms
in object oriented programming for things
that set and get values.
And then the getter is int C1 again, getvalue
and return i.
And now you'll notice when we build and run,
this works exactly as we expect,
but now we have separated out what's called
the implementation from what's called the interface.
So these parts would commonly be
in at least three separate files.
The interface would be often in a header file
with a .H or .HPP extension and the implementation
would often be in a separate CPP file
and then the usage would be in its own CPP file.
But for teaching purposes and as you're learning
the basic syntax of C++, it's a little more convenient
to put all of this in one file like this
and we'll be mostly doing that throughout this chapter
but you'll see examples of having them in separate files
later in this course.