. Tại sao khi mình dùng tham chiếu ở hai phép operator gán và operator cộng thì kết quả nó lại ra không đúng à.còn khi mà mình dùng tham trị ở operator gán và cộng (phanso a) thì nó lại ra kết quả đúng ạ .
#include <iostream>
#include<string>
#include<cmath>
using namespace std;
class phanso {
private:
float tu;
float mau;
public:
void nhap();
void xuat();
phanso& operator+(const phanso&);
phanso& operator=(const phanso&);
void xuly(phanso&);
};
void phanso::nhap() {
cout << "Nhap Phan So:";
cin >> tu >> mau;
}
void phanso::xuat() {
cout << "Phan so la:" << tu << "/" << mau << endl;
}
phanso& phanso::operator+( phanso &a) {
phanso temp;
temp.tu = tu * a.mau + mau * a.tu;
temp.mau = mau * a.mau;
return temp;
}
phanso& phanso::operator=( phanso &a) {
this->tu = a.tu;
this->mau = a.mau;
return *this;
}
void phanso::xuly(phanso& a) {
float b = abs(a.tu);
float c = abs(a.mau);
while (c * b != 0) {
if (b > c) b = b - c;
else c = c - b;
}
a.tu = (a.tu / (b + c));
a.mau = (a.mau / (b + c));
}
int main() {
phanso a, b, c;
a.nhap();
b.nhap();
c = (a + b);
c.xuly(c);
cout << endl;
c.xuat();
return 0;
}