-
Notifications
You must be signed in to change notification settings - Fork 0
/
simpleinherit.cpp
71 lines (60 loc) · 1.67 KB
/
simpleinherit.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
* =====================================================================================
*
* Filename: simpleinherit.cpp
*
* Description: Shows inheritance
*
* Version: 1.0
* Created: 19/01/13 10:33:45
* Revision: none
* Compiler: gcc
*
* Author: Robert Halliday (rh), [email protected]
* Company: rphhpr
*
* =====================================================================================
*/
#include <iostream>
enum BREED { YORKIE, CAIRN, DANDIE, SHETLAND, DOBERMAN, LAB };
class Mammal
{
public:
// constructors
Mammal():itsAge(2), itsWeight(5){}
~Mammal(){}
// accessors
int GetAge() const {return itsAge;}
void SetAge(int age) {itsAge = age; }
int GetWeight() const { return itsWeight; }
void SetWeight(int weight) { itsWeight = weight; }
// Other methods
void Speak() const { std::cout << "Mammal sound!\n"; };
void Sleep() const { std::cout << "shhh. I'm sleeping.\n"; }
protected:
int itsAge;
int itsWeight;
};
class Dog : public Mammal
{
public:
// Constructors
Dog():itsBreed(YORKIE){}
~Dog(){}
// Accessors
BREED GetBread() const { return itsBreed; }
void SetBreed(BREED breed) { itsBreed = breed; }
// Other methods
void WagTail() {std::cout << "Tail wagging...\n"; }
void BegForFood() {std::cout << "Begging for food...\n"; }
private:
BREED itsBreed;
};
int main()
{
Dog fido;
fido.Speak();
fido.WagTail();
std::cout << "Fido is " << fido.GetAge() << " years old\n";
return 0;
}