运算符重载

递增运算符重载

前置递增
MyInteger &MyInteger::operator++()
  • 为什么要用引用
cout << ++(++myint) << endl;
cout << myint;

如果不加引用,这段代码的结果就是 2 1
原因是做了一次递增后 返回的是一个新的对象 所以要返回引用

#include<iostream>
using namespace std;
class MyInteger
{
private:
    int m_num;
public:
    MyInteger()
    {
        m_num = 0;
    }
    MyInteger &operator++();
    friend ostream& operator<<(ostream& cout, MyInteger myint);
};
//重载左移运算符
ostream& operator<<(ostream& cout, MyInteger myint)
{
    cout << myint.m_num;
    return cout; 
}
//重载前置++运算符
MyInteger &MyInteger::operator++()
{
    m_num++;
    return *this;    //将自身返回
}
void test01()
{
    MyInteger myint;
    //cout << ++myint;
    cout << ++(++myint) << endl;
    cout << myint;
    //如果不加引用,输出结果就是 2 1
    //原因是做了一次递增后 返回的是一个新的对象 所以要返回引用
}
int main()
{
    test01();
    return 0;
}
后置递增
MyInteger MyInteger::operator++(int)
  • 返回值
  • int 占位符
//重载后置++运算符
MyInteger MyInteger::operator++(int)  //int是占位参数 用来区分 前者递增 和 后置递增
{                                     //后置递增返回值 不能返回引用
    //先记录当前结果
    MyInteger temp = *this;
    //再递增
    m_num++;
    //最后返回记录的结果
    return temp;
}

总结

前置递增返回引用,后置递增返回值

递减运算符重载

前置递减
  • 引用
  • 返回自身
    MyIntegar &operator--()
    {
        m_num--;
        return *this;
    }
后置递减
  • int 占位
  • 值传递,不加引用
   MyIntegar operator--(int)
    {
        MyIntegar temp = *this;
        m_num--;
        return temp;
    }
完整代码
#include <iostream>
using namespace std;
class MyIntegar
{
private:
    int m_num;

public:
    MyIntegar()
    {
        m_num = 0;
    }
    //重载左移运算符
    friend ostream &operator<<(ostream &cout, MyIntegar myint)
    {
        cout << myint.m_num;
        return cout;
    }
    //重载前置--运算符
    MyIntegar &operator--()
    {
        m_num--;
        return *this;
    }
    //重载后置--运算符
    MyIntegar operator--(int)
    {
        MyIntegar temp = *this;
        m_num--;
        return temp;
    }
};
void test01()
{
    MyIntegar myint;
    cout << --myint << endl;
}
void test02()
{
    MyIntegar myint;
    cout << myint-- << endl;
    cout << myint << endl;
}
int main()
{
    // test01();
    test02();
    return 0;
}

加法运算符重载

手撕

![[Pasted image 20231015185546.png]]

类成员函数
#include <iostream>
using namespace std;
class Person
{
public:
    int m_A;
    int m_B;
    Person();
    Person(int a, int b);
    Person operator+(Person &p);
    void show();
};
Person::Person()
{
    m_A = 0;
    m_B = 0;
}
Person::Person(int a, int b)
{
    m_A = a;
    m_B = b;
}
void Person::show()
{
    cout << "m_A=" << m_A << endl;
    cout << "m_B=" << m_B << endl;
}
//自己写一个成员函数,实现两个对象属性相加返回新的对象
Person Person::operator+(Person &p)
{
    Person temp;
    temp.m_A = this->m_A + p.m_A;
    temp.m_B = this->m_B + p.m_B;
    return temp;
}
void test01()
{
    Person p1(10, 10);
    Person p2(20, 20);
    Person p3 = p1.operator+(p2);
    p3.show();
}
int main()
{
    test01();
    return 0;
}
全局函数
#include<iostream>
using namespace std;
class Person
{
public:
    int m_A;
    int m_B;
    Person();
    Person(int a, int b);
    //Person operator+(Person& p);
    void show();
};
Person::Person()
{
    m_A = 0;
    m_B = 0;
}
Person::Person(int a, int b)
{
    m_A = a;
    m_B = b;
}
void Person::show()
{
    cout << "m_A=" << m_A << endl;
    cout << "m_B=" << m_B << endl;
}
自己写一个成员函数,实现两个对象属性相加返回新的对象
//Person Person::operator+(Person& p)
//{
//    Person temp;
//    temp.m_A = this->m_A + p.m_A;
//    temp.m_B = this->m_B + p.m_B;
//    return temp;
//}
Person operator+(Person& p1, Person& p2)
{
    Person temp;
    temp.m_A = p1.m_A + p2.m_A;
    temp.m_B = p1.m_B + p2.m_B;
    return temp;
}
void test01()
{
    Person p1(10, 10);
    Person p2(20, 20);
    p1.show();
    cout << "-----------" << endl;
    p2.show();
    cout << "-----------" << endl;
    Person p3;
    //p3 = p1.PersonAddPerson(p2);
    p3 = operator+(p1, p2);
    p3.show();
}
int main()
{
    test01();
    return 0;
}

将函数名改为 operator+ 可以直接使用 p3=p1+p2 调用
但本质是 p3=p1.operrator+(p2)p3=operator(p1,p2)

运算符重载也可以重载函数

#include <iostream>
using namespace std;
class Person
{
public:
    int m_A;
    int m_B;
    Person();
    Person(int a, int b);
    // Person operator+(Person &p);
    void show();
};
Person::Person()
{
    m_A = 0;
    m_B = 0;
}
Person::Person(int a, int b)
{
    m_A = a;
    m_B = b;
}
void Person::show()
{
    cout << "m_A=" << m_A << endl;
    cout << "m_B=" << m_B << endl;
}
//自己写一个成员函数,实现两个对象属性相加返回新的对象
// Person Person::operator+(Person &p)
// {
//     Person temp;
//     temp.m_A = this->m_A + p.m_A;
//     temp.m_B = this->m_B + p.m_B;
//     return temp;
// }
Person operator+(Person &p1, Person &p2)
{
    Person temp;
    temp.m_A = p1.m_A + p2.m_A;
    temp.m_B = p1.m_B + p2.m_B;
    return temp;
}
Person operator+(Person &p, int num)
{
    Person temp;
    temp.m_A = p.m_A + num;
    temp.m_B = p.m_B + num;
    return temp;
}
void test01()
{
    Person p1(10, 10);
    Person p2(20, 20);
    // Person p3 = p1.operator+(p2);
    // p3.show();
    Person p3 = p1 + p2;
    Person p4 = p1 + 100;
    p3.show();
    p4.show();
}
int main()
{
    test01();
    return 0;
}

左移运算符重载

  • 只能用全局函数实现
    • 成员函数重载左移运算符 p.operator<<(cout) <=> p<<cout
    • 无法实现 cout 在左侧 所以一般不用成员函数重载<<运算符
  • 如果属性是私有的,可以用友元函数实现
#include <iostream>
using namespace std;
class Person
{
public:
    int m_A;
    int m_B;
    //成员函数重载左移运算符   p.operator<<(cout)  <=>  p<<cout
    //无法实现cout在左侧  所以一般不用成员函数重载<<运算符
};
//只能用全局函数实现
ostream &operator<<(ostream &cout, Person &p) 
// operator<<(p)  <=>  cout<<p    cout只有一个,要用引用    
//为了后面还能继续追加 <<"..."  要返回cout类型ostream  
{
    cout << "m_A=" << p.m_A << " "    
         << "m_B=" << p.m_B << endl;
         return cout;
}
void test01()
{
    Person p;
    p.m_A = 10;
    p.m_B = 10;
    cout << p << endl;
}
int main()
{
    test01();
    return 0;
}

关系运算符重载

 //重载关系运算符
 bool operator==(Person& p)
 {
     if (m_Name == p.m_Name && m_Age == p.m_Age)
         return true;
     return false;
 }

函数调用运算符重载

  • 使用起来很像函数调用 所以又叫仿函数
 int operator()(int a, int b)
 {
     return a + b;
 }
匿名对象

匿名对象会被创建并立即调用,输出结果 200。输出完成后马上被删除

    //匿名函数对象
    cout << MyAdd()(100, 100) << endl;
完整代码
class MyPrint
{
public:
    //重载函数调用符
    void operator()(string test)
    {
        cout << test << endl;
    }
};
class MyAdd
{
public:
    int operator()(int a, int b)
    {
        return a + b;
    }
};
void test01()
{
    MyPrint myprint;
    myprint("Hello World");  //使用起来很像函数调用 所以叫仿函数
}
void test02()
{
    MyAdd myadd;
    int ret = myadd(10, 20);
    cout << "10+20=" << ret << endl;
    //匿名函数对象
    cout << MyAdd()(100, 100) << endl;
}
int main()
{
    //test01();
    test02();
    return 0;
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值