fork(4) download
  1. // If you are not sure what some lines of code do, try looking back at
  2. // previous example programs, notes, or ask a question.
  3.  
  4. #include <iostream>
  5. #include <vector>
  6.  
  7. using namespace std;
  8.  
  9. // Base class
  10. class animal {
  11. public:
  12. animal();
  13. // You're usually going to want to make destructors virtual so the specific one will be called before the general one
  14. virtual ~animal();
  15.  
  16. // Speak is virtual so subclasses can override it
  17. virtual void speak() const;
  18.  
  19. // Inherited classes will have access to this data
  20. protected:
  21. char name[10]; // Example data
  22.  
  23. // Inherited classes will NOT have access to private data
  24. private:
  25. int ID; // Example data
  26. };
  27.  
  28. // 1st inherited class
  29. class dog : public animal {
  30. public:
  31. // Normal constructor/destructor
  32. dog();
  33. // This should not be a base class, so we don't make this virtual
  34. ~dog();
  35.  
  36. void speak() const;
  37.  
  38. // Dog can have its own private data
  39. private:
  40. char color;
  41. };
  42.  
  43. // 2nd inherited class
  44. class cat : public animal {
  45. public:
  46. // Normal constructor/destructor
  47. cat();
  48. // This should not be a base class, so we don't make this virtual
  49. ~cat();
  50.  
  51. void speak() const;
  52.  
  53. // Cat can have its own private data
  54. private:
  55. // Some data
  56. };
  57.  
  58. animal::animal() {
  59. cout << "animal constructor" << endl;
  60. }
  61.  
  62. animal::~animal() {
  63. cout << "animal destructor" << endl;
  64. }
  65.  
  66. void animal::speak() const {
  67. cout << "some animal sound" << endl;
  68. }
  69.  
  70. dog::dog() {
  71. cout << "dog constructor" << endl;
  72. }
  73.  
  74. dog::~dog() {
  75. cout << "dog destructor" << endl;
  76. }
  77.  
  78. void dog::speak() const {
  79. cout << "bark!" << endl;
  80. }
  81.  
  82. cat::cat() {
  83. cout << "cat constructor" << endl;
  84. }
  85.  
  86. cat::~cat() {
  87. cout << "cat destructor" << endl;
  88. }
  89.  
  90. void cat::speak() const {
  91. cout << "meow!" << endl;
  92. }
  93.  
  94.  
  95. int main() {
  96. // Polymorphism won't work here
  97. vector<animal> animals;
  98. // Polymorphism will work here
  99. vector<animal*> kennel;
  100.  
  101. cout << endl << "Constructing a dog:" << endl;
  102. dog d;
  103. cout << endl << "Constructing a cat:" << endl;
  104. cat c;
  105. cout << endl << "Constructing an animal:" << endl;
  106. animal a;
  107. // Add animals to animals vector. Here, everything gets turned into just an "animal," making polymorphism useless
  108. cout << endl << "Pass-by-value copies will be destructed as animals:" << endl;
  109. animals.push_back(d);
  110. animals.push_back(c);
  111. animals.push_back(a);
  112.  
  113. // Add animals to kennel vector. Here, each animal retains its real type of dog or cat
  114. for(int index = 0; index < 3; index++) {
  115. cout << endl << "Dynamically constructing a dog:" << endl;
  116. kennel.push_back(new dog);
  117. cout << endl << "Dynamically constructing a cat:" << endl;
  118. kennel.push_back(new cat);
  119. }
  120.  
  121. // Here, all the animals will output the basic "some animal sound"
  122. cout << endl << endl << "NON-POLYMORPHIC VECTOR: SPEAK" << endl;
  123. for(int index = 0; index < animals.size(); index++) {
  124. animals[index].speak();
  125. }
  126. cout << endl;;
  127.  
  128. // Here, all the animals will output their real "bark" or "meow"
  129. cout << endl << "POLYMORPHIC VECTOR: SPEAK" << endl;
  130. for(int index = 0; index < kennel.size(); index++) {
  131. kennel[index]->speak();
  132. }
  133. cout << endl;
  134.  
  135. // Delete polymorphic animals
  136. for(int index = 0; index < kennel.size(); index++) {
  137. cout << endl << "Destructing some polymorphic animal:" << endl;
  138. delete kennel[index];
  139. }
  140.  
  141. cout << endl << "Destructing static dog, cat, animal, and then non-polymorphic animal vector:" << endl;
  142. return 0;
  143. }
  144.  
Success #stdin #stdout 0s 3420KB
stdin
Standard input is empty
stdout
Constructing a dog:
animal constructor
dog constructor

Constructing a cat:
animal constructor
cat constructor

Constructing an animal:
animal constructor

Pass-by-value copies will be destructed as animals:
animal destructor
animal destructor
animal destructor

Dynamically constructing a dog:
animal constructor
dog constructor

Dynamically constructing a cat:
animal constructor
cat constructor

Dynamically constructing a dog:
animal constructor
dog constructor

Dynamically constructing a cat:
animal constructor
cat constructor

Dynamically constructing a dog:
animal constructor
dog constructor

Dynamically constructing a cat:
animal constructor
cat constructor


NON-POLYMORPHIC VECTOR: SPEAK
some animal sound
some animal sound
some animal sound


POLYMORPHIC VECTOR: SPEAK
bark!
meow!
bark!
meow!
bark!
meow!


Destructing some polymorphic animal:
dog destructor
animal destructor

Destructing some polymorphic animal:
cat destructor
animal destructor

Destructing some polymorphic animal:
dog destructor
animal destructor

Destructing some polymorphic animal:
cat destructor
animal destructor

Destructing some polymorphic animal:
dog destructor
animal destructor

Destructing some polymorphic animal:
cat destructor
animal destructor

Destructing static dog, cat, animal, and then non-polymorphic animal vector:
animal destructor
cat destructor
animal destructor
dog destructor
animal destructor
animal destructor
animal destructor
animal destructor