fork download
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. class A
  5. {
  6. public:
  7. virtual void print() const { std::cout << "I'm an A!" << std::endl; }
  8. };
  9.  
  10. class B : public A
  11. {
  12. public:
  13. void print() const { std::cout << "I'm a B!" << std::endl; }
  14. };
  15.  
  16. int main() {
  17. std::vector<A> v;
  18. std::vector<A*> vv;
  19.  
  20. v.push_back(A());
  21. v.push_back(B());
  22.  
  23. for(const auto& x : v)
  24. {
  25. x.print();
  26. }
  27.  
  28. vv.push_back(new A);
  29. vv.push_back(new B);
  30.  
  31. for(const auto& x : vv)
  32. {
  33. x->print();
  34. }
  35.  
  36. return 0;
  37. }
Success #stdin #stdout 0s 3464KB
stdin
Standard input is empty
stdout
I'm an A!
I'm an A!
I'm an A!
I'm a B!