0
点赞
收藏
分享

微信扫一扫

程序设计B运算符重载

程序员伟杰 2022-04-16 阅读 44

闲聊一下:

  • 终于弄懂了,只想这么说一句!

今天主要复习一下:

  • 加减运算符重载
  • 左移运算符重载
  • 右移运算符重载
  • 递增,递减(前置,后置)运算符重载
  • 赋值运算符重载
  • 关系运算符重载
  • 括号运算符重载(还没有更新)
#include <iostream>
using namespace std;
//加号运算符
class jiahuao {
public:
    //成员函数
    void get_num(int a, int b) {
        m_a = a;
        m_b = b;
    }
    jiahuao operator+(jiahuao&p) {
        jiahuao temp;
        temp.m_a = this->m_a + p.m_a;
        temp.m_b = this->m_b + p.m_b;
        return temp;
    }
    int shuchu_a() {
        return m_a;
    }
    int shuchu_b() {
        return m_b;
    }
    //全局函数
    /*jiahuao operator+(jiahuao& p1, jiahuao& p2) {
        jiahuao temp;
        temp=p1.m_a+p2.m_a;
        temp=p1.m_b+p2.m_b;
        return temp;
    }*/
private:
    int m_a;
    int m_b;
};
//加号运算符重载测试1
void test1() {
    jiahuao A;
    jiahuao B;
    jiahuao C;
    A.get_num(10, 5);
    B.get_num(20, 10);
    C = A + B;
    cout << C.shuchu_a() << " " << C.shuchu_b() << endl;
}
//减号运算符重载
class jianhao {
public:
    //成员函数
    void get_num(int a, int b) {
        m_a = a;
        m_b = b;
    }
    jianhao operator-(jianhao& p) {
        jianhao temp;
        temp.m_a = this->m_a - p.m_a;
        temp.m_b = this->m_b - p.m_b;
        return temp;
    }
    int shuchu_a() {
        return m_a;
    }
    int shuchu_b() {
        return m_b;
    }
    //全局函数
    /*jianhao operator-(jianhao& p1, jianhao& p2) {
        jianhao temp;
        temp=p1.m_a-p2.m_a;
        temp=p1.m_b-p2.m_b;
        return temp;
    }*/
private:
    int m_a;
    int m_b;
};
//减号运算符测试
void test2() {
    jianhao A;
    jianhao B;
    jianhao C;
    A.get_num(5, 10);
    B.get_num(4, 5);
    C = A - B;
    cout << C.shuchu_a() << " " << C.shuchu_b() << endl;
}
//左移右移运算符
class liu {
public:
    friend istream& operator>>(istream& in, liu& p); 
    friend ostream& operator<<(ostream& out, liu& p);
private:
    int m_a;
    int m_b;
};
istream& operator>>(istream& in, liu& p) {
    in >> p.m_a;
    in >> p.m_b;
    return in;
}
ostream& operator<<(ostream& out, liu& p) {
    out << "a:" << p.m_a << endl;
    out << "b:" << p.m_b << endl;
    return out;
}
void test3() {
    liu A;
    //输入 10 5
    cin >> A;
    cout << A;
}
//递增运算符重载
class dizeng {
public:
    dizeng(int a, int b) {
        n_a = a;
        n_b = b;
    }
    //前置运算符重载
    dizeng& operator++() {
        n_a++;
        n_b++;
        return *this;
    }
    dizeng operator++(int) {
        dizeng temp= *this;
        n_a++;
        n_b++;
        return temp;
    }
    friend ostream& operator<<(ostream& out, dizeng p);

private:
    int n_a;
    int n_b;
};
ostream& operator<<(ostream& out, dizeng p) {
    out << "a:" << p.n_a << endl;
    out << "b:" << p.n_b << endl;
    return out;
}
//前置,先++,再返回
void test4() {
    dizeng p(5, 10);
    cout << p;
    cout << ++p;

}
//后置++
void test5() {
    dizeng p(5, 10);
    cout << p++;//?
    cout << p;
}
//递减
class dijian {
public:
    dijian(int a) {
        n_age = a;
        
    }
    //前置运算符重载
    dijian& operator--() {
        n_age--;
        return *this;
    }
    dijian operator--(int) {
        dijian temp = *this;
        n_age--;
        return temp;
    }
    friend ostream& operator<<(ostream& out, dijian p);

private:
    int n_age;
    
};
ostream& operator<<(ostream& out, dijian p) {
    out << "age:" <<p.n_age<< endl;
    return out;
}
//前置,先--,再返回
void test6() {
    dijian p(5);
    cout << p;
    cout << --p;

}
//后置--
void test7() {
    dijian q(5);
    cout << q--;//?
    cout << q;
}
//赋值运算符重载,牵扯到深拷贝的问题,需要特别注意,我们的本意并不是仅仅复制值,要把深层次的东西也给复制进去
class fuzhi {
public:
    fuzhi(int nums) {
        n_nums = new int(nums);
    }
    fuzhi& operator=(fuzhi& p) {
        if (n_nums != NULL) {
            delete n_nums;//直接将原来的空间给释放掉
            n_nums = NULL;
        }
        n_nums = new int(*p.n_nums);//重新生成一个新空间深拷贝
        return *this;
    }
    ~fuzhi()
    {
        if (n_nums != NULL) {
            delete n_nums;
            n_nums = NULL;
        }
    }
    int* n_nums;
};
//赋值运算符
void test8() {
    fuzhi A(5);
    fuzhi B(10);
    fuzhi C(30);
    C = B = A;
    cout << "A:" << *A.n_nums << endl;
    cout << "B:" << *B.n_nums << endl;
    cout << "C:" << *C.n_nums << endl;
}
//关系运算符重载
class guanxi {
public:
    guanxi(string name, int age) {
        m_name = name;
        m_age = age;
    }
    bool operator==(guanxi& p) {
        if (m_name == p.m_name && m_age == p.m_age) {//两个相等
            return true;
        }
        else {
            return false;
        }
    }
private:
    string m_name;
    int m_age;
};
void test9() {
    guanxi A("aabb", 15);
    guanxi B("aabb", 15);
    if (A == B) {
        cout << "两个变量相等。" << endl;
    }
    else {
        cout << "两个变量不相等。" << endl;
    }
}
int main()
{
    //加号运算符重载测试1
    test1();
    //减号运算符测试2
    test2();
    //输入输出流测试3
    //输入10 5
    test3();
    //前置++测试4
    test4();
    //后置++测试5
    test5();
    //前置--测试6
    test6();
    //后置--测试7
    test7();
    //赋值运算符重载测试8
    test8();
    关系运算符重载测试9
    test9();
}
举报

相关推荐

0 条评论