forked from wadehuber/codeexamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanimals.cpp
More file actions
126 lines (100 loc) · 3.06 KB
/
Copy pathanimals.cpp
File metadata and controls
126 lines (100 loc) · 3.06 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include<iostream>
#include<string>
using namespace std;
class Animal {
protected:
string name;
public:
Animal(string s="Noname") : name(s) { } // Default value = "Noname"
// Pure virtual method - must be overridden by non-abstract derived class
virtual void speak() = 0;
// Virtual method - can be overridden by the child class. The type of the
// *OBJECT* being pointed to determines the method that gets called.
virtual void move() { cout << " ANIMAL " << name << " : I'm moving" << endl; }
// Non-virtual method - can be overridden by the child class. The type
// of the *POINTER* being used determines the method that gets called.
void eat() { cout << " ANIMAL " << name << " : I'm hungry!" << endl; }
};
class Dog : public Animal {
public:
Dog(string s="GoodBoy") : Animal(s) {}
void speak() override { cout << " DOG " << name << " : Woof!" << endl; }
void move() override { cout << " DOG " << name << " : I'm running!" << endl; }
void fetch() { cout << " DOG " << name << " : Ball! Ball!" << endl; }
};
class Cat : public Animal {
public:
Cat(string s="Kitty") : Animal(s) {}
void speak() override { cout << " CAT " << name << " : Meow!" << endl; }
virtual void move() override { cout << " CAT " << name << " : Later." << endl; }
};
int main() {
/* // Can't instantiate an Animal object
Animal a;
Animal * aPtr;
Animal & aRef = a;
*/
Animal * adPtr;
Dog d("Spot");
Dog * dPtr;
Dog & dRef = d;
Cat c("Eek");
Cat * cPtr;
Cat & cRef = c;
Animal & acRef = c;
/* // Can't instantiate an Animal object
cout << endl << "Animal object: " << endl;
a.speak();
a.move();
a.eat();
cout << endl << "Animal pointer to an Animal object: " << endl;
aPtr = new Animal("Dale");
aPtr->speak();
aPtr->move();
aPtr->eat();
cout << endl << "Animal reference to an animal object: " << endl;
aRef.speak();
aRef.move();
aRef.eat();
*/
cout << endl << "Dog object: " << endl;
d.speak();
d.move();
d.eat();
d.fetch();
cout << endl << "Dog pointer to an Dog object: " << endl;
dPtr = new Dog("Felix");
dPtr->speak();
dPtr->move();
dPtr->eat();
dPtr->fetch();
cout << endl << "Animal pointer to an Dog object: " << endl;
adPtr = &d;
adPtr->speak();
adPtr->move();
adPtr->eat();
// adPtr->fetch(); // Animal class has no 'fetch' member
cout << endl << "Dog reference to an dog object: " << endl;
dRef.speak();
dRef.move();
dRef.eat();
dRef.fetch();
cout << endl << "Cat object: " << endl;
c.speak();
c.move();
c.eat();
cout << endl << "Cat pointer to an Cat object: " << endl;
cPtr = new Cat("Mittens");
cPtr->speak();
cPtr->move();
cPtr->eat();
cout << endl << "Cat reference to an cat object: " << endl;
cRef.speak();
cRef.move();
cRef.eat();
cout << endl << "Animal reference to an cat object: " << endl;
acRef.speak();
acRef.move();
acRef.eat();
return 0;
}