c++常见的运算符重载

c++常见的运算符重载

重载运算符作用:对已有的运算符进行重新定义,赋予其另一种功能,以适应不同的数据类型

1.加号运算符重载

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

class Person
{
    
    
public:
    int m_A;
    int m_B;
    // Person operator+(Person &p)
    // {
    
    
    //     Person temp;
    //     temp.m_A = m_A + p.m_A;
    //     temp.m_B = 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 &p1, int num)
{
    
    
    Person temp;
    temp.m_A = p1.m_A + num;
    temp.m_B = p1.m_B + num;
    return temp;
}
void test()
{
    
    
    Person p1;
    p1.m_A = 10;
    p1.m_B = 20;
    Person p2;
    p2.m_A = 10;
    p2.m_B = 20;
    Person p3 = p1 + p2;
    cout << "p3 m_A = " << p3.m_A << endl;
    cout << "p3 m_B = " << p3.m_B << endl;

    Person p4 = p1 + 100;

    cout << "p4 m_A = " << p4.m_A << endl;
    cout << "p4 m_B = " << p4.m_B << endl;
}

int main()
{
    
    
    test();
    return 0;
}

2.左移运算符重载

作用:输出自定义数据类型

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

class Person
{
    
    
public:
    int m_A;
    int m_B;
};

ostream &operator<<(ostream &cout, Person &p)
{
    
    
    cout << p.m_A << " " << p.m_B << endl;
    return cout;
}

void test()
{
    
    
    Person p;
    p.m_A = 10;
    p.m_B = 10;
    cout << p << "hello world" << endl;
}

int main()
{
    
    
    test();
    return 0;
}

3.递增运算符重载

作用:通过重载递增运算符,实现自定义数据整形相加

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

class MyInteger
{
    
    
    friend ostream &operator<<(ostream &cout, MyInteger my);
public:
    MyInteger()
    {
    
    
        m_num = 0;
    }
    MyInteger &operator++()
    {
    
    
        ++m_num;
        return *this;
    }

    MyInteger operator++(int)
    {
    
    
        MyInteger temp = *this;
        m_num++;
        return temp;
    }

private:
    int m_num;
};

//前置++运算符
ostream &operator<<(ostream &cout, MyInteger my)
{
    
    
    cout << my.m_num;
    return cout;
}


void test()
{
    
    
    MyInteger myint;
    cout << ++(++myint) << endl;
    cout << myint << endl;
}

void test2()
{
    
    
    MyInteger myint;
    cout << myint++ << endl;
    cout << myint << endl;
}
int main()
{
    
    
    test();
    // test2();
    return 0;
}

4.赋值运算符重载

C++编译器默认至少给一个类添加4个函数

  • 默认构造函数(无参,函数体为空)
  • 默认析构函数(无参,函数体为空)
  • 默认拷贝构造函数,对属性值进行拷贝
  • 赋值运算符 operator=,对属性进行值拷贝

如果类中有属性指向堆区,做赋值操作时也会导致深浅拷贝问题

基础类型的

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

class Person
{
    
    
public:
    Person(int age)
    {
    
    
        m_Age = age;
    }
    int m_Age;
};

void test()
{
    
    
    Person p1(18);
    cout << "p1 =="<<p1.m_Age<<endl;
    Person p2(20);
    p1 = p2;
    cout << "p1 =="<<p1.m_Age<<endl;

}
int main()
{
    
    
    test();
    system("pause");
    return 0;
}

对象类型的

涉及到深浅拷贝问题

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

class Person
{
    
    
public:
    Person(int age)
    {
    
    
        m_Age = new int(age);
    }

    ~Person()
    {
    
    
        if (m_Age != NULL)
        {
    
    
            delete m_Age;
            m_Age = NULL;
        }
    }
    Person &operator=(Person &p)
    {
    
    
        this->m_Age = new int(*p.m_Age);
        return *this;
    }
    int *m_Age;
};

void test()
{
    
    
    Person p1(18);
    cout << "p1 ==" << *p1.m_Age << endl;
    Person p2(20);
    Person p3(22);
    p1 = p2 = p3;
    cout << "p1 ==" << *p1.m_Age << endl;
}
int main()
{
    
    
    test();
    system("pause");
    return 0;
}

5.关系运算符重载

作用:重载关系运算符,可以让两个自定义的对象进行比较

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

class Person
{
    
    
public:
    Person(string name, int age)
    {
    
    
        m_Name = name;
        m_Age = age;
    }
    bool operator==(Person &p)
    {
    
    
        if (this->m_Name == p.m_Name && this->m_Age == p.m_Age)
        {
    
    
            return true;
        }
        return false;
    }
    bool operator!=(Person &p)
    {
    
    
        if (this->m_Name == p.m_Name && this->m_Age == p.m_Age)
        {
    
    
            return false;
        }
        return true;
    }
    string m_Name;
    int m_Age;
};

void test()
{
    
    
    Person p1("Tom", 18);
    Person p2("Jerry", 18);
    if (p1 == p2)
    {
    
    
        cout << "p1 和 P2是相等的!" << endl;
    }
    else
    {
    
    
        cout << "p1 和 P2是不相等的!" << endl;
    }

    if (p1 != p2)
    {
    
    
        cout << "p1 和 P2是不相等的!" << endl;
    }
    else
    {
    
    
        cout << "p1 和 P2是相等的!" << endl;
    }
}
int main()
{
    
    
    test();
    system("pause");
    return 0;
}

6.函数调用运算符重载

  • 函数调用运算符也可以重载
  • 由于重载后,使用很想函数的调用,所以被称为仿函数
  • 仿函数没有固定写法,非常灵活
#include <iostream>
using namespace std;
#include <string>

class MyPrint
{
    
    
public:
    void operator()(string txt)
    {
    
    
        cout << txt << endl;
    }
    int operator()(int num1, int num2)
    {
    
    
        return num1 + num2;
    }
};

void test01()
{
    
    
    MyPrint myPrint;
    myPrint("hello world");   //看着像函数,实际不是
    MyPrint()("Hello World"); //匿名仿函数
    int num = myPrint(100, 300);
    cout << num << endl;
}
int main()
{
    
    
    test01();
    return 0;
}

猜你喜欢

转载自blog.csdn.net/cijiancao/article/details/107677347