栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++运算符重载

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++运算符重载

C++运算符重载 什么是运算符重载
  • 赋予运算符具有操作自定义类型数据功能

友元函数运算符重载 成员函数运算符重载
 

#include
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
#include
using namespace std;
int main()
{
    string str;
    cin >> str;    
    cout << str;
    return 0;
}
如何流重载
#include
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
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< 

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

#include
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< 

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

  • 了解即可,与时间相关

#include
#include
#include 
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
using namespace std;
#include
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 
#include 
#include 
#include 
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;
};

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/656799.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号