-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest1.cpp
88 lines (70 loc) · 2.03 KB
/
test1.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#if 0
// cut and paste this to a 'makefile'
// cut start ** cut start ** cut start ** cut start ** cut start **
# 'makefile' to
# 1. compile and build test1
# 2. compile again with VIRTUAL defined to make test1-virtual
all : test1 test1-virtual
test1: test1.cpp
g++ test1.cpp -o test1
test1-virtual: test1.cpp
g++ -DVIRTUAL test1.cpp -o test1-virtual
// cut stop ** cut stop ** cut stop ** cut stop ** cut stop **
#endif
#include <iostream>
class Leg {
public:
double legLength; // meters (m)
double longestClawLength; // meters (m)
bool missingClaw; // yes/no
};
class CatFamily { // base class
private:
// properties of a cat
bool hungrey; // yes/no
double tailLength; // meters (m)
double weight; // kilograms (kg)
double earLength; // meters (m)
int legCount; // most cats have 4 legs, some cats have 3 legs
Leg *legTable;
void setupLegs(int legs) { legCount = legs; legTable = new Leg[legs]; } // some cats are missing a leg
public:
CatFamily(int legs = 4) { setupLegs(legs); } // most have 4 legs.
~CatFamily() {delete [] legTable; }
#ifdef VIRTUAL
virtual void meow() { std::cout << "who called the base class?\n"; }
#else
void meow() { std::cout << "who called the base class?\n"; }
#endif
};
class Cat : public CatFamily { // derived class
public:
void meow() { std::cout << "regular cat! meow!\n"; }
};
class Tiger : public CatFamily { // derived class
public:
void meow() { std::cout << "tiger! MREOWWW!\n"; }
};
class Cougar : public CatFamily { // derived class
public:
void meow() { std::cout << "cougar! rrrrrrrrrrrrr!-hiss!\n"; }
};
void speak(CatFamily *cat) {
cat->meow();
}
int main(int argc, char **argv) {
#ifdef VIRTUAL
std::cout <<"main: compiled with 'VIRTUAL' defined\n";
#else
std::cout <<"main: compiled without 'VIRTUAL' defined\n";
#endif
CatFamily catFamily;
Cat cat;
Tiger tiger;
Cougar cougar;
speak(&catFamily);
speak(&cat);
speak(&tiger);
speak(&cougar);
return 0;
}