-
Notifications
You must be signed in to change notification settings - Fork 0
/
tutorialpb.cpp
71 lines (52 loc) · 1.68 KB
/
tutorialpb.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
using namespace std;
// Virtual Methods and Polymorphism
// Polymorpism allows you to treat subclasses as their superclass and yet
// call the correct overwritten methods in the subclass automatically
class Animal{
public:
void getFamily() { cout << "We are Animals" << endl; }
// When we define a method as virtual we know that Animal
// will be a base class that may have this method overwritten
virtual void getClass() { cout << "I'm an Animal" << endl; }
};
class Dog : public Animal{
public:
void getClass() { cout << "I'm a Dog" << endl; }
};
class Stuff : public An{
}
class GermanShepard : public Dog{
public:
void getClass() { cout << "I'm a German Shepard" << endl; }
void getDerived() { cout << "I'm an Animal and Dog" << endl; }
};
void whatClassAreYou(Animal *animal){
animal -> getClass();
}
int main(){
Animal *animal = new Animal;
Dog *dog = new Dog;
// If a method is marked virtual or not doesn't matter if we call the method
// directly from the object
animal->getClass();
dog->getClass();
// If getClass is not marked as virtual outside functions won't look for
// overwritten methods in subclasses however
whatClassAreYou(animal);
whatClassAreYou(dog);
Dog spot;
GermanShepard max;
// A base class can call derived class methods as long as they exist
// in the base class
Animal* ptrDog = &spot;
Animal* ptrGShepard = &max;
// Call the method not overwritten in the super class Animal
ptrDog -> getFamily();
// Since getClass was overwritten in Dog call the Dog version
ptrDog -> getClass();
// Call to the super class
ptrGShepard -> getFamily();
// Call to the overwritten GermanShepard version
ptrGShepard -> getClass();
return 0;
}