|
|
|
|
|
|
|
complete declaration of a simple Cat class and the implementation of its accessor function and one general class member function. |
|
|
|
|
|
|
|
|
LISTING 6.1 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: {
|
|
|
|
|
|