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

相关推荐
老华带你飞11 分钟前
英语学习|基于Java英语学习系统(源码+数据库+文档)
java·开发语言·数据库·vue.js·spring boot·后端·学习
qq_4798754312 分钟前
C++ 模板元编程
java·开发语言·c++
codingPower13 分钟前
Java EasyExcel创建复杂表格的完整指南:WriteTable
java·开发语言
爱装代码的小瓶子15 分钟前
【cpp知识铺子】map与set的底层AVL树
开发语言·数据结构·c++·b树·算法·链表
IT·小灰灰16 分钟前
腾讯HY2.0 Think推理模型深度解析:技术突破、应用场景与实践指南
开发语言·人工智能·python·深度学习·神经网络·算法·数据分析
源代码•宸18 分钟前
100 Go Mistakes(#4 过度使用getter和setter、#5 接口污染)
开发语言·经验分享·后端·golang
修炼地26 分钟前
代码随想录算法训练营第二十八天 | 动态规划理论基础、509. 斐波那契数、70. 爬楼梯、746. 使用最小花费爬楼梯
c++·算法·动态规划
某空m29 分钟前
【Android】浅析DataBinding
android·开发语言
小南家的青蛙32 分钟前
LeetCode第773题 - 滑动谜题
算法·leetcode·职场和发展
Felven42 分钟前
C. Isamatdin and His Magic Wand!
c语言·数据结构·算法