Tuning Objects

There are other features you can apply to make your objects more professional and less prone to errors. These include constant arguments, constant, private, and inline methods, etc.

Constant Arguments

When studying functions, we learned that when a function receives an argument that it does not modify, the argument should be declared as constant. This allows the compiler to make sure the argument would not be modified. The same technique applies to an argument used by a method of an object.

To declare an argument of an object?s method as constant, type the const keyword on the left of the argument?s data type. To improve our ShoeBox object, we would like to know the area of each side because different things will be displayed on each side and we need to know how much space is available. If we were dealing with a rectangle, we would just declare an Area() method as follows:

 

On a box (rectangular paralleled), we have three rectangles types that represent the six faces.
We can declare one method that takes any two sides and calculates their area. Such a method would be declared as follows:
double Area(Double Side1, Double Side2);
After declaring it in the public section, we would define it as follows:

In the Display() method, we could display the area of the length by the height using:

As you can see, the Side1 and Side2 arguments are not modified; therefore, they should be declared as constants. To declare each as constant, we would change the declaration of the method as follows:

And we would define it as follows:

For an example such as this one, the const keyword can be typed before or after the data type, the result would be the same. If a method is receiving more than one argument, their constancy is independent: the fact that one of them needs to be constant does not imply anything about the other(s).

To help the method specify what side it is calculating the area, we could provide a string that names the side. We could declare the method as follows:

We would then define the new method as follows:

In the Display() method, we could specify the name of the side and provide the corresponding measures as follows:

Here is the complete implementation:

Some of the method members of an object, though using member variables of the same object, do not modify them. To make sure that such a method does not alter the value of a member variable, the method should be declared and implemented as constant.

To declare a method as a constant, add the const keyword to the right side of the method when declaring it. Here is an example:

The CalcVolume() and the CalcShoeSize() methods do not modify the member variables they receive. You can reinforce this by declaring them as const:

double CalcVolume() const;float CalcShoeSize() const;

When implementing the method, type the const keyword on the right side of the method?s closing parenthesis:

If you decide to define constant methods locally (inline), the only difference is to remove the semi-colon of the end of the declaration and define the method normally.

Notice that the Display() method assigns values to the member variables, which means it alters their values. Therefore, it cannot be declared or defined as constant. Here is the new version of our ShoeBox object: