-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathweek3-app2.cpp
80 lines (58 loc) · 1.67 KB
/
week3-app2.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
// OOP:
// inheritance
// dynamic polymorphism / virtual functions vs RTTI
// move-ctor, move-assignment (delayed)
// functional programming:
// - overloading
// (all the operators are implemented as functions and they also can ve overloaded)
// Generics / Templates:
// function, class, variable, type templates
#include <iostream>
#include <string>
using namespace std;
namespace Utils {
int VALUE = 123;
}
struct Animal
{
string name;
Animal() { }
Animal(const string& name) : name(name) { };
// Animal(const Animal& other) : name(other.name) { }
// ~Animal() { }
virtual void whoAmI() { // this is the 0th index in the vtable
cout << "I am an arbitrary animal!" << endl;
}
};
struct Dog : Animal {
// string name;
string breed;
// Dog() { }
Dog(const string& name, const string& breed = "")
: Animal(name), breed(breed) { };
void whoAmI() override {
cout << "I am a dog!" << endl;
}
};
int main(int argc, char* argv[])
{
using Utils::VALUE;
cout << VALUE << endl;
auto animal = Animal("Elephant"); // const char[9]
auto dog = Dog("Poppy", "Rotweiler");
animal.whoAmI();
dog.whoAmI();
// Animal* animalptr;
// typedef Animal* AnimalPtr;
using AnimalPtr = Animal*; // type aliasing
AnimalPtr animals[] = { &animal, &dog, &animal, &dog };
cout << "Size of Animal = " << sizeof(Animal) << endl;
cout << "Size of Dog = " << sizeof(Dog) << endl;
cout << sizeof(animals) << endl;
cout << sizeof(AnimalPtr) << endl;
const auto size = sizeof(animals) / sizeof(AnimalPtr);
for(auto i=0; i<size; i++) {
animals[i]->whoAmI();
}
return 0;
}