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:
1 | Double Area(); |
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:
1 2 3 4 | double ShoeBox::Area(Double Side1, Double Side2) { return Side1 * Side2; } |
In the Display() method, we could display the area of the length by the height using:
1 | cout << ?\n\tArea = ? << Area(Length, Height); |
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:
1 | Double Area(const Double Side1, const Double Side2); |
And we would define it as follows:
1 2 3 4 | double ShoeBox::Area(const Double Side1, const Double Side2) { return Side1 * Side2; } |
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:
1 | void Area(const Double Side1, const Double Side2, PChar SideName); |
We would then define the new method as follows:
1 2 3 4 | void ShoeBox::Area(const Double S1, const Double S2, PChar n) { cout << "\nThe area of the " << n << " side is: " << S1 * S2; } |
In the Display() method, we could specify the name of the side and provide the corresponding measures as follows:
1 | Area(Length, Height, "front");Area(Length, Width, "top");Area(Width, Height, "left"); |
Here is the complete implementation:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | #include <iostream> #include <string> using namespace std; class ShoeBox { public: double CalcVolume() { return Length * Width * Height; } void Area(const double Side1, const double Side2, char SideName[]); float CalcShoeSize() { return Length - 0.35; } void Display(); private: double Length; double Width; double Height; char Color[32]; }; void ShoeBox::Area(const double S1, const double S2, char N[]) { cout << "\nThe area of the " << N << " side is: " << S1 * S2; } void ShoeBox::Display() { // Initializing the dimensions Length = 10.42; Width = 5.28; Height = 5.88; Color = "Yellow Stone"; // Display the characteristics of the shoe box cout << "Characteristics of this shoe box"; cout << "\nDimensions(L*H*W) = " << Length << " * " << Height << " * " << Width; Area(Length, Height, "front"); Area(Length, Width, "top"); Area(Height, Width, "left"); cout << "\n\tVolume = " << CalcVolume() << "\n\tColor = " << Color << "\n\tSize = " << CalcShoeSize(); } int main() { ShoeBox Bambou; Bambou.Display(); return 0; } Constant Methods |
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:
1 | string SpecFullName() const; |
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:
1 2 3 4 5 6 | double ShoeBox::CalcVolume() const { return Length * Width * Height; } float ShoeBox::CalcShoeSize() const { return Length - 0.35; } |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | class ShoeBox { public: double CalcVolume() const { return Length * Width * Height; } void Area(const double Side1, const double Side2, char SideName[]); float CalcShoeSize() const { return Length - 0.35; } void Display(); private: double Length; double Width; double Height; string Color; }; The program would still produce the same result. |