C++运算符重载

Posted 皓月肥波

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C++运算符重载相关的知识,希望对你有一定的参考价值。

C++运算符重载

什么是运算符重载

  • 赋予运算符具有操作自定义类型数据功能

友元函数运算符重载

成员函数运算符重载

/*
    什么是运算符重载?
        赋予运算符具有操作自定义类型数据功能
    运算符重载的实质是什么? 
        运算符重载的实质本身就是函数调用
    运算符重载函数的写法
        函数返回值   函数名(函数参数)
    函数返回值 :运算完成后的值决定的               Complex
    函数名    : operator 加上重载运算符组成函数名  operator+ 
    参数      :看运算符的操作数,具体参数个数是要看你重载函数形式是什么
    函数体    : 写运算符具体想要的操作
*/

#include<iostream>
using namespace std;
class Complex

public:
    Complex(int a = 0, int b = 0) :a(a), b(b) 
    void print()
    
        cout << a << " " << b << endl;
    
    //友元重载: 参数个数就是操作数据
    friend Complex operator+(Complex one, Complex two);
    //类成员函数重载,参数个数等于操作减一
    //对象可以表示一个数据,所以参数应该少一个
    bool operator>(Complex object)
    
        if (this->a > object.a)
        
            return true;
        
        else if (this->a == object.a && this->b > object.b)
        
            return true;
        
        else
        
            return false;
        
    
protected:
    int a;
    int b;
;
Complex operator+(Complex one, Complex two)

    return Complex(one.a + two.a, one.b + two.b);

​
int main()

    Complex one(3,4);
    Complex two(2,1);
    Complex three;
    three = one + two;   //隐性调用,实质是operator+(one, two)
    three.print();
    if (one > two)
    
        cout << "one比较大";
    
    return 0;

流重载

为什么string可以输入输出而对象不可以

  • string类做了流重载

#include<iostream>
#include<string>
using namespace std;
int main()

    string str;
    cin >> str;    
    cout << str;
    return 0;

如何流重载

#include<iostream>
using namespace std;
//= () -> [] 只能采用类的成员函数形式重载
//流重载采用友元方式
//.  .*  ?:   :: 不能重载
class MM

public:
    MM(string name="",int age=18):name(name),age(age)
    void print()
    
        cout << name << "\\t " << age << endl;
    
    friend void operator>>(istream& in, MM& mm);
    friend void operator<<(ostream& out, MM& mm);
protected:
    string name;
    int age;
;
void operator>>(istream& in, MM& mm)

    in >> mm.name >> mm.age;

void operator<<(ostream& out, MM& mm)

    out << mm.name << " " << mm.age << endl;

int main()

    int num;
    MM mm;
    cin>> mm;      //void operator>>(istream &in,MM &mm)
    cout << mm;    //void operator<<(ostream &out,MM &mm)
    mm.print();
    return 0;

如何流重载多次输入输出

  • 上面cin>>mm>>num会报错

    • 只有当cin>>mm返回cin对象时才不会报错

#include<iostream>
using namespace std;
class MM

public:
    MM(string name="",int age=18):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 << " " << mm.age<<" ";
    return out;

int main()

    int num;
    MM mm;
    cin>>mm>>num;      
    cout << mm<<num;
    return 0;

++--运算符重载

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

#include<iostream>
using namespace std;
class MM

public:
    MM(string name="",int age=18):name(name),age(age)
    friend ostream& operator<<(ostream& out, const MM& object)
    
        out << object.name << "\\t" << object.age << endl;
        return out;
    
    MM operator++(int)          //需要一个无用参数 充当标记,区别前置后置
                               //后置
        return MM(name,age++);  
    
    MM operator++()
    
        return MM(name, ++age);
    
protected:
    string name;
    int age;
;
​
int main()

    MM mm("卷王之王",20);
    MM object = mm++;       //age=20     mm.age=21  
    cout << object<<mm;     
    object = ++mm;          //age:22     mm.age=22
    cout << object<<mm;
    return 0;

文本重载

  • 新标准中的,稍微落后一点开发工具不适用

  • 了解即可,与时间相关

#include<iostream>
#include<chrono>
#include <thread>
using namespace std;
unsigned long long  operator"" _h(unsigned long long num)

    return 60 * 60 * num;

unsigned long long  operator"" _min(unsigned long long num)

    return 60 * num;

​
int main()

    //this_thread::sleep_for(1s);
    //cout << "1s结束" << endl;
    int num = 20_h + 40_min + 30;
    cout << num << "s" << endl;
​
    return 0;

对象隐式转换

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

public:
    MM(string name, int age) :name(name), age(age) 
    //类的对象的隐式转换  operator
    operator int()            //此处为int根据类型而定
    
        return age;
    
protected:
    string name;
    int age;
;
int main()

    MM boy("卷王之王",20);
    int boyAge=boy;
    cout << boyAge << endl;
​
    return 0;

其他运算符

  • = () -> [] 只能采用类的成员函数形式重载

  • 流重载采用友元方式

  • . .* ?: :: 不能重载

重载综合案例

#include <iostream>
#include <string>
#include <functional>
#include <memory>
using namespace std;
class Int

public:
    Int(int num) :num(num) 
    int& data() 
    
        return num;
    
    string tostr() 
    
        return to_string(num);
    
    //算术运算符重载
    Int operator+(const Int& value) 
    
        return Int(this->num + value.num);
    
    //友元重载 :操作数-1 等于重载函数的参数个数
    friend Int operator-(const Int& one, const Int& two) 
    
        //operator-(one,two);
        return Int(one.num - two.num);
    
    Int operator+=(const int& value) 
    
        return Int(this->num + value);
    
    Int operator+=(const Int& value)
    
        return Int(this->num + value.num);
    
​
    Int operator++(int) 
    
        return Int(this->num++);
    
    Int operator++() 
    
        return Int(++this->num);
    
​
    Int operator&(const Int& value) 
    
        return Int(this->num & value.num);
    
    bool operator!() 
    
        return !this->num;
    
    Int operator-() 
    
        return Int(-this->num);
    
​
    friend ostream& operator<<(ostream& out, const Int& object) 
    
        out << object.num << endl;
        return out;
    
    friend  istream& operator>>(istream& in, Int& object) 
    
        in >> object.num;
        return in;
    
    int* operator&() 
    
        return &this->num;
    
    bool operator>(const Int& object) 
    
        return this->num > object.num;
    
​
protected:
    int num;
;

以上是关于C++运算符重载的主要内容,如果未能解决你的问题,请参考以下文章

运算符 + 重载 C++

C++运算符重载

c++ 运算符重载问题

C++重载'--'后缀运算符

C++ 运算符重载

C++运算符的重载