-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfullreview3.cpp
More file actions
62 lines (44 loc) · 1.4 KB
/
fullreview3.cpp
File metadata and controls
62 lines (44 loc) · 1.4 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
#include <iostream>
using namespace std;
// Polymorpism allows you to treat subclasses as their superclass and yet
// call the correct overwritten methods in the subclass automatically
class Animal{
public:
virtual void makeSound(){ cout << "The Animal says grrrr" << endl; }
// The Animal class could be a capability class that exists
// only to be derived from by containing only virtual methods
// that do nothing
};
class Cat : public Animal{
public:
void makeSound(){ cout << "The Cat says meow" << endl; }
};
class Dog : public Animal{
public:
void makeSound(){ cout << "The Dog says woof" << endl; }
};
// An abstract data type is a class that acts as the base to other classes
// They stand out because its methods are initialized with zero
// A pure virtual method must be overwritten by subclasses
class Car{
public :
virtual int getNumWheels() = 0;
virtual int getNumDoors() = 0;
};
class StationWagon : public Car{
public :
int getNumWheels() { cout << "Station Wagon has 4 Wheels" << endl; }
int getNumDoors() { cout << "Station Wagon has 4 Doors" << endl; }
StationWagon() { }
~StationWagon();
};
int main(){
Animal* pCat = new Cat;
Animal* pDog = new Dog;
pCat -> makeSound();
pDog -> makeSound();
// Create a StationWagon using the abstract data type Car
Car* stationWagon = new StationWagon();
stationWagon -> getNumWheels();
return 0;
}