|

As
you’ve seen, an accessor function provides a public
interface to the private member data of the class.
Each accessor function, along with any other class
methods that you declare, must have an implementation.
The implementation is called the function definition.
A
member function definition begins with the name
of the class, followed by two colons, the name of
the function, and its parameters. Listing 6.3 shows
the complete declaration of a simple Cat class and
the implementation of its accessor function and
one general class member function.
Listing
6.3. Implementing the methods of a simple class.
1:
// Demonstrates declaration of a class and
2:
// definition of class methods,
3:
4:
#include <iostream.h> // for cout
5:
6:
class Cat // begin declaration of the class
7:
{
8:
public: // begin public section
9:
int GetAge(); // accessor function
10:
void SetAge (int age); // accessor function
11:
void Meow(); // general function
12:
private: // begin private section
13:
int itsAge; // member variable
14:
};
15:
16:
// GetAge, Public accessor function
17:
// returns value of itsAge member
18:
int Cat::GetAge()
19:
{
20:
return itsAge;
21:
}
22:
23:
// definition of SetAge, public
24:
// accessor function
25:
// returns sets itsAge member
26:
void Cat::SetAge(int age)
27:
{
28:
// set member variable its age to
29:
// value passed in by parameter age
30:
itsAge = age;
31:
}
32:
33:
// definition of Meow method
34:
// returns: void
35:
// parameters: None
36:
// action: Prints "meow" to screen
37:
void Cat::Meow()
38:
{
39:
cout << "Meow.\n";
40:
}
41:
42:
// create a cat, set its age, have it
43:
// meow, tell us its age, then meow again.
44:
int main()
45:
{
46:
Cat Frisky;
47:
Frisky.SetAge(5);
48:
Frisky.Meow();
49:
cout << "Frisky is a cat who is " ;
50:
cout << Frisky.GetAge() << " years old.\n";
51:
Frisky.Meow();
52;
return 0;
53:
}
Output:
Meow.
Frisky is a cat who is 5 years old.
Meow.
Analysis: Lines 6-14 contain the definition
of the Cat class. Line 8 contains the keyword public,
which tells the compiler that what follows is a
set of public members. Line 9 has the declaration
of the public accessor method GetAge(). GetAge()
provides access to the private member variable itsAge,
which is declared in line 13. Line 10 has the public
accessor function SetAge(). SetAge() takes an integer
as an argument and sets itsAge to the value of that
argument.
Line 11 has the declaration of the class method
Meow(). Meow() is not an accessor function. Here
it is a general method that prints the word Meow
to the screen.
Line
12 begins the private section, which includes only
the declaration in line 13 of the private member
variable itsAge. The class declaration ends with
a closing brace and semicolon in line 14.
Lines
18-21 contain the definition of the member function
GetAge(). This method takes no parameters; it returns
an integer. Note that class methods include the
class name followed by two colons and the function
name (Line 18). This syntax tells the compiler that
the GetAge() function that you are defining here
is the one that you declared in the Cat class. With
the exception of this header line, the GetAge()
function is created like any other function.
The
GetAge() function takes only one line; it returns
the value in itsAge. Note that the main() function
cannot access itsAge because itsAge is private to
the Cat class. The main() function has access to
the public method GetAge(). Because GetAge() is
a member function of the Cat class, it has full
access to the itsAge variable. This access enables
GetAge() to return the value of itsAge to main().
Line
26 contains the definition of the SetAge() member
function. It takes an integer parameter and sets
the value of itsAge to the value of that parameter
in line 30. Because it is a member of the Cat class,
SetAge() has direct access to the member variable
itsAge.
Line
37 begins the definition, or implementation, of
the Meow() method of the Cat class. It is a one-line
function that prints the word Meow to the screen,
followed by a new line. Remember that the \n character
prints a new line to the screen.
Line
44 begins the body of the program with the familiar
main() function. In this case, it takes no arguments
and returns void. In line 46, main() declares a
Cat named Frisky. In line 47, the value 5 is assigned
to the itsAge member variable by way of the SetAge()
accessor method. Note that the method is called
by using the class name (Frisky) followed by the
member operator (.) and the method name (SetAge()).
In this same way, you can call any of the other
methods in a class.
Line
48 calls the Meow() member function, and line 49
prints a message using the GetAge() accessor. Line
51 calls Meow() again.
|