14: int GetWeight() const { return *itsWeight; }
15: void setWeight (int weight) { *itsWeight = weight; }
16:
17: private:
18: int * itsAge;
19: int * itsWeight;
20: };
21:
22: SimpleCat::SimpleCat()
23: {
24: itsAge = new int(2);
25: itsWeight = new int(5);
26: }
27:
28: SimpleCat::~SimpleCat()
29: {
30: delete itsAge;
31: delete itsWeight;
32: }
33:
34: int main()
35: {
36: using namespace std;
37: SimpleCat *Frisky = new SimpleCat;
38: cout << “Frisky is “ << Frisky->GetAge()
39: << “ years old “ << endl;
40: Frisky->SetAge(5);
41: cout << “Frisky is “ << Frisky->GetAge()
42: << “ years old “ << endl;
43: delete Frisky;
44: return 0;
45: }
Frisky is 2 years old
Frisky is 5 years old
The class SimpleCatis declared to have two member variables—both of which
are pointers to integers—on lines 18 and 19. The constructor (lines 22–26) ini-
tializes the pointers to memory on the free store and to the default values.
Notice on lines 24 and 25 that a pseudoconstructor is called on the new integer, passing
in the value for the integer. This creates an integer on the heap and initializes its value
(on line 24 to the value 2 and on line 25 to the value 5 ).
OUTPUT
242 Day 8
LISTING8.7 continued
ANALYSIS