C++运算符重载

 友元函数重载运算符 

        运算符重载:赋予运算符具有操作自定义类型数据功能;运算符重载的实质就是函数调用。

写法:         函数返回值  函数名(函数参数)

函数返回值是由运算符运算完成后的值决定;函数名是由 operator加上重载运算符组成函数名;

参数:看运算符的操作数,具体参数个数看重载函数形式是什么。

#include<iostream>
using namespace std;
calss AA
{
public:
    AA(int a,int b):a(a),b(b){}
    void print()
    {
        cout << a << "\t" << b << endl;
    }
    //友元运算符重载
    friend AA operator+(AA aa,AA bb);

    //类的成员函数重载,参数个数等于操作个数-1
    bool operator>(AA aa)
    {
        if(this->a > aa.a)
        {    
            return true;
        }
        else if(this->a == aa.a && this->b > aa.b)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    bool operator<(AA aa);

protected:
    int a;
    int b;
};

//友元重载:参数个数就是操作数据
AA operator+(AA aa,AA bb)
{
    return AA(aa.a + bb.a,aa.b + bb.b);
}

bool AA::operator<(AA.aa)
{
    if(this->a < aa.a)
    {
        return true;
    }else if(this->a == aa.a && this->b < aa.b)
    {
        return true;
    }else
    {
        return false;
    }
}

int main()
{
    AA aa(1,1);
    AA bb(2,2);
    AA cc;
    AA dd;
    cc = aa + bb;            //重载 + 号,返回值AA,重载函数的隐式调用
    cc.print();
    dd = operator+(aa,bb);   //重载函数显示调用
    dd.print();

    if(aa > bb)              //重载函数隐式调用
    {
        cout << "aa 比较大" << endl;
    }

    if(aa.operator<(bb))     //重载函数显示调用
    {
        cout << "aa 比较小" << endl;
    }
    
    return 0;
}


特殊运算符重载

        流运算符重载

        cin类型:istream类的对象;                                cout类型:ostream类的对象。  

        流运算符:  <<  >>

      流运算符重载必须采用友元函数重载!!!

#include<iostream>
#include<string>
using namespace std;
class MM
{
public:
    MM(string name = " ",int age = 0):name(name),age(age){}
    friend istream& operator>>(istream& in,MM& mm);
    friend ostream& operator<<(ostream& out,MM& mm);

protected:
    string name;
    int age;
};

istream& operator>>(istream& in,MM& mm)
{
    in >> mm.name >> mm.age;
    return in;
}
ostream& operator<<(ostream& out,MM& mm)
{
    out << mm.name << "\t" << mm.age << endl;
    return out;
}

int main()
{
    int num;
    MM mm;
    //只有返回引用才能连续流入
    cin >> num >> mm;        
    cout << num << mm;

    return 0;
}

++ --运算符重载

        为了解决前置和后置问题,增加无用参数 int去表示当前运算符重载是后置操作

#include<iostream>
#include<string>
using namespace std;

class MM
{
public:
    MM(string name,int age):name(name),age(age){}

    friend ostream& operator<<(ostream& out,MM& object)
    {
        out << object.name << "\t" << object.age << endl;
        return out;
    }

    MM operator++(int)    //需要一个无用参数int,充当后加加标记。 --同理
    {
        return MM(name,age++);
    }

    MM operator++()
    {
        return MM(name,++age);
    }
    //类的对象的隐式转换  operator +类型
    operator int()
    {
        return age;
    }
protected:
    string name;
    int age;
};

int main()
{
    MM mm("小芳",19);
    cout << MM ;

    MM object = mm++;
    cout << object << mm ;
    object = ++mm;
    cout << object << mm;

    int mmAge = mm;
    cout << mmAge << endl;

    return 0;
}

文本重载

#include<iostream>
using namespace std;

unsigned long long operator"" _h(unsigned long long num)
{
    return 60 * 60;
}
unsigned long long operator"" _min(unsigned long long num)
{
    return 60;
}

int main()
{
    int time = 2_h + 23_min + 31;
    cout << time << "秒" << endl;
    
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值