fork download
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. class Base {
  5. public:
  6. virtual void go() {
  7. std::cout << "I am the base class" << std::endl;
  8. }
  9. };
  10.  
  11. class DerA : public Base {
  12. void go() {
  13. std::cout << "I am the first derivative" << std::endl;
  14. }
  15. };
  16.  
  17. class DerB : public Base {
  18. void go() {
  19. std::cout << "I am the second derivative" << std::endl;
  20. }
  21. };
  22.  
  23. class UnrelatedClass {
  24. public:
  25. std::vector<Base*> vec;
  26. void add_to_vec(Base* thing) {
  27. vec.push_back(thing);
  28. }
  29. };
  30.  
  31. int main() {
  32. Base base;
  33. DerA dera;
  34. DerB derb;
  35.  
  36. UnrelatedClass uc;
  37. uc.add_to_vec(&base);
  38. uc.add_to_vec(&dera);
  39. uc.add_to_vec(&derb);
  40.  
  41. for (auto thing : uc.vec) {
  42. thing->go();
  43. }
  44.  
  45. return 0;
  46. }
Success #stdin #stdout 0s 3476KB
stdin
Standard input is empty
stdout
I am the base class
I am the first derivative
I am the second derivative