fork download
  1. #include <iostream>
  2. #include <string>
  3.  
  4. class Action {
  5. public:
  6. virtual void exec() const = 0;
  7. };
  8.  
  9. class HelloWorld : public Action {
  10. public:
  11. virtual void exec() const { std::cout << "Hello, World!" << std::endl; }
  12. };
  13.  
  14. class Greeting : public Action {
  15. public:
  16. Greeting(const std::string &name) : m_name(name) {}
  17. virtual void exec() const { std::cout << "Hello, " << m_name << "!" << std::endl; }
  18. private:
  19. std::string m_name;
  20. };
  21.  
  22. void runAction(const Action &action = HelloWorld())
  23. {
  24. action.exec();
  25. }
  26.  
  27. int main()
  28. {
  29. runAction();
  30. runAction(HelloWorld());
  31. runAction(Greeting("Ivan"));
  32. return 0;
  33. }
Success #stdin #stdout 0.01s 2856KB
stdin
Standard input is empty
stdout
Hello, World!
Hello, World!
Hello, Ivan!