fork download
  1. #include <iostream>
  2. #include <memory>
  3. #include <string>
  4.  
  5. class IFoo
  6. {
  7. public:
  8. virtual void f() = 0;
  9. virtual std::string getName() const noexcept = 0;
  10.  
  11. virtual ~IFoo() noexcept = default;
  12. };
  13.  
  14. class Foo : public IFoo
  15. {
  16. public:
  17. explicit Foo(const std::string& name) : name_{name} {}
  18.  
  19. void f() override = 0;
  20.  
  21. std::string getName() const noexcept
  22. {
  23. return name_;
  24. }
  25. private:
  26. std::string name_;
  27. };
  28.  
  29. class FooBar final : public Foo
  30. {
  31. public:
  32. FooBar() : Foo("FooBar") {}
  33.  
  34. void f() override
  35. {
  36. std::cout << "Hello from FooBar! :-)" << std::endl;
  37. }
  38. };
  39.  
  40. int main()
  41. {
  42. std::shared_ptr<IFoo> foo{ std::make_shared<FooBar>() };
  43.  
  44. foo->f();
  45. std::cout << foo->getName() << std::endl;
  46.  
  47. return 0;
  48. }
Success #stdin #stdout 0s 5320KB
stdin
Standard input is empty
stdout
Hello from FooBar! :-)
FooBar