fork download
  1. #include <string>
  2. #include <memory>
  3. #include <cassert>
  4. #include <iostream>
  5.  
  6. class A
  7. {
  8. public:
  9. virtual ~A() = default;
  10. virtual std::string foo() const
  11. {
  12. return "A";
  13. }
  14. };
  15.  
  16. void testA()
  17. {
  18. A a;
  19. assert(a.foo() == "A");
  20. }
  21.  
  22. class B: public A
  23. {
  24. public:
  25. virtual std::string foo() const
  26. {
  27. return "B";
  28. }
  29. };
  30.  
  31. void testB()
  32. {
  33. B b;
  34. assert(b.foo() == "B");
  35. }
  36.  
  37. void real_code()
  38. {
  39. std::unique_ptr<A> ptr(new B);
  40. std::cout << ptr->foo() << std::endl;
  41. }
  42.  
  43. int main() {
  44. // your code goes here
  45. real_code();
  46. return 0;
  47. }
Success #stdin #stdout 0s 4200KB
stdin
Standard input is empty
stdout
B