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

相关推荐
张np4 分钟前
java基础-Deque 接口
java·开发语言
wbs_scy7 分钟前
C++:二叉搜索树(BST)完全指南(从概念原理、核心操作到底层实现)
数据结构·算法
柒壹漆7 分钟前
用Python制作一个USB Hid设备数据收发测试工具
开发语言·git·python
东华万里8 分钟前
Release 版本禁用 assert:NDEBUG 的底层逻辑与效率优化
java·jvm·算法
有点。8 分钟前
C++ ⼀级 2023 年09 ⽉
c++
LXS_3579 分钟前
Day 16 C++提高之模板
开发语言·c++·笔记·学习方法
盒马盒马11 分钟前
Rust:内部可变性 & 型变
开发语言·后端·rust
沐知全栈开发12 分钟前
XSLT `<value-of>` 元素详解
开发语言
wyw000013 分钟前
鸿蒙开发-如何将C++侧接收的PixelMap转换成cv::mat格式
c++·华为·harmonyos
liulilittle15 分钟前
C++ CRTP 替代虚函数
数据结构·c++·算法