fork download
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. class Number;
  6. class Integer;
  7. class Rational;
  8.  
  9. class Number
  10. {
  11. public:
  12. int num, den;
  13. virtual void add(Number&) = 0;
  14. virtual void addInteger(Integer&) = 0;
  15. virtual void addRational(Rational&) = 0;
  16. virtual string toString() = 0;
  17. };
  18.  
  19. class Rational : public Number
  20. {
  21. void addInteger(Integer& i);
  22.  
  23. void addRational(Rational &r);
  24.  
  25. public:
  26. void add(Number& n)
  27. {
  28. n.addRational(*this);
  29. }
  30.  
  31. Rational(int n, int d)
  32. {
  33. this->num = n;
  34. this->den = d;
  35. }
  36. };
  37.  
  38. class Integer : public Number
  39. {
  40. void addInteger(Integer& i);
  41.  
  42. void addRational(Rational& r);
  43.  
  44. public:
  45. void add(Number& n)
  46. {
  47. n.addInteger(*this);
  48. }
  49.  
  50. Integer(int n)
  51. {
  52. this->num = n;
  53. this->den = 1;
  54. }
  55. };
  56.  
  57. void Rational::addInteger(Integer& i)
  58. {
  59. this->num = this->num + i.num*this->den;
  60. this->den = this->den;
  61. cout << this->num << "/" << this->den;
  62. }
  63.  
  64. void Rational::addRational(Rational &r)
  65. {
  66. this->num = this->num*r.den + this->den*r.num;
  67. this->den = this->den*r.den;
  68. cout << this->num << "/" << this->den;
  69. }
  70.  
  71. void Integer::addInteger(Integer& i)
  72. {
  73. this->num += i.num;
  74. this->den = 1;
  75. cout << this->num;
  76. }
  77.  
  78. void Integer::addRational(Rational& r)
  79. {
  80. this->num = this->num*r.den + r.num;
  81. this->den = r.den;
  82. cout << "this->num" << "/" << this->den;
  83. }
  84.  
  85. int main()
  86. {
  87. cout << "Helo World";
  88. return 0;
  89. }
Success #stdin #stdout 0s 3456KB
stdin
Standard input is empty
stdout
Helo World