|
|
 |
|
|
|
|
17: void SetWeight(int weight) { itsWeight = weight; }
18:
19: //Other methods
20: void Speak()const { cout << Mammal sound!\n; }
21: void Sleep()const { cout << shhh. I'm sleeping.\n; }
22:
23:
24: protected:
25: int itsAge;
26: int itsWeight;
27: };
28:
29: class Dog : public Mammal
30: {
31: public:
32:
33: // Constructors
34: Dog():itsBreed(YORKIE){}
35: Dog(){}
36:
37: // Accessors
38: BREED GetBreed() const { return itsBreed; }
39: void SetBreed(BREED breed) { itsBreed = breed; }
40:
41: // Other methods
42: void WagTail()const { cout << Tail wagging\n; }
43: void BegForFood()const { cout << Begging for food\n; }
44:
45: private:
46: BREED itsBreed;
47: };
48:
49: int main()
50: {
51: Dog fido;
52: fido.Speak();
53: fido.WagTail();
54: cout << Fido is << fido.GetAge() << years old\n;
55: return 0;
56: } |
|
|
|
|
|
|
|
|
Mammal sound!
Tail wagging
Fido is 2 years old |
|
|
|
|
|
|
|
|
Analysis: On lines 627, the Mammal class is declared (all its functions are inline to save space here). On lines 2947, the Dog class is declared as a derived class of Mammal. Thus, by these declarations, all Dogs have an age, a weight, and a breed. |
|
|
|
|
|