Classes and Member Functions
The primary motivation of using classes in a program is to create objects as complete as possible. An object must be able to handle its own business so that the other objects of the program or of another program would only need to know which object can take care of a particular need they have.
A regular variable, as a member of an object, cannot handle assignments; this job is handled by particular functions declared as members of a class. A function as a member of a class is also called a Method. In this book, the words ?method? and ?function?, when associated with a class, will refer to the same thing: a member function of the class.
Declaring Member Functions
A member function is declared like any of the functions we have used so far; it could or could not return a value.
The shoe box we have been using so far needs a volume that would be used to determine what size can fit in the box. Therefore, we will use a member function that can perform that calculation. Our object, when including methods could be structured as follows:
1 2 3 4 5 6 7 8 9 10 | class ShoeBox { public: double Length; double Width; double Height; string Color; float ObtainShoeSize(); double CalcVolume(); private: }; |
When using functions on a class, the variables are used to hold or store values, called data, of the object, while member functions are used to perform assignments as related to the objects. One way you can control the data held by variables is to hide data from the “external world”. To achieve this, you should declare the member variables in the private section. After doing this, use the methods in the public section to help the class interact with the other objects or functions of the program. At this time, our ShoeBox object would look like this:
1 2 3 4 5 6 7 8 9 10 | class ShoeBox { public: float ObtainShoeSize(); double CalcVolume(); string Color; private: double Length; double Width; double Height; }; |
There are at least two techniques you can use to implement a method member.
Implementing Member Functions Locally
To implement a method in the class where it is declared, use the same techniques we used to define regular functions. When a method is a class’ member, it has access to the member variables of the same class; this means you do not need to pass the variables as arguments (there are cases when you will need to); you can just use any of them as if it were supplied. Here is how you would define the CalcVolume() method inside of the ShoeBox class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | #include <iostream> using namespace std; class ShoeBox { public: float ObtainShoeSize(); double CalcVolume() { return (Length * Width * Height); } string Color; private: double Length; double Width; double Height; }; |
If your class has a lot of methods, this technique could be cumbersome. You should use it only for small methods.
Implementing Member Functions Globally
When the methods execute long assignments, you should implement them outside of the object by first accessing the desired function member of the class. To access a method of a class when implementing it, instead of the member access operator ?.?, you will use the scope resolution operator represented as two colons ::
To implement a method outside of the class, type the return value of the method, followed by the class’ name, followed by the scope resolution operator ?::?, followed by the method’s name, followed by the arguments, if any, between parentheses, and finally define what the function should do, in its body.
Another implementation of our CalcVolume() method would be:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | class ShoeBox <iostream> { public: float ObtainShoeSize(); double CalcVolume(); string Color; private: double Length; double Width; double Height; }; double ShoeBox::CalcVolume() { return (Length * Width * Height); } |
Inline Methods
When studying functions, we learned that an assignment can be carried where it is being called. The same process can apply to a class? member.
To declare a class’ method as inline, precede its name with the inline keyword when declaring the method in the class:
1 2 3 4 5 6 7 8 | class ShoeBox { public: inline Double CalcVolume(); float CalcShoeSize(); void Display(); private: . . . }; |
You can choose which methods would be inline and which ones would not. When implementing the method, you can precede the method with the inline keyword. You can also omit the inline keyword in the class but use it when defining the method.
If you decide to implement a method locally (in the class), you have the option of implementing it as inline:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | class ShoeBox { public: inline double CalcVolume() { return Length * Width * Height; } inline float CalcShoeSize() { return Length - 0.35; } void Display(); private: double Length; double Width; double Height; string Color; }; |
On the other hand, if you omit the inline keyword, the C++ compiler would take care of it. Normally, any function implemented in the body of the class is considered inline.