what is the difference ?

Hi,
what is the right impementation of operator* :

1
2
3
rational operator*(const rational& r1, const rational& r2){
     return rational(r1.p * r2.p, r1.q * r2.q);
}


and
1
2
3
rational operator*(const rational& r2){
     return rational(p * r2.p, q * r2.q);
}


thnx.
Last edited on
Either works so long the first one has access to p and q inside rational, and as long as the second one is part of the rational class and your meant r2 instead of r1.

Ooooor, if this is a trick question: Neither because both code pieces implement operator*, not operator+!!!
ooooh i meant operator*, i fixed it :)
thnx man :)
rational operator*(const rational& r2) const; so it can work with constant objects.
I think that you should overload the compound assignment operators as methods, and templatise the rest (as rel_ops)

By the way
1
2
rational a,b,c;
(a*b) = c; //this compiles. If you don't want that, return a constant object instead 
Last edited on
Topic archived. No new replies allowed.