< previous page page_182 next page >

Page 182
LISTING 12.2 continued
d5ef64f4d3250b96ba5c07ca5bbc2f56.gif
44:      cout << Frisky.GetAge() <<  years old\n;
45:      int age = 5;
46:      Frisky.SetAge(age);
47:      cout << Frisky is ;
48:      cout << Frisky.GetAge() <<  years old\n;
49:      cout << Calling FunctionTwo\n;
50:      FunctionTwo(&Frisky);
51:      cout << Frisky is ;
52:      cout << Frisky.GetAge() <<  years old\n;
53:      return 0;
54:   }
55:
56:   // functionTwo, passes a const pointer
57:   const SimpleCat * const
58:      FunctionTwo (const SimpleCat * const theCat)
59:   {
60:      cout << Function Two. Returning\n;
61:      cout << Frisky is now  << theCat->GetAge();
62:      cout <<  years old \n;
63:      // theCat->SetAge(8);   const!
64:      return theCat;
65:   }

Output:
Making a cat
Simple Cat Constructor
Frisky is 1 years old
Frisky is 5 years old
Calling FunctionTwo
Function Two. Returning
Frisky is now 5 years old
Frisky is 5 years old
Simple Cat Destructor
Analysis: SimpleCat has added two accessor functions: GetAge() on line 12, which is a const function; and SetAge() on line 13, which is not. It has also added the member variable itsAge on line 16.
The constructor, copy constructor, and destructor are still defined to print their messages. The copy constructor is never called, however, because the object is passed by reference; and so no copies are made. On line 41, an object is created, and its default age is printed on lines 42 and 43.
On line 45, itsAge is set using the accessor SetAge, and the result is printed on lines 46 and 47. FunctionOne() is not used in this program, but FunctionTwo() is called. FunctionTwo() has changed slightly; the parameter and return value are now declared, on lines 36 and 37, to take a constant pointer to a constant object and to return a constant pointer to a constant object.

 
< previous page page_182 next page >

If you like this book, buy it!