Class Members Interactions
Regardless of how the member methods of an object are implemented, any method can call another without using an access operator. This allows an object?s methods to exchange information among themselves easily. Furthermore, unlike regular functions where a function must be declared prior to another function calling it, the method members of a class do not abide by that rule: one method can call another method before or after the other has been implemented, as long as it is defined somewhere.
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 | #include <iostream> #include <string> using namespace std; class ShoeBox { public: ... }; void ShoeBox::Display() { // Initializing the dimensions Length = 12.55; Width = 6.32; Height = 8.74; Color = "Early Breeze"; // Display the characteristics of the shoe box cout << "Characteristics of this shoe box"; cout << "\n\tLength = " << Length << "\n\tWidth = " << Width << "\n\tHeight = " << Height << "\n\tVolume = " << CalcVolume() << "\n\tColor = " << Color << "\n\tSize = " << CalcShoeSize(); } duble ShoeBox::CalcVolume() { return Length * Width * Height; } |
Once an object is defined and behaves as complete as possible, the other function or objects of the program can make the appropriate calls trusting that the called object can handle its assignments efficiently. This ability allows you to (tremendously) reduce the work overload of the other components of a program.
The main() function can simply call the appropriate member of the ShoeBox object now:
1 2 3 4 5 6 7 | int main() { ShoeBox Sample; Sample.Display(); return 0; } |