fork download
  1. #include <iostream>
  2. #include <memory>
  3. #include <vector>
  4. #include <algorithm>
  5. using namespace std;
  6.  
  7. class Base
  8. {
  9. public:
  10. Base() {}
  11. virtual void doStuff() = 0;
  12. };
  13.  
  14. class DerivedA : public Base
  15. {
  16. private:
  17. //specific A member variables
  18. public:
  19. DerivedA() : Base() {}
  20. virtual void doStuff() {
  21. std::cout << "Derived Class A - Do Stuff" << std::endl;
  22. }
  23. };
  24.  
  25. class DerivedB : public Base
  26. {
  27. private:
  28. //specific B member variables
  29. public:
  30. DerivedB() : Base() {}
  31. virtual void doStuff() {
  32. std::cout << "Derived Class B - Do Stuff" << std::endl;
  33. }
  34. };
  35. int main() {
  36. // your code goes here
  37. std::vector<std::unique_ptr<Base> > container;
  38. container.push_back(std::make_unique<DerivedA>());
  39. container.push_back(std::make_unique<DerivedB>());
  40.  
  41. std::for_each(container.begin(), container.end(), [](std::unique_ptr<Base> & b) {
  42. b->doStuff();
  43. });
  44. return 0;
  45. }
Success #stdin #stdout 0s 3232KB
stdin
Standard input is empty
stdout
Derived Class A - Do Stuff
Derived Class B - Do Stuff