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

相关推荐
↣life♚1 分钟前
从SAM看交互式分割与可提示分割的区别与联系:Interactive Segmentation & Promptable Segmentation
人工智能·深度学习·算法·sam·分割·交互式分割
zqh176736464697 分钟前
2025年阿里云ACP人工智能高级工程师认证模拟试题(附答案解析)
人工智能·算法·阿里云·人工智能工程师·阿里云acp·阿里云认证·acp人工智能
于壮士hoho23 分钟前
Python | Dashboard制作
开发语言·python
2301_8035545224 分钟前
c++和c的不同
java·c语言·c++
Darkwanderor26 分钟前
c++STL-通用(反向)迭代器适配器
c++
fie888926 分钟前
用模型预测控制算法实现对电机位置控制仿真
算法
Kent_J_Truman30 分钟前
【交互 / 差分约束】
算法
ghie909036 分钟前
x-IMU matlab zupt惯性室内定位算法
人工智能·算法·matlab
Magnum Lehar44 分钟前
3d游戏引擎的Utilities模块实现
c++·算法·游戏引擎
Asus.Blogs1 小时前
为什么go语言中返回的指针类型,不需要用*取值(解引用),就可以直接赋值呢?
开发语言·后端·golang