Advertisement
dmkozyrev

fraction.cpp

Jan 8th, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.11 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class Fraction
  5. {
  6.     private:
  7.         double p, q;
  8.     public:
  9.         Fraction(double a = 0, double b = 1){p=a; q=b;};
  10.         void print(){cout<<p<<"/"<<q;};
  11.         Fraction sum(Fraction f){return Fraction(p*f.q+f.p*q, q*f.q);};
  12.         Fraction sum(double d){return sum(Fraction(d, 1));};
  13.         Fraction operator+(Fraction f){return sum(f);};
  14.         Fraction operator+(double d){return sum(d);};
  15.         Fraction operator=(const Fraction & f){p = f.p; q = f.q;};
  16.         void setP(double value){p=value;};
  17.         void setQ(double value){q=value;};
  18.         double getP(){return p;};
  19.         double getQ(){return q;};
  20. };
  21.  
  22. int main()
  23. {
  24.     Fraction a(2,5), b(3,8);
  25.     cout << "a = "; a.print(); cout << endl;
  26.     cout << "b = "; b.print(); cout << endl;
  27.     double num(5);
  28.     cout << "num = " << num << endl;
  29.     Fraction c = a.sum(b);
  30.     Fraction d = a + b;
  31.     Fraction e = c + num;
  32.     cout << " c = a.sum(b) = "; a.print(); cout << " + "; b.print(); cout << " = "; c.print(); cout << endl;
  33.     cout << " d = a + b = "; a.print(); cout << " + "; b.print(); cout << " = "; d.print(); cout << endl;
  34.     cout << " e = c + num = "; c.print(); cout << " + " << num << " = "; e.print(); cout << endl;
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement