fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class RationalNumber
  5. {
  6. public:
  7. RationalNumber(); //default constructor: initializes private data to "default" values
  8. RationalNumber(int n = 0, int d = 1); //constructor: initializes balance to d + c/100 and rate to r
  9. void setNumerator(int newnum);
  10. void setDenominator(int newden);
  11. void output();
  12. int getNumerator();
  13. int getDenominator();
  14. sum();
  15. private:
  16. int numerator;
  17. int denominator;
  18. };
  19.  
  20.  
  21.  
  22. RationalNumber::RationalNumber(int n, int d) {
  23. setNumerator (n);
  24. setDenominator (d);
  25. // reduce();
  26. }
  27.  
  28. void RationalNumber::setNumerator(int newnum)
  29. {
  30.  
  31. numerator = newnum;
  32. }
  33.  
  34. void RationalNumber::setDenominator(int newden)
  35. {
  36.  
  37. denominator = newden;
  38. }
  39.  
  40. int RationalNumber::getNumerator()
  41. {
  42. return numerator;
  43. }
  44. int RationalNumber::getDenominator()
  45. {
  46. return denominator;
  47. }
  48.  
  49. void RationalNumber::output() {
  50.  
  51. cout << numerator << "/" << denominator;
  52. }
  53.  
  54. RationalNumber RationalNumber::sum(const RationalNumber& a) {
  55. Rational s;
  56.  
  57. s.numerator = a.numerator * denominator + a.denominator * numerator;
  58. s.denominator = a.denominator * denominator;
  59. //t.reduce();
  60.  
  61. return s;
  62. }
  63.  
  64. int main()
  65. {
  66. RationalNumber op1 = RationalNumber(1, -3);
  67. op1.output();
  68. RationalNumber op2 = RationalNumber(2);
  69. op2.output();
  70. RationalNumber sum = op1.add(op2);
  71. cout << "sum: ";
  72. sum.output(cout);
  73.  
  74. }
  75.  
  76.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:14:7: error: ISO C++ forbids declaration of 'sum' with no type [-fpermissive]
   sum();
       ^
prog.cpp:54:16: error: prototype for 'RationalNumber RationalNumber::sum(const RationalNumber&)' does not match any in class 'RationalNumber'
 RationalNumber RationalNumber::sum(const RationalNumber& a) {
                ^
prog.cpp:14:3: error: candidate is: int RationalNumber::sum()
   sum();
   ^
prog.cpp: In function 'int main()':
prog.cpp:70:27: error: 'class RationalNumber' has no member named 'add'
  RationalNumber sum = op1.add(op2);
                           ^
prog.cpp:72:17: error: no matching function for call to 'RationalNumber::output(std::ostream&)'
  sum.output(cout);
                 ^
prog.cpp:72:17: note: candidate is:
prog.cpp:49:6: note: void RationalNumber::output()
 void RationalNumber::output() {
      ^
prog.cpp:49:6: note:   candidate expects 0 arguments, 1 provided
stdout
Standard output is empty