lab.cpp/tp2/ZFraction.cpp

43 lines
943 B
C++

/* [0] CONSTRUCTEUR
=========================================================*/
ZFraction::ZFraction(int a, int b) : _a(a), _b(b){}
/* [1] Getters
=========================================================*/
ostream& operator<<(ostream& o, const ZFraction& f){
o << "(" << f._a << "/" << f._b << ")";
return o;
}
/* [2] Operateurs
=========================================================*/
/* (1) Addition */
ZFraction ZFraction::operator+(const ZFraction& x) const{
int new_a = _a * x._b + _b * x._a;
int new_b = _b * x._b;
return ZFraction(new_a, new_b);
}
ZFraction& ZFraction::operator+=(const ZFraction& x){
*this = *this + x;
return *this;
}
/* (2) Multiplication */
ZFraction ZFraction::operator*(const ZFraction& x) const{
int new_a = _a * x._a;
int new_b = _b * x._b;
return ZFraction(new_a, new_b);
}
ZFraction& ZFraction::operator*=(const ZFraction& x){
*this = *this * x;
return *this;
}