8.24 拷贝赋值函数 匿名对象 友元 常成员函数 运算符重载

 

#include <iostream>

using namespace std;

class Stu
{
    friend const Stu operator+(const Stu &L, const Stu &R);
    friend const Stu operator-(const Stu &L, const Stu &R);
    friend const Stu operator*(const Stu &L, const Stu &R);
    friend const Stu operator/(const Stu &L, const Stu &R);
    friend bool operator>(const Stu &L, const Stu &R);
    friend bool operator>=(const Stu &L, const Stu &R);
    friend bool operator<(const Stu &L, const Stu &R);
    friend bool operator<=(const Stu &L, const Stu &R);
    friend bool operator==(const Stu &L, const Stu &R);
    friend bool operator!=(const Stu &L, const Stu &R);
    int a;
    int b;
public:
    Stu(int a = 0, int b = 0):a(a), b(b){}
    void show(){
        cout << "a = " << a << endl;
        cout << "b = " << b << endl;
    }
//    const Stu operator+(const Stu &R) const{
//        Stu temp;
//        temp.a = a + R.a;
//        temp.b = b + R.b;
//        return temp;
//    }
//    Stu(const Stu &other):a(other.a), b(other.b){
//        cout << "拷贝构造函数" << endl;
//    }
    Stu &operator=(const Stu &other){
        a = other.a;
        b = other.b;
        cout << "拷贝赋值函数" << endl;
        return *this;
    }
};
const Stu operator+(const Stu &L, const Stu &R){
    Stu temp;
    temp.a = L.a + R.a;
    temp.b = L.b + R.b;
    return temp;
}
const Stu operator-(const Stu &L, const Stu &R){
    Stu temp;
    temp.a = L.a - R.a;
    temp.b = L.b - R.b;
    return temp;
}
const Stu operator*(const Stu &L, const Stu &R){
    Stu temp;
    temp.a = L.a * R.a;
    temp.b = L.b * R.b;
    return temp;
}
const Stu operator/(const Stu &L, const Stu &R){
    Stu temp;
    temp.a = L.a / R.a;
    temp.b = L.b / R.b;
    return temp;
}
bool operator>(const Stu &L, const Stu &R){
    return (L.a > R.a) && (L.b > R.b);
}
bool operator>=(const Stu &L, const Stu &R){
    return (L.a >= R.a) && (L.b >= R.b);
}
bool operator<(const Stu &L, const Stu &R){
    return (L.a < R.a) && (L.b < R.b);
}
bool operator<=(const Stu &L, const Stu &R){
    return (L.a <= R.a) && (L.b <= R.b);
}
bool operator==(const Stu &L, const Stu &R){
    return (L.a == R.a) && (L.b == R.b);
}
bool operator!=(const Stu &L, const Stu &R){
    return (L.a != R.a) && (L.b != R.b);
}
int main()
{
    Stu p1(1,1);
    Stu p2(2,2);

    Stu p3 = p1 + p1;
    p3.show();
   cout << "---------------" << endl;
//    Stu p4 = p1 - p2;
//    p4.show();

//    Stu p5 = p1 * p2;
//    p5.show();

//    Stu p6 = p1 / p2;
//    p6.show();

    if(p3 > p1)
        cout << "p3 > p1" << endl;

    if(p2 >= (p1+p1))
        cout << "p2 >= p1+p1" << endl;

    if(p1 < p2)
        cout << "p1 < p2" << endl;

    if(p1 <= p2)
        cout << "p1 <= p2" << endl;

    if(p3 == p2)
        cout << "p3 == p2" << endl;

    if(p1 != p2)
        cout << "p1 != p2" << endl;
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值