#include using namespace std; class Rational { public: //constructeurs Rational(int n = 0, int d = 1); Rational(const Rational &r); //constructeur de copie //operateur surcharge Rational operator*(const Rational &) const; double Convert(); void Invert(); void Print(); private: int num; int den; }; //constructeur avec 2 parametres //les deux constructeurs (fonctions) suivants sont equivalents /*Rational::Rational(int n, int d) { num = n; den = d; cout << "Un nombre Rational a ete cree." << endl; } */ Rational::Rational(int n, int d): num(n), den(d) { cout << "Un nombre Rational a ete cree." << endl; } //constructeur de copie //les deux constructeurs (fonctions) de copie suivants sont equivalents /*Rational::Rational(const Rational &r) { num = r.num; den = r.den; cout << "Un nombre Rational a ete copie." << endl; } */ Rational::Rational(const Rational &r): num(r.num), den(r.den) { cout << "Un nombre Rational a ete copie." << endl; } double Rational::Convert() { return double(num) / double(den); } void Rational::Invert() { int temp = num; num = den; den = temp; } void Rational::Print() { cout << num << "/" << den << endl; } Rational Rational::operator*(const Rational &q) const { int n, d; n = num * q.num; d = den * q.den; Rational res = Rational(n,d); return res; } int main() { Rational q(11, 3), r(5,7); cout << "q = "; q.Print(); cout << "r = "; r.Print(); //multiplication par operateur surcharge Rational t = q*r; cout << "q * r = "; t.Print(); //affectation lors de la declaration Rational a = t; cout << "a = t = "; a.Print(); return 0; }