The Date class has a member function named display. This function corresponds to the display_date function in C, which prints the contents of a date structure. However, notice the following differences.
First, consider the way the function is declared and defined. The function's prototype appears inside the declaration of Date, and when the function is defined, it is called Date::display(). This indicates that it is a member of the class and that its name has “class scope.” You could declare another function named display outside the class, or in another class, without any conflict. The class name (combined with ::, the scope resolution operator) prevents any confusion between the definitions.
You can also overload a member function, just like any other function in C++, as long as each version is distinguishable by its parameter list. All you have to do is declare each member function's prototype in the class declaration, and prefix its name with the class name and :: when defining it.
Now compare the implementation of the display member function with that of the corresponding function in C, display_date. The C function refers to dt.month, dt.day, and dt.year. In contrast, the C++ member function refers to month, day, and year; no object is specified. Those data members belong to the object that the function was called for. For example:
myDate.display();
yourDate.display();
The first time display is called, it uses the data members of myDate. The second time it's called, it uses the members of yourDate. A member function automatically uses the data members of the “current” object, the object to which it belongs.
You can also call a member function through a pointer to an object, using the -> operator. For example:
Date myDate( 3, 12, 1985 );
Date *datePtr = &myDate;
datePtr->display();
This code declares a pointer to a Date object and calls display through that pointer.
You can even call a member function through a reference to an object. For example:
Date myDate( 3, 12, 1985 );
Date &otherDate = myDate;
otherDate.display();
This code calls display through the reference variable otherDate. Because otherDate is an alias for myDate, the contents of myDate are displayed.
These techniques for calling a member function work only if the function is declared public. If a member function is declared private, only other member functions within the same class can call it. For example:
class Date
{
public:
void display(); // Public member function
// ...
private:
int daysSoFar(); // Private member function
// ...
};
// --------- Display date in form "DDD YYYY"
void Date::display()
{
cout << daysSoFar() // Call private member function
<< " " << year;
}
// -------- Compute number of days elapsed
void Date::daysSoFar()
{
int total = 0;
static int length[] = { 0, 31, 28, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31 };
for( int i = 1; i month; i++ )
total += length[i];
total += day;
return total;
}
Notice that display calls daysSoFar directly, without preceding it with an object name. A member function can use data members and other member functions without specifying an object. In either case, the “current” object is used implicitly.