Sams Teach Yourself C++ in 21 Days

(singke) #1
21: Cat::Cat()
22: {
23: itsAge = new int;
24: itsWeight = new int;
25: *itsAge = 5;
26: *itsWeight = 9;
27: }
28:
29: Cat::Cat(const Cat & rhs)
30: {
31: itsAge = new int;
32: itsWeight = new int;
33: *itsAge = rhs.GetAge(); // public access
34: *itsWeight = *(rhs.itsWeight); // private access
35: }
36:
37: Cat::~Cat()
38: {
39: delete itsAge;
40: itsAge = 0;
41: delete itsWeight;
42: itsWeight = 0;
43: }
44:
45: int main()
46: {
47: Cat Frisky;
48: cout << “Frisky’s age: “ << Frisky.GetAge() << endl;
49: cout << “Setting Frisky to 6...\n”;
50: Frisky.SetAge(6);
51: cout << “Creating Boots from Frisky\n”;
52: Cat Boots(Frisky);
53: cout << “Frisky’s age: “ << Frisky.GetAge() << endl;
54: cout << “Boots’ age: “ << Boots.GetAge() << endl;
55: cout << “setting Frisky to 7...\n”;
56: Frisky.SetAge(7);
57: cout << “Frisky’s age: “ << Frisky.GetAge() << endl;
58: cout << “boot’s age: “ << Boots.GetAge() << endl;
59: return 0;
60: }

Frisky’s age: 5
Setting Frisky to 6...
Creating Boots from Frisky
Frisky’s age: 6
Boots’ age: 6
setting Frisky to 7...
Frisky’s age: 7
Boots’ age: 6

OUTPUT


300 Day 10


LISTING10.5 continued
Free download pdf