分类: C/C++
2009-08-03 18:28:40
int i;
Rational r;// r 是个有理数
...
cin >> i >> r;
cout << i << r;
你所写的表示有理数的类的代码可能象下面这样:
class Rational {
public:
Rational(int numerator = 0, int denominator = 1);
...
private:
int n, d;// 分子,分母
friend ostream& operator<<(ostream& s, const Rational& );
};
ostream& operator<<(ostream& s, const Rational& r)
{
s<< r.n << '/' << r.d;
return s;
}
//////////////////////////////////////
我确实说的是