有理数の演算を行うための次のコードが、実際に代入演算とストリーム挿入演算子<<をオーバーロードしてオブジェクトを出力しているかどうかを知りたいです。私はC++があまり得意ではないので、これは私にとっては新しいことですが、私が言えることから、すでにそうだと思います。
/*
*
* C++ version
*
*/
/* rational.h */
#ifndef RATIONAL_H
#define RATIONAL_H
#include <iostream>
using std::ostream;
struct rational {
rational(int = 0, int = 1);
rational operator+(const rational &) const;
rational operator-(const rational &) const;
rational operator*(const rational &) const;
rational operator/(const rational &) const;
rational operator+(int) const;
rational operator-(int) const;
rational operator*(int) const;
rational operator/(int) const;
friend rational operator+(int, const rational &);
friend rational operator-(int, const rational &);
friend rational operator*(int, const rational &);
friend rational operator/(int, const rational &);
friend ostream &operator<<(ostream &, const rational &);
private:
int den;
int num;
};
#endif /* RATIONAL_H */
/* rational.cc */
#include <iostream>
#include "rational.h"
rational::rational(int num, int den) : num(num), den(den) {}
rational rational::operator+(const rational &o) const {
return rational(num * o.den + o.num * den, den * o.den);
}
rational rational::operator+(int n) const {
return rational(num + n * den, den);
}
rational rational::operator-(const rational &o) const {
return rational(num * o.den - o.num * den, den * o.den);
}
rational rational::operator-(int n) const {
return rational(num - n * den, den);
}
rational rational::operator*(const rational &o) const {
return rational(num * o.num, den * o.den);
}
rational rational::operator*(int n) const {
return rational(num * n, den);
}
rational rational::operator/(const rational &o) const {
return rational(num * o.den, den * o.num);
}
rational rational::operator/(int n) const {
return rational(num, den * n);
}
rational operator+(int n, const rational &o) {
return o + n;
}
rational operator-(int n, const rational &o) {
return rational(n) - o;
}
rational operator*(int n, const rational &o) {
return o * n;
}
rational operator/(int n, const rational &o) {
return rational(n) / o;
}
ostream &operator<<(ostream &out, const rational &o) {
out << '(' << o.num << " / " << o.den << ')';
return out;
}
/* main.cc */
#include <iostream>
#include "rational.h"
using std::cout;
using std::endl;
int main(void) {
rational a(1, 2);
rational b(2, 3);
int i = 5;
cout << a << " + " << b << " = " << a + b << endl;
cout << a << " - " << b << " = " << a - b << endl;
cout << a << " * " << b << " = " << a * b << endl;
cout << a << " / " << b << " = " << a / b << endl;
cout << a << " + " << i << " = " << a + i << endl;
cout << a << " - " << i << " = " << a - i << endl;
cout << a << " * " << i << " = " << a * i << endl;
cout << a << " / " << i << " = " << a / i << endl;
cout << i << " + " << a << " = " << i + a << endl;
cout << i << " - " << a << " = " << i - a << endl;
cout << i << " * " << a << " = " << i * a << endl;
cout << i << " / " << a << " = " << i / a << endl;
return 0;
}