c++ day4

cpp 复制代码
#include <iostream>

using namespace std;
class stu
{
    friend const bool operator>(const stu &s1,const stu &s2);
    friend const bool operator<(const stu &s1,const stu &s2);
    friend const bool operator>=(const stu &s1,const stu &s2);
    friend const bool operator<=(const stu &s1,const stu &s2);
    friend const bool operator==(const stu &s1,const stu &s2);
    int a;
    int b;
public:
    stu(int a,int b):a(a),b(b)
    {
        cout << "有参构造" << endl;
    }

};
const bool operator>(const stu &s1,const stu &s2)//类外+号运算符重载 本质:s3=operator +(s1+s2)
{
    if(s1.a>s2.a&&s1.b>s2.b)
    {
        return true;
    }
    else
    {
        return false;
    }
 }
const bool operator<(const stu &s1,const stu &s2)//类外+号运算符重载 本质:s3=operator +(s1+s2)
 {
   if(s1.a<s2.a&&s1.b<s2.b)
   {
       return true;
   }
   else
   {
       return false;
   }

}
const bool operator>=(const stu &s1,const stu &s2)//类外+号运算符重载 本质:s3=operator +(s1+s2)
 {
   if(s1.a>=s2.a&&s1.b>=s2.b)
   {
       return true;
   }
   else
   {
       return false;
   }

}
const bool operator<=(const stu &s1,const stu &s2)//类外+号运算符重载 本质:s3=operator +(s1+s2)
 {
   if(s1.a<=s2.a&&s1.b<=s2.b)
   {
       return true;
   }
   else
   {
       return false;
   }

}
const bool operator==(const stu &s1,const stu &s2)//类外+号运算符重载 本质:s3=operator +(s1+s2)
 {
   if(s1.a==s2.a&&s1.b==s2.b)
   {
       return true;
   }
   else
   {
       return false;
   }

}
int main()
{
    stu s1(20,30);
    stu s2(20,20);
    if(s1>s2)
        cout << "s1>s2" << endl;
    else if(s1<s2)
        cout << "s1<s2" << endl;
    else if(s1==s2)
        cout << "s1==s2" << endl;
    else if(s1>=s2)
        cout << "s1>=s2" << endl;
    else if(s1<=s2)
        cout << "s1<=s2" << endl;
    else
        cout << "s1!=s2" << endl;

    return 0;
}

相关推荐
努力学算法的蒟蒻7 小时前
day114(3.16)——leetcode面试经典150
算法·leetcode·职场和发展
重庆兔巴哥7 小时前
如何在Dev-C++中使用MinGW-w64编译器?
linux·开发语言·c++
ysa0510307 小时前
贪心【逆向dp】
数据结构·c++·笔记·算法
夜月yeyue7 小时前
Linux 邻接(Neighbor)子系统架构与 NUD 状态机
linux·运维·服务器·嵌入式硬件·算法·系统架构
魔道不误砍柴功7 小时前
Java Function 高级使用技巧:从工程实战中来
java·开发语言·python
三佛科技-187366133977 小时前
LP3783A芯茂微5V2.1A低功耗原边反馈充电器芯片替代PL3378/C
c语言·开发语言
不知名。。。。。。。。7 小时前
仿muduo库实现高并发服务器----EventLoop与线程整合起来
java·开发语言·jvm
编程大师哥7 小时前
JAVA 集合框架进阶
java·开发语言
叶子野格7 小时前
《C语言学习:Visual Studio使用》2
c++·学习·visual studio
春日见7 小时前
车载系统中的CPU与内存监管
java·开发语言·驱动开发·docker·计算机外设