Basics
In any programming language, there exists something called a class.
By definition, a class is a collection of objects which can include functions,
assessors, data variables, operator overloads etc...
A simple class structure is declared in C++ as follows:
To end a class, there must be a semicolon after the closing brace.
Constructors
All classes have something called constructors. Constructors must match the name of
the class EXACTLY. They also have no return type (not even void. See example below
for this demonstration).
Without a constructor(s) the class you are making will not exist. Think of a
blueprint for a building. You are designing it carefully and when it is done,
you need to construct the building. In a similar way, the class by itself is the
blueprint containing all the appropriate information. Once you are ready to use
it in the program, you need to construct the class, otherwise it does not exist.
Example 1:
Simple class I
Download source code here (Right click - Save Tagret As...)
As you will observe, the class is representing a simple rectangle which will calculate
the area and perimeter of it. This is just a sample so you can see what is going on here
relating to the constructors.
Notice in the main() function we declared the object r of type Rect.
This uses one of the two constructos available (here, the non default constructor).
Any constructor that takes more than zero arguments is considered an additional constructor and
is not default. So according to the class, the two sides we give it are 3 and 5 (representing
s1 and s2 respectively).
Had the other constructor been used (the default constructor), the
2 sides would have both been set to 1 (logically sound since a rectangle cannot have any side
equal to zero).
The reason it is called default is that the constructor does not take any arguments and simply
sets the data variables of the class to logical initial values.
The Area() and Perimeter() functions are called member functions.
See the next section for more details. Member functions were discussed in the functions tutorial
(tutorial 5). Also note that the member functions are accessed with the
dot operator ( . )
The output of the above program is:
Accessors
Since we declare our data variables as private, they can only be accessed from within the class
itself, not outside (say in the main() function). When you attempt to use the dot operator
on a private variable you will get an error.
This problem can be solved with what is called an accessor.
An accessor is a member function with the appropriate return type of the variable you are
trying to access which will simply return the value of that variable. Let's add on to the
previous example to see what an accessor is:
Notice that the variables are simply being returned and nothing is changing. This will
allow the program to access the data variables from anywhere in the program.
Example 2:
Simple class II
Download source code here (Right click - Save Tagret As...)
Notice that the only changes comes in the main() function. Here, we are declaring variables
called s1 and s2 which will hold the values of the variables from that class.
There is also no name conflicts between the variables since we are in the main() function.
The accessors make life a bit easier as we do not have to constantly hard code numbers each time
when calling a member function.
Mutators
Mutators are opposite of accessors. Here, they are going to change the data variables
in the class and not get them. All mutators are of type void since they do not return
values. Also, they take one argument since that will be the new value of the variable.
Now, with mutators, you need to do some error checking. For the purpose of this class,
a side of a rectangle cannot be zero or negative. So an if statement is appropriate checking
for that. Let's see the two mutators:
These are going to be placed in the class right after the accessors. Here, we do the
appropriate error checking with the if statement. You could also tell the program to
exit(1) instead of return but then the entire program will terminate.
See the next tutorial for the continuation of the series. That will feature member functions
and operator overloads.