Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- class Fraction
- {
- private:
- double p, q;
- public:
- Fraction(double a = 0, double b = 1){p=a; q=b;};
- void print(){cout<<p<<"/"<<q;};
- Fraction sum(Fraction f){return Fraction(p*f.q+f.p*q, q*f.q);};
- Fraction sum(double d){return sum(Fraction(d, 1));};
- Fraction operator+(Fraction f){return sum(f);};
- Fraction operator+(double d){return sum(d);};
- Fraction operator=(const Fraction & f){p = f.p; q = f.q;};
- void setP(double value){p=value;};
- void setQ(double value){q=value;};
- double getP(){return p;};
- double getQ(){return q;};
- };
- int main()
- {
- Fraction a(2,5), b(3,8);
- cout << "a = "; a.print(); cout << endl;
- cout << "b = "; b.print(); cout << endl;
- double num(5);
- cout << "num = " << num << endl;
- Fraction c = a.sum(b);
- Fraction d = a + b;
- Fraction e = c + num;
- cout << " c = a.sum(b) = "; a.print(); cout << " + "; b.print(); cout << " = "; c.print(); cout << endl;
- cout << " d = a + b = "; a.print(); cout << " + "; b.print(); cout << " = "; d.print(); cout << endl;
- cout << " e = c + num = "; c.print(); cout << " + " << num << " = "; e.print(); cout << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement