C++day4 匿名对象、友元、常成员函数和常对象、运算符重载

作业

实现关系运算符的重载

#include <iostream>

using namespace std;
class B;
class A
{
    friend const B operator%(const A &s,const A &d);
    friend const B operator/(const A &s,const A &d);
    friend const B operator*(const A &s,const A &d);
    friend const B operator-(const A &s,const A &d);
    friend const B operator+(const A &s,const A &d);
private:
    int n;
    int a;
public:
    A(){}
    A(int n,int a):n(n),a(a)
    {}
    void show()
    {
        cout << n << " " << a <<endl;
    }
    //成员函数实现运算符重载
    //结果不被改变   右操作数不被改变 左操作数不被改变
//    const A operator+(const A &s) const
//    {
//        A temp;
//        temp.a = s.a + d.a + s.n + d.n;
//        temp.n = s.a + d.a + s.n + d.n;
//        return temp;
//    }

    bool operator<(const A &d) const
    {
        return (this->a + this->n) < (d.a + d.n);
    }
    bool operator>(const A &d) const
    {
        return (this->a + this->n) > (d.a + d.n);
    }
    bool operator==(const A &d) const
    {
        return (this->a + this->n) == (d.a + d.n);
    }
    bool operator!=(const A &d) const
    {
        return (this->a + this->n) != (d.a + d.n);
    }
};
class B
{
    friend const B operator%(const A &s,const A &d);
    friend const B operator/(const A &s,const A &d);
    friend const B operator*(const A &s,const A &d);
    friend const B operator-(const A &s,const A &d);
    friend const B operator+(const A &s,const A &d);
private:
    int b;
public:
    B(){}
    B(int a):b(a){}
    void show()
    {
        cout << b << endl;
    }
    bool operator<=(const B &a) const
    {
        return this->b <= a.b;
    }
    bool operator>=(const B &a) const
    {
        return this->b >= a.b;
    }
    bool operator!=(const B &a) const
    {
        return this->b != a.b;
    }
};

//全局函数实现运算符重载
const B operator+(const A &s,const A &d)
{
    B temp;
    temp.b = s.a + d.a + s.n + d.n;
    return temp;
}

const B operator-(const A &s,const A &d)
{
    B temp;
    temp.b = (s.a + s.n) - (d.a + d.n);
    return temp;
}
const B operator*(const A &s,const A &d)
{
    B temp;
    temp.b = (s.a + s.n) * (d.a + d.n);
    return temp;
}
const B operator/(const A &s,const A &d)
{
    B temp;
    temp.b = (s.a + s.n) / (d.a + d.n);
    return temp;
}
const B operator%(const A &s,const A &d)
{
    B temp;
    temp.b = (s.a + s.n) % (d.a + d.n);
    return temp;
}
/*
const bool operator<(const A &s,const A &d)
{
    return (s.a + s.n) < (d.a + d.n);
}
const bool operator>(const A &s,const A &d)
{
    return (s.a + s.n) > (d.a + d.n);
}
const bool operator==(const A &s,const A &d)
{
    return (s.a + s.n) == (d.a + d.n);
}*/
int main()
{
    int n1,n2,n3,n4,n5,n6;
    cin >> n1;
    cin >> n2;
    cin >> n3;
    cin >> n4;
    cin >> n5;
    cin >> n6;
    A a(n1,n2);
    a.show();
    A b(n3,n4);
    b.show();
    B d(n5);
    B e(n6);
    B c = a+b;
    c.show();
    c = a-b;
    c.show();
    c = a*b;
    c.show();
    c = a/b;
    c.show();
    c = a%b;
    c.show();
    if(a<b)
    {
        cout << "a < b" << endl;
    }
    else if(a>b)
    {
        cout << "a > b" << endl;
    }
    if(a != b)
    {
        cout << "a != b" << endl;
    }
    else
    {
        cout << "a = b" << endl;
    }
    if(d <= e)
    {
        cout << "d <= e" << endl;
    }
    else if(d >= e)
    {
        cout << "d >= e" << endl;
    }
    if (d != e)
    {
        cout << "d != e" << endl;
    }
    return 0;
}

思维导图

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值