把老师代码上传一下,方便用移动设备时刻查看
#include <iostream>
using namespace std;
class Rational{
private:
int fz;
int fm;
public:
static int count;
Rational();
Rational(const Rational& );
~Rational();
friend Rational operator+(Rational,Rational);
Rational operator*(Rational);
Rational& operator=(Rational);
Rational& operator++();
Rational operator++(int);
friend bool operator<(Rational,Rational);
friend ostream& operator<<(ostream&,const Rational&);
friend istream& operator>>(istream&,Rational&);
};
int Rational::count = 0;
Rational::Rational()
{
fz =0 ;
fm =1 ;
count++;
}
Rational::Rational(const Rational& r)
{
fz = r.fz;
fm = r.fm;
count++;
}
Rational::~Rational()
{
count--;
}
ostream& operator<<(ostream& output,const Rational& x)
{
output << x.fz << "/" << x.fm;
return output;
}
istream& operator>>(istream& input,Rational& x)
{
input >> x.fz >> x.fm;
return input;
}
Rational Rational::operator*(Rational y)
{
Rational result;
result.fz = this->fz * y.fz;
result.fm = this->fm * y.fm;
return result;
}
Rational operator+(Rational x,Rational y)
{
Rational result;
result.fz = x.fz * y.fm + x.fm * y.fz;
result.fm = x.fm * y.fm;
return result;
}
Rational& Rational::operator=(Rational y)
{
this->fm = y.fm;
this->fz = y.fz;
return *this;
}
Rational& Rational::operator++()
{
this->fz += this->fm;
return *this;
}
Rational Rational::operator++(int)
{
Rational temp(*this);
this->fz += this->fm;
return temp;
}
bool operator<(Rational x,Rational y)
{
if((x.fz*y.fm - x.fm*y.fz) < 0 )
return true;
else
return false;
}
int main()
{
Rational r,b,c;
cout << "请输入四个整数,表示两个分数的分子和分母:" ;
cin >> b >> c;
cout << "b=" << b << ", c=" << c << '\n';
r = b + c;
cout << "r = b + c = " << r << '\n';
r = b * c;
cout << "r = b * c = " << r << '\n';
cout << "r++ = " << (r++) << ", r = " << r << '\n';
cout << "++r = " << (++r) << ", r = " << r << '\n';
Rational num1 = r++;
Rational num2 = r;
Rational num3 = ++r;
if(num1 < num2)
cout << "后置自增返回原对象的值\n";
else
cout << "后置自增返回自增后的对象值\n";
if(num2 < num3)
cout << "前置自增返回自增后的对象值\n";
else
cout << "前置自增返回原对象的值\n";
return 0;
}