第十一章——运算符重载

1.运算符重载是为了使类的对象能像基本的数据类型一样进行加减乘除等运算

2.不能重载的操作符:.   ?:   .*   ::   sizeof

3.运算符重载为成员函数

#include <iostream>

using namespace std;
class rational {
private:
    int num;
    int den;
    void reductfraction();//约分
public:
    rational(int n=0,int d=1) {num=n;den=d;reductfraction();}
    rational operator+(const rational &r1) const;
    rational operator*(const rational &r1) const;
    bool operator<(const rational &r1) const;
    bool operator==(const rational &r1) const;
    void display() const {cout<<num<<'/'<<den;}



};
void rational::reductfraction()//返回值类型 类名 函数名 参数
{
    int tmp=(num>den)?den:num;
    for(;tmp>1;--tmp)
        if(num%tmp==0&&den%tmp==0)
    {
        num/=tmp;den/=tmp; break;
    }
}
rational rational::operator+(const rational &r1) const
{
    rational tmp;
    tmp.num=num*r1.den+r1.num*den;
    tmp.den=den*r1.den;
    tmp.reductfraction();
    return tmp;
}

rational rational::operator*(const rational &r1) const
{
    rational tmp;
    tmp.num=num*r1.num;
    tmp.den=den*r1.den;
    tmp.reductfraction();
    return tmp;
}

bool rational::operator<(const rational &r1) const
{
    return num*r1.den<den*r1.num;
}


bool rational::operator==(const rational &r1) const
{
    return num==r1.num&&den==r1.den;
}
int main()
{
    rational r1(1,6),r2(1,3),r3;
    r3=r1+r2;
    r1.display();cout<<"+"; r2.display();
    cout<<"="; r3.display(); cout<<endl;
    cout<<(r1==r2?"true":"false")<<endl;

    return 0;
}
/* const对象只能调用const成员函数。
  const对象的值不能被修改,在const成员函数中修改const对象数据成员的值是语法错误
  在const函数中调用非const成员函数是语法错误
  */

4.当运算符重载为全局函数时,通常把此重载函数设为类的友元函数,便于访问类的私有成员

#include <iostream>

using namespace std;
class rational {
    friend rational operator+(const rational &r1,const rational &r2) ;
    friend rational operator*(const rational &r1,const rational &r2) ;
    friend bool operator<(const rational &r1,const rational &r2) ;
    friend bool operator==(const rational &r1,const rational &r2) ;
private:
    int num;
    int den;
    void reductfraction();//约分
public:
     rational(int n=0,int d=1) {num=n;den=d;reductfraction();}

    void display() const {cout<<num<<'/'<<den;}



};
void rational::reductfraction()//返回值类型 类名 函数名 参数
{
    int tmp=(num>den)?den:num;
    for(;tmp>1;--tmp)
        if(num%tmp==0&&den%tmp==0)
    {
        num/=tmp;den/=tmp; break;
    }
}
rational operator+(const rational &r1,const rational &r2)
{
    rational tmp;
    tmp.num=r2.num*r1.den+r1.num*r2.den;
    tmp.den=r2.den*r1.den;
    tmp.reductfraction();
    return tmp;
}

rational operator*(const rational &r1,const rational &r2)
{
    rational tmp;
    tmp.num=r2.num*r1.num;
    tmp.den=r2.den*r1.den;
    tmp.reductfraction();
    return tmp;
}

bool operator<(const rational &r1,const rational &r2)
{
    return r1.num*r2.den<r1.den*r2.num;
}


bool operator==(const rational &r1,const rational &r2)
{
    return r2.num==r1.num&&r2.den==r1.den;
}
int main()
{
    rational r1(1,6),r2(1,3),r3;
    r3=r1+r2;
    r1.display();cout<<"+"; r2.display();
    cout<<"="; r3.display(); cout<<endl;
    cout<<(r1==r2?"true":"false")<<endl;

    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_42183904/article/details/80585931