Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- class Fraction
- {
- int chis;
- int znam;
- public:
- int GetChis() const
- {
- return chis;
- }
- int GetZnam() const
- {
- return znam;
- }
- void SetChis(int chis)
- {
- this->chis = chis;
- }
- void SetZnam(int znam)
- {
- this->znam = znam == 0 ? 1 : znam;
- }
- Fraction operator + (/*Fraction* this, */ const Fraction& f2)
- {
- Fraction result;
- result.SetChis(this->GetChis() * f2.GetZnam() + f2.GetChis() * this->GetZnam());
- result.SetZnam(this->GetZnam() * f2.GetZnam());
- return result;
- }
- };
- void main()
- {
- Fraction a;
- a.SetChis(1);
- a.SetZnam(2);
- Fraction b;
- b.SetChis(1);
- b.SetZnam(4);
- Fraction result = a + b;
- Fraction r = a.operator + (b);
- cout << result.GetChis() << " / " << result.GetZnam() << "\n";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement