fork(1) download
  1. #include <iostream>
  2. #include <vector>
  3. #include <memory>
  4.  
  5. class MatrixInterface
  6. {
  7. public:
  8. virtual ~MatrixInterface() = default;
  9.  
  10. virtual void doSomething() const = 0;
  11. };
  12.  
  13. class MatrixA : public MatrixInterface
  14. {
  15. public:
  16. void doSomething() const override
  17. {
  18. std::cout << "This is MatrixA" << std::endl;
  19. }
  20. };
  21.  
  22. class MatrixB : public MatrixInterface
  23. {
  24. void doSomething() const override
  25. {
  26. std::cout << "This is MatrixB" << std::endl;
  27. }
  28. };
  29.  
  30. class MatrixC : public MatrixInterface
  31. {
  32. void doSomething() const override
  33. {
  34. std::cout << "This is MatrixC" << std::endl;
  35. }
  36. };
  37.  
  38. int main() {
  39. using VectorType = std::vector<std::unique_ptr<MatrixInterface>>;
  40. VectorType matricies;
  41.  
  42. matricies.push_back(VectorType::value_type(new MatrixA));
  43. matricies.push_back(VectorType::value_type(new MatrixB));
  44. matricies.push_back(VectorType::value_type(new MatrixC));
  45.  
  46. for(auto& element : matricies)
  47. {
  48. element->doSomething();
  49. }
  50.  
  51. return 0;
  52. }
Success #stdin #stdout 0s 3432KB
stdin
Standard input is empty
stdout
This is MatrixA
This is MatrixB
This is MatrixC