fork download
  1. #include <iostream>
  2. #include <memory>
  3. #include <vector>
  4.  
  5. class MyInterface {
  6. public:
  7. virtual const std::string identify() = 0;
  8. };
  9.  
  10. class A : public MyInterface {
  11. public:
  12. const std::string identify() {
  13. return std::string("A");
  14. }
  15. };
  16.  
  17. class B : public MyInterface {
  18. public:
  19. const std::string identify() {
  20. return std::string("B");
  21. }
  22. };
  23.  
  24. class C : public MyInterface {
  25. public:
  26. const std::string identify() {
  27. return std::string("C");
  28. }
  29. };
  30.  
  31. int main() {
  32. std::vector<std::shared_ptr<MyInterface>> theVec;
  33. theVec.push_back(std::make_shared<A>());
  34. theVec.push_back(std::make_shared<B>());
  35. theVec.push_back(std::make_shared<C>());
  36.  
  37. std::cout << theVec[0]->identify();
  38. std::cout << theVec[1]->identify();
  39. std::cout << theVec[2]->identify();
  40. }
Success #stdin #stdout 0s 3432KB
stdin
Standard input is empty
stdout
ABC