1#include <cstdlib>
2#include <ctime>
3#include <iostream>
4#include <vector>
5
6class Parent {
7public:
8 virtual void sayHi()
9 {
10 std::cout << "Parent here!" << std::endl;
11 }
12};
13
14class Child : public Parent {
15public:
16 void sayHi()
17 {
18 std::cout << "Child here!" << std::endl;
19 }
20};
21
22class DifferentChild : public Parent {
23public:
24 void sayHi()
25 {
26 std::cout << "DifferentChild here!" << std::endl;
27 }
28};
29
30int main()
31{
32 std::vector<Parent*> parents;
33
34 // Add 10 random children
35 srand(time(NULL));
36 for (int i = 0; i < 10; ++i) {
37 int child = rand() % 2; // random number 0-1
38 if (child) // 1
39 parents.push_back(new Child);
40 else
41 parents.push_back(new DifferentChild);
42 }
43
44 // Call sayHi() for each type! (example of polymorphism)
45 for (const auto& child : parents) {
46 child->sayHi();
47 }
48
49 return 0;
50}