fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <vector> // For std::vector
  4. #include <memory> // For std::unique_ptr
  5.  
  6. // Base Class (from previous example)
  7. class Animal {
  8. public:
  9. std::string name;
  10. int age;
  11.  
  12. Animal(std::string n, int a) : name(n), age(a) {
  13. std::cout << " Animal CTOR is called." << std::endl;
  14. }
  15.  
  16. // Making 'eat' virtual allows derived classes to override it
  17. // and ensures the correct derived version is called via base class pointer/reference.
  18. virtual void eat() const {
  19. std::cout << name << " is eating generic animal food." << std::endl;
  20. }
  21.  
  22. void sleep() const { // Not virtual
  23. std::cout << name << " is sleeping." << std::endl;
  24. }
  25.  
  26. virtual ~Animal() {
  27. std::cout << "Animal destructor called for " << name << std::endl;
  28. }
  29. };
  30.  
  31. // Derived Class: Dog (from previous example)
  32. class Dog : public Animal {
  33. public:
  34. std::string breed;
  35. Dog(std::string n, int a, std::string b) : Animal(n, a), breed(b) {
  36. std::cout << " DOG CTOR is called." << std::endl;
  37. }
  38.  
  39. // Override the virtual 'eat' method
  40. void eat() const override {
  41. std::cout << name << " the " << breed << " is happily eating dog food." << std::endl;
  42. }
  43.  
  44. void bark() const {
  45. std::cout << name << " says Woof! Woof!" << std::endl;
  46. }
  47.  
  48. ~Dog() override {
  49. std::cout << "Dog destructor called for " << name << std::endl;
  50. }
  51. };
  52.  
  53. // Derived Class: Cat (from previous example)
  54. class Cat : public Animal {
  55. public:
  56. Cat(std::string n, int a) : Animal(n, a) {
  57. std::cout << " CAT CTOR is called." << std::endl;
  58. }
  59.  
  60. void eat() const override { // Cat also overrides 'eat'
  61. std::cout << name << " the Cat is delicately eating fish." << std::endl;
  62. }
  63.  
  64. void meow() const {
  65. std::cout << name << " says Meow!" << std::endl;
  66. }
  67.  
  68. ~Cat() override {
  69. std::cout << "Cat destructor called for " << name << std::endl;
  70. }
  71. };
  72.  
  73.  
  74. int main() {
  75. // Using base class pointers to achieve polymorphism
  76. std::vector<std::unique_ptr<Animal>> farm;
  77.  
  78. farm.push_back(std::make_unique<Dog>("Buddy", 3, "Golden Retriever"));
  79. farm.push_back(std::make_unique<Cat>("Whiskers", 5));
  80. farm.push_back(std::make_unique<Animal>("Generic Animal", 1)); // Can also add a base class object
  81.  
  82. // Iterate through the vector, treating all objects as 'Animal'
  83. // The virtual 'eat()' method ensures the correct derived version is called at runtime.
  84. for (const auto& animal_ptr : farm) {
  85. animal_ptr->eat(); // Polymorphic call
  86. animal_ptr->sleep(); // Non-polymorphic call (calls Animal's sleep())
  87. std::cout << "---" << std::endl;
  88. }
  89.  
  90. // Demonstrating non-polymorphic call (if 'eat' wasn't virtual)
  91. Animal* a = new Dog("Fido", 2, "Labrador");
  92. a->sleep(); // This calls Animal::sleep() because sleep() is not virtual
  93. delete a; // Calls Animal destructor, then Dog destructor (because base destructor is virtual)
  94.  
  95.  
  96. return 0;
  97. }
Success #stdin #stdout 0.01s 5320KB
stdin
10
aba
geeksforgeeks
geeksforgeeks
geeksforgeeks
geeksforgeeks
geeksforgeeks
geeksforgeeks
geeksforgeeks
geeksforgeeks
geeksforgeeks
stdout
 Animal CTOR is called.
 DOG CTOR is called.
 Animal CTOR is called.
 CAT CTOR is called.
 Animal CTOR is called.
Buddy the Golden Retriever is happily eating dog food.
Buddy is sleeping.
---
Whiskers the Cat is delicately eating fish.
Whiskers is sleeping.
---
Generic Animal is eating generic animal food.
Generic Animal is sleeping.
---
 Animal CTOR is called.
 DOG CTOR is called.
Fido is sleeping.
Dog destructor called for Fido
Animal destructor called for Fido
Dog destructor called for Buddy
Animal destructor called for Buddy
Cat destructor called for Whiskers
Animal destructor called for Whiskers
Animal destructor called for Generic Animal