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;
}

相关推荐
阿昭L2 小时前
MFC仿真
c++·mfc
阿幸软件杂货间3 小时前
Office转PDF转换器v1.0.py
开发语言·pdf·c#
扯淡的闲人3 小时前
多语言编码Agent解决方案(5)-IntelliJ插件实现
开发语言·python
丑小鸭是白天鹅3 小时前
Kotlin协程详细笔记之切线程和挂起函数
开发语言·笔记·kotlin
ChillJavaGuy3 小时前
常见限流算法详解与对比
java·算法·限流算法
sali-tec3 小时前
C# 基于halcon的视觉工作流-章34-环状测量
开发语言·图像处理·算法·计算机视觉·c#
java搬砖工-苤-初心不变3 小时前
基于 lua_shared_dict 的本地内存限流实现
开发语言·junit·lua
charlie1145141913 小时前
Kotlin 的 apply / with / run 详解
开发语言·kotlin·程序设计·面对对象
Tiger_shl3 小时前
【层面一】C#语言基础和核心语法-02(反射/委托/事件)
开发语言·c#
GW_Cheng4 小时前
分享一个vue2的tinymce配置
开发语言·javascript·ecmascript