Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.87 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3.  
  4. class is_printable {
  5. public:
  6. virtual std::ostream& print() const = 0;
  7. };
  8.  
  9. class A : public is_printable {
  10. public:
  11. A(std::string s)
  12. : _s(s)
  13. {
  14. }
  15. std::ostream& print() const override
  16. {
  17. return std::cout << _s;
  18. }
  19.  
  20. private:
  21. std::string _s;
  22. };
  23.  
  24. class B : public is_printable {
  25. public:
  26. B(int s)
  27. : _s(s)
  28. {
  29. }
  30. std::ostream& print() const override
  31. {
  32.  
  33. return std::cout << _s;
  34. }
  35.  
  36. private:
  37. int _s;
  38. };
  39.  
  40. std::ostream& operator<<(std::ostream& s, const is_printable& o)
  41. {
  42. return o.print();
  43. }
  44.  
  45. int main()
  46. {
  47. A a("Tekst");
  48. B b { 123 };
  49.  
  50. std::cout << "a:\t" << a << "; b:\t" << b << "\n";
  51. is_printable& a_r = a;
  52. is_printable& b_r = b;
  53.  
  54. std::cout << "a_r:\t" << a_r << "; b_r:\t" << b_r << "\n";
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement