Member functions
Member functions were discussed in some detail in the functions tutorial. By definition,
a member function is a function of a class or object that is accessed by the dot operator.
They are declared in the public section of the class and are written just like regular functions.
Just like before, the Rect class has two member functions called Area() and Perimeter(). These
functions are accessed with the dot operator outside the class (here, in then main() function)
and preform appropriate calculations.
Thinking about a rectangle, let's add another member function called triple() which will simply
triple both sides of the rectangle. This is just for you to see how member functions work.
Operator overloads
In C++, there are hundreds of definitions of operators relating to addition, subtraction,
division, logic, pointers etc... Now, let's say that we have two objects of type Rect and
we wanted to copy the values from one rectangle to another. WHOOPS! ERROR! C++ does not
know what a Rect is since it is not a primitive data type. No worries, we can do what is
called operator overloading where we can define our own
operator action. Below is what I was referring to:
Here is the definition of an operator overload:
where in the above, type is the return type of the operator, sign is the appropriate sign
related to the operator ( +, -, * etc...) and parameters are the right hand side of the
operator.
Now that we know how to write the overload of the operator, let's actually overload
the assignment operator ( = ). Add this code after all the member functions in the class:
The reason the parameter is constant is that we are not actually changing the value on the
right hand side. We are just getting them so we can place them in the left hand side (which
relating to an above snippet would be the Rect r object).
Now, add this to the main function at the end:
The above snippets will compile and work when used. This now shows how to assign values.
But what if we wanted to add two rectangles together? Simple, overload the addition
operator. Again, place this snippet after the member functions:
This makes use of the member function Perimeter which is called twice, making use of the current
sides of the class and the other rectangles sides. The return type is int because you are returning
the sum of the sides.
Finally, here is the complete class with all everything from both tutorials:
Download source code here (Right click - Save Tagret As...)
This keyword
The keyword this represents a pointer to the object whose member
function is being executed. It is a pointer to the object itself.
As good use of the this keyword is to check if a parameter is a pointer to the object itself.
Below is a possible function for the Rect class above:
The above function will check if the address of the parameter r is the same as the
address of the current object (represented by this).
Another use of the this keyword is for the assignment operator overload. We could have
rewritten the Rect operator= like this:
Notice that the return type is a reference to the Rect class and not void as before.
The parameter is also constant since again we are nopt changing the right hand side.