fork download
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. class base {
  6. public:
  7. virtual void foo() = 0;
  8. };
  9.  
  10. class d1: public base {
  11. public:
  12. virtual void foo() {
  13. cout << "d1" << endl;
  14. }
  15. };
  16.  
  17. class d2: public base {
  18. public:
  19. virtual void foo() {
  20. cout << "d2" << endl;
  21. }
  22. };
  23.  
  24. int main() {
  25. std::vector<base*> v;
  26. v.push_back(new d1());
  27. v.push_back(new d2());
  28. for(auto b: v) {
  29. b->foo();
  30. }
  31. return 0;
  32. }
Success #stdin #stdout 0s 3276KB
stdin
Standard input is empty
stdout
d1
d2