C++ 重载

1. 加号运算符重载

// 1. 加号运算符重载
/**
 概念:对已有的运算符重新进行定义,赋予其另一种功能,以适应不同的数据类型
 */
class Person
{
public:
    // 1. 成员函数重载+号
//    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;
//    }
    
    int m_A;
    int m_B;
};

// 2. 也可以用全局函数重载+号
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;
}

int main(int argc, const char * argv[]) {
    Person p;
    p.m_A = 10;
    p.m_B = 10;
    
    Person p1;
    p1.m_A = 10;
    p1.m_B = 10;
    
    Person p2;
    p2 = p1 + p;
    
    cout << "m_A: " << p2.m_A << "m_B: " << p2.m_B << endl;
    
    return 0;
}

2. 左移运算符重载

// 2. 左移运算符重载
/**
 概念:对已有的运算符重新进行定义,赋予其另一种功能,以适应不同的数据类型
 */
class Person
{
    friend ostream& operator<<(ostream &cout, Person &p);
public:
    Person(int a, int b)
    {
        m_A = a;
        m_B = b;
    }
private:
    int m_A;
    int m_B;
};

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

int main(int argc, const char * argv[]) {
    Person p(10,10);
    cout << p << endl; // 打印结果:m_A = 10 m_B = 10
 
    
    return 0;
}

3. 递增运算符重载

class MyInt
{
    friend ostream& operator<<(ostream &cout, MyInt myint);
public:
    // 前置递增: ++a
    MyInt& operator++()
    {
        value++;
        return *this;
    }
    
    // 后置递增: a++
    MyInt operator++(int) // 后置递增需要一个占位符来区分
    {
        MyInt temp = *this;
        value++;
        return temp;
    }
    
    // 前置递减:--a
    MyInt& operator--()
    {
        value--;
        return *this;
    }
    // 后置递减:a--
    MyInt operator--(int)
    {
        MyInt temp = *this;
        value--;
        return temp;
    }
    
    MyInt()
    {
        value = 0;
    }
private:
    int value;
};

ostream& operator<<(ostream &cout, MyInt myint)
{
    cout << myint.value << endl;
    return cout;
}


4. 赋值运算符重载

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)
    {
        if (m_age != NULL)
        {
            delete m_age;
            m_age = NULL;
        }
        m_age = new int(*p.m_age); // 对传进来的对象,进行深拷贝
        
        return *this;
    }
    
    int *m_age;
};


int main(int argc, const char * argv[]) {
    
    Person p1(19);
    
    Person p2(20);
    
    Person p3(21);
    
    p2 = p1 = p3;
    
    cout << "p1的年龄:" << *p1.m_age << endl;
    cout << "p2的年龄:" << *p2.m_age << endl;
    
    return 0;
}

5. 关系运算符重载

class Person
{
    
public:
    Person(int age, int height)
    {
        _age = age;
        _height = height;
    }
    
    bool operator==(Person &p)
    {
        if (this->_age == p._age && this->_height == p._height)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    
    int _age;
    int _height;
   
};


int main(int argc, const char * argv[]) {
    Person p1(20,20);
    Person p2(20,20);
    
    if (p1 == p2)
    {
        cout << "相等" << endl;
    }
    else
    {
        cout << "不相等" << endl;
    }
    
    return 0;
}

6. 函数调用运算符重载

/**
 1. 函数调用运算符()也可以重载
 2. 由于重载后使用的方式非常像函数的调用,因此称为仿函数
 3. 仿函数没有固定写法,非常灵活
 */

class MyPrint
{
public:
    void operator()(string test)
    {
        cout << test << endl;
    }
};

void MyPrint2(string test)
{
    cout << test << endl;
}

class MyAdd
{
public:
    int operator()(int v1, int v2)
    {
        return v1 + v2;
    }
};

int main(int argc, const char * argv[]) {
    MyPrint myprint;
    myprint("hello");
    
    MyPrint2("hello --");
    
    MyAdd myadd;
    cout << myadd(1,2) << endl;
    cout << MyAdd()(2,3) << endl; // 匿名对象
    
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值