接上篇文章https://blog.csdn.net/Master_Cui/article/details/109515376,继续实现分数类和相关运算符的重载
3.重载算术运算符和复合赋值运算符
算术运算符应该定义为非成员运算符,主要是为了允许左侧或者右侧的运算对象能够转换成自定义类的类型(比如string中的operator+),因为算符运算符不会改变运算对象的状态,所以形参是const的引用。
算术运算符得到的新值不同于运算对象,经常是局部变量,所以返回值是一个局部变量的拷贝
如果一个类定义了算术运算符,那么一般也会有对应的复合赋值运算符,通常,在复合赋值运算符中实现复合赋值运算,然后在算符运算符中调用复合赋值运算符,这样代码会很简洁。
而且在重载运算符时,都会用已经实现的重载运算符去实现一个未实现的重载运算符
复合赋值运算符和拷贝赋值运算符类似,为了更新自身的值,也得返回自定义类型的引用
拷贝赋值运算符和复合赋值运算符的实现如下
fraction &fraction::operator=(const fraction &t)
{
cout<<__func__<<endl;
this->numerator_=t.numerator_;
this->denominator_=t.denominator_;
return *this;
}
fraction &fraction::operator+=(const fraction &t)
{
cout<<__func__<<endl;
this->numerator_=this->numerator_*t.denominator_+t.numerator_*this->denominator_;
this->denominator_=this->denominator_*t.denominator_;
return *this;
}
fraction &fraction::operator-=(const fraction &t)
{
cout<<__func__<<endl;
this->numerator_=this->numerator_*t.denominator_-t.numerator_*this->denominator_;
this->denominator_=this->denominator_*t.denominator_;
return *this;
}
对应的算术运算符如下
fraction operator+(const fraction &one, const fraction &two)
{
cout<<__func__<<endl;
fraction res=one;
res+=two;
return res;
}
fraction operator-(const fraction &one, const fraction &two)
{
cout<<__func__<<endl;
fraction res=one;
res-=two;
return res;
}
测试
int main(int argc, char const *argv[])
{
fraction t1(1,20),t2(3,-7);
cout<<t1-t2<<endl;
cout<<"------------"<<endl;
cout<<t1+t2<<endl;
return 0;
}
上述代码的时序如下:首先,创建两个对象,调用两次构造函数,然后执行t1-t2,调用operator-,在operator-中,创建了一个局部变量res,并进行拷贝初始化,接着调用operator-=,operator-=执行结束后,operator-将res的副本返回并拷贝初始化了一个res的临时变量,所以调用了一次拷贝构造函数,同时函数的作用域结束,res被释放,调用了一次析构函数,然后,调用operator<<将临时对象打印出来,打印结束后,表达式执行结束,fraction的临时对象释放,调用一次析构函数,第五行之后的代码的时序类似。
在这里之所以能将一个临时对象传入operator<<,是因为operator<<的形参是const complex &,所以可以绑定一个临时变量,具体原因见博客https://blog.csdn.net/Master_Cui/article/details/106353580
如果是普通引用,则不能绑定临时变量,因为此时引用改变的是临时量而不是希望改变的那个对象,这种改变是无意义的。所以规定普通引用不能绑定到临时量上。引用必须和具体的变量绑定
加减运算符及其复合赋值运算符实现之后,乘除法运算符和对应的复合赋值运算符的实现如下
fraction operator*(const fraction &one, const fraction &two)
{
cout<<__func__<<endl;
fraction res=one;
res*=two;
return res;
}
fraction operator/(const fraction &one, const fraction &two)
{
cout<<__func__<<endl;
fraction res=one;
res/=two;
return res;
}
fraction &fraction::operator*=(const fraction &t)
{
cout<<__func__<<endl;
this->numerator_=this->numerator_*t.numerator_;
this->denominator_=this->denominator_*t.denominator_;
return *this;
}
fraction &fraction::operator/=(const fraction &t)
{
cout<<__func__<<endl;
this->numerator_=this->numerator_*t.denominator_;
this->denominator_=this->denominator_*t.numerator_;
return *this;
}
int main(int argc, char const *argv[])
{
fraction t1(1,20),t2(3,-7);
cout<<t1*t2<<endl;
cout<<"------------"<<endl;
cout<<t1/t2<<endl;
return 0;
}
时序和加减法类似
4.相等运算符
相等运算只需要比较类对象中的每个非static成员是否相等即可,如果一个类实现了operator==,也应该实现operator!=,并且其中一个是实现了具体的对象比较工作,而另一个只需要调用那个真正工作的运算符即可
具体实现及测试如下
bool operator==(const fraction &one, const fraction &two)
{
cout<<__func__<<endl;
fraction tmpone=one;
tmpone.numerator_*=two.denominator_;
fraction tmptwo=two;
tmptwo.numerator_*=one.denominator_;
return tmpone.numerator_==tmptwo.numerator_;//只需要比较通分之后的分子
}
int main(int argc, char const *argv[])
{
fraction t1(10,20),t2(-5,-10), t3(1,2);
cout<<(t1==t2)<<endl;
cout<<(t1!=t3)<<endl;
return 0;
}
5.关系运算符
定义了分数的相等和不等判断,顺便也定义下>、<、>=、<=,尤其是<,对于关联容器和排序算法默认的排序方式就是operator<。
如果一个类实现了operator==,那么如果判断这个类两个的对象满足operator!=,那么,这两个对象应该满足operator<。
但是并不时所有的类都有唯一可靠的operator<的定义,如果有,那么可以考虑为该类定义operator<。如果该类已经实现了operator==,那么,要保证定义的operator<和operator==不能同时成立,如果同时成立,将不满足严格弱序的要求
分数的关系运算符实现代码如下
bool operator>(const fraction &one, const fraction &two)
{
cout<<__func__<<endl;
fraction tmpone=one;
tmpone.numerator_*=two.denominator_;
tmpone.denominator_*=two.denominator_;
fraction tmptwo=two;
tmptwo.numerator_*=one.denominator_;
tmptwo.denominator_*=one.denominator_;
if (tmpone.denominator_>0) {
return tmpone.numerator_>tmptwo.numerator_;
}
return tmpone.numerator_<tmptwo.numerator_;
}
bool operator<(const fraction &one, const fraction &two)
{
cout<<__func__<<endl;
return !(one>two) && !(one==two);
}
bool operator>=(const fraction &one, const fraction &two)
{
cout<<__func__<<endl;
return !(one<two);
}
bool operator<=(const fraction &one, const fraction &two)
{
cout<<__func__<<endl;
return !(one>two);
}
int main(int argc, char const *argv[])
{
fraction t1(10,20),t2(-5,-10), t3(15,20), t4(-1, 30);
cout<<(t1>t2)<<endl;
cout<<(t1>=t2)<<endl;
cout<<(t4<t2)<<endl;
cout<<(t4<=t3)<<endl;
return 0;
}
四个输出结果都是正确的
下篇见https://blog.csdn.net/Master_Cui/article/details/109532391
参考
《C++ Primer》
欢迎大家评论交流,作者水平有限,如有错误,欢迎指出