#include using namespace std; class Rational { public: Rational(int n=0, int d=1) : n_(n), d_(d) { cout << "calling constructor" << endl; } Rational(const Rational& rhs) : n_(rhs.n_), d_(rhs.d_) { cout << "calling Copy constructor" << endl; } Rational& operator=(const Rational& rhs) { cout << "calling operator=" << endl; n_=rhs.n_; d_=rhs.d_; return *this; } int num() const {return n_;} int den() const {return d_;} private: int n_, d_; friend ostream& operator<<(ostream&, const Rational&); }; const Rational operator*(const Rational& lhs, const Rational& rhs) { return Rational(lhs.num()*rhs.num(), lhs.den()*rhs.den()); } ostream& operator<<(ostream& out , const Rational& r) { out << r.n_ << "/" << r.d_; } int main() { Rational r1(10); Rational r2(3,5); cout << "r1 " << r1<< endl; cout << "r2 " << r2<< endl; Rational r3 = r1 * r2; cout << "r3 " << r3<< endl; Rational r4 = r1 * 3; cout << "r4 " << r4<< endl; Rational r5 = 3 * r1; // r5 = 3.operator*(r1); ?? 3 e' in int! cout << "r5 " << r5<< endl; Rational r6 = r1; Rational r7(r1); }