C++QT-day4

news2024/7/6 18:27:01
#include <iostream> //运算符重载
using namespace std;

class Person
{
//    //全局函数实现+运算符重载
//    friend const Person operator+(const Person &L,const Person &R);
//    //全局函数实现-运算符重载
//    friend const Person operator-(const Person &L,const Person &R);
//    //全局函数实现*运算符重载
//    friend const Person operator*(const Person &L,const Person &R);
//    //全局函数实现/运算符重载
//    friend const Person operator/(const Person &L,const Person &R);
//    //全局函数实现%运算符重载
//    friend const Person operator%(const Person &L,const Person &R);

//    //全局函数实现>运算符重载
//    friend bool operator>(const Person &L, const Person &R);
//    //全局函数实现>=运算符重载
//    friend bool operator>=(const Person &L, const Person &R);
//    //全局函数实现<运算符重载
//    friend bool operator<(const Person &L, const Person &R);
//    //全局函数实现<=运算符重载
//    friend bool operator<=(const Person &L, const Person &R);
//    //全局函数实现==运算符重载
//    friend bool operator==(const Person &L, const Person &R);
//    //全局函数实现!=运算符重载
//    friend bool operator!=(const Person &L, const Person &R);


//    //全局函数实现=运算符重载
//    friend Person &operator=(Person &L, const Person &R);
//    //全局函数实现+=运算符重载
//    friend Person &operator+=(Person &L, const Person &R);
//    //全局函数实现-=运算符重载
//    friend Person &operator-=(Person &L, const Person &R);
//    //全局函数实现*=运算符重载
//    friend Person &operator*=(Person &L, const Person &R);
//    //全局函数实现/=运算符重载
//    friend Person &operator/=(Person &L, const Person &R);
//    //全局函数实现%=运算符重载
//    friend Person &operator%=(Person &L, const Person &R);

private:
    int a;
    int b;
public:
    Person(){}
    Person(int a, int b):a(a),b(b){}
/****************成员函数实现+运算符重载*******************/
    const Person operator+(const Person &p)const
    {
        Person temp;
        temp.a = a + p.a;
        temp.b = b + p.b;
        return  temp;
    }
    /****************成员函数实现-运算符重载*******************/
    const Person operator-(const Person &p)const
    {
        Person temp;
        temp.a = a - p.a;
        temp.b = b - p.b;
        return  temp;
    }
    /****************成员函数实现*运算符重载*******************/
    const Person operator*(const Person &p)const
    {
        Person temp;
        temp.a = a * p.a;
        temp.b = b * p.b;
        return  temp;
    }
    /****************成员函数实现/运算符重载*******************/
    const Person operator/(const Person &p)const
    {
        Person temp;
        if(0==p.a && 0==p.b)
            cout << "除数为0" << endl;
        else if(0==p.a && 0!=p.b)
        {
            cout << "a除数为0" << "  b = " << b << endl;
            temp.a = a / p.b;
        }
        else if(0!=p.a && 0==p.b)
        {
            temp.b = b / p.a;
            cout << "a = " << a << " b除数为0" << endl;
        }
        else
        {
            temp.b = b / p.a;
            temp.a = a / p.b;
            cout << "a = " << a << "  b = " << b << endl;
        }
        return  temp;
    }
    /****************成员函数实现%运算符重载*******************/
    const Person operator%(const Person &p)const
    {
        Person temp;
        temp.a = a % p.a;
        temp.b = b % p.b;
        return  temp;
    }
/***************成员函数实现>关系运算符重载***************/
    bool operator>(const Person &R)const
    {
        if(a>R.a && b>R.b)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    /***************成员函数实现>=关系运算符重载***************/
    bool operator>=(const Person &R)const
    {
        if(a>=R.a && b>=R.b)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    /***************成员函数实现<关系运算符重载***************/
    bool operator<(const Person &R)const
    {
        if(a<R.a && b<R.b)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    /***************成员函数实现<=关系运算符重载***************/
    bool operator<=(const Person &R)const
    {
        if(a<=R.a && b<=R.b)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    /***************成员函数实现==关系运算符重载***************/
    bool operator==(const Person &R)const
    {
        if(a==R.a && b==R.b)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    /***************成员函数实现!=关系运算符重载***************/
    bool operator!=(const Person &R)const
    {
        if(a!=R.a && b!=R.b)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
/***************成员函数实现=赋值运算符重载***************/
    Person &operator=(const Person &R)
    {
        a = R.a;
        b = R.b;
        return *this;
    }
    /***************成员函数实现+=赋值运算符重载***************/
    Person &operator+=(const Person &R)
    {
        a += R.a;
        b += R.b;
        return *this;
    }
    /***************成员函数实现-=赋值运算符重载***************/
    Person &operator-=(const Person &R)
    {
        a -= R.a;
        b -= R.b;
        return *this;
    }
    /***************成员函数实现*=赋值运算符重载***************/
    Person &operator*=(const Person &R)
    {
        a *= R.a;
        b *= R.b;
        return *this;
    }
    /***************成员函数实现/=赋值运算符重载***************/
    Person &operator/=(const Person &R)
    {
        if(0==R.a && 0==R.b)
            cout << "除数为0" << endl;
        else if(0==R.a && 0!=R.b)
        {
            cout << "a除数为0" << "  b = " << b << endl;
            a = a / R.b;
        }
        else if(0!=R.a && 0==R.b)
        {
            b = b / R.a;
            cout << "a = " << a << " b除数为0" << endl;
        }
        else if(0!=R.a && 0!=R.b)
        {
            a = a / R.a;
            b = b / R.b;
            cout << "a = " << a << "  b = " << b << endl;
        }
        return *this;
    }
    /***************成员函数实现%=赋值运算符重载***************/
    Person &operator%=(const Person &R)
    {
        a %= R.a;
        b %= R.b;
        return *this;
    }

    void show()
    {
        cout << "a = " << a << "  b = " << b << endl;
    }

};

///****************全局函数实现+运算符重载*******************/
//const Person operator+(const Person &L,const Person &R)
//{
//    Person temp;
//    temp.a = L.a + R.a;
//    temp.b = L.b + R.b;
//    return  temp;
//}
///****************全局函数实现-运算符重载*******************/
//const Person operator-(const Person &L,const Person &R)
//{
//    Person temp;
//    temp.a = L.a - R.a;
//    temp.b = L.b - R.b;
//    return  temp;
//}
///****************全局函数实现*运算符重载*******************/
//const Person operator*(const Person &L,const Person &R)
//{
//    Person temp;
//    temp.a = L.a * R.a;
//    temp.b = L.b * R.b;
//    return  temp;
//}
///****************全局函数实现/运算符重载*******************/
//const Person operator/(const Person &L,const Person &R)
//{
//    Person temp;
//    if(0==R.a && 0==R.b)
//        cout << "除数为0" << endl;
//    else if(0==R.a && 0!=R.b)
//    {
//        cout << "a除数为0" << "  b = " << L.b << endl;
//        temp.a = L.a / R.b;
//    }
//    else if(0!=R.a && 0==R.b)
//    {
//        temp.b = L.b / R.a;
//        cout << "a = " << L.a << " b除数为0" << endl;
//    }
//    else if(0!=R.a && 0!=R.b)
//    {
//        temp.b = L.b / R.a;
//        temp.a = L.a / R.b;
//        cout << "a = " << L.a << "  b = " << L.b << endl;
//    }
//    return  temp;
//}
///****************全局函数实现%运算符重载*******************/
//const Person operator%(const Person &L,const Person &R)
//{
//    Person temp;
//    temp.a = L.a % R.a;
//    temp.b = L.b % R.b;
//    return  temp;
//}

///***************全局函数实现>关系运算符重载***************/
//bool operator>(const Person &L, const Person &R)
//{
//    if(L.a>R.a && L.b>R.b)
//    {
//        return true;
//    }
//    else
//    {
//        return false;
//    }
//}
///***************全局函数实现>=关系运算符重载***************/
//bool operator>=(const Person &L, const Person &R)
//{
//    if(L.a>=R.a && L.b>=R.b)
//    {
//        return true;
//    }
//    else
//    {
//        return false;
//    }
//}
///***************全局函数实现<关系运算符重载***************/
//bool operator<(const Person &L, const Person &R)
//{
//    if(L.a<R.a && L.b<R.b)
//    {
//        return true;
//    }
//    else
//    {
//        return false;
//    }
//}
///***************全局函数实现>关系运算符重载***************/
//bool operator<=(const Person &L, const Person &R)
//{
//    if(L.a<=R.a && L.b<=R.b)
//    {
//        return true;
//    }
//    else
//    {
//        return false;
//    }
//}
///***************全局函数实现==关系运算符重载***************/
//bool operator==(const Person &L, const Person &R)
//{
//    if(L.a==R.a && L.b==R.b)
//    {
//        return true;
//    }
//    else
//    {
//        return false;
//    }
//}
///***************全局函数实现!=关系运算符重载***************/
//bool operator!=(const Person &L, const Person &R)
//{
//    if(L.a!=R.a && L.b!=R.b)
//    {
//        return true;
//    }
//    else
//    {
//        return false;
//    }
//}

///***************全局函数实现=赋值运算符重载***************/
//Person &operator=(Person &L, const Person &R)
//{
//    L.a = R.a;
//    L.b = R.b;
//    return L;
//}
///***************全局函数实现+=赋值运算符重载***************/
//Person &operator+=(Person &L, const Person &R)
//{
//    L.a += R.a;
//    L.b += R.b;
//    return L;
//}
///***************全局函数实现-=赋值运算符重载***************/
//Person &operator-=(Person &L, const Person &R)
//{
//    L.a -= R.a;
//    L.b -= R.b;
//    return L;
//}
///***************全局函数实现*=赋值运算符重载***************/
//Person &operator*=(Person &L, const Person &R)
//{
//    L.a *= R.a;
//    L.b *= R.b;
//    return L;
//}
///***************全局函数实现/=赋值运算符重载***************/
//Person &operator/=(Person &L, const Person &R)
//{
//    if(0==R.a && 0==R.b)
//        cout << "除数为0" << endl;
//    else if(0==R.a && 0!=R.b)
//    {
//        cout << "a除数为0" << "  b = " << L.b << endl;
//        L.b /= R.b;
//    }
//    else if(0!=R.a && 0==R.b)
//    {
//        L.a /= R.a;
//        cout << "a = " << L.a << " b除数为0" << endl;
//    }
//    else if(0!=R.a && 0!=R.b)
//    {
//        L.a /= R.a;
//        L.b /= R.b;
//        cout << "a = " << L.a << "  b = " << L.b << endl;
//    }
//    return L;
//}
///***************全局函数实现%=赋值运算符重载***************/
//Person &operator%=(Person &L, const Person &R)
//{
//    L.a %= R.a;
//    L.b %= R.b;
//    return L;
//}

/****************************************************/
int main()
{
    Person p1(3,4);
    Person p2(1,2);
    Person p3 = p1 + p2; p3.show();
    Person p4 = p1 - p2; p4.show();
    Person p5 = p1 * p2; p5.show();
    Person p6 = p1 / p2;
    Person p7 = p1 % p2; p7.show();

    if(p3>p2)
        cout << "p3>p2" << endl;
    else if(p3>=p2)
        cout << "p3>=p2" << endl;
    else if(p3<p2)
        cout << "p3<p2" << endl;
    else if(p3<=p2)
        cout << "p3<=p2" << endl;
    else if(p3==p2)
        cout << "p3==p2" << endl;
    else if(p3!=p2)
        cout << "p3!=p2" << endl;

    p3+=p2;p3.show();
    p3-=p2;p3.show();
    p3*=p2;p3.show();
    p3/=p2;
    p3%=p2;p3.show();

    return 0;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1077573.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

【LeetCode】——链式二叉树经典OJ题详解

主页点击直达&#xff1a;个人主页 我的小仓库&#xff1a;代码仓库 C语言偷着笑&#xff1a;C语言专栏 数据结构挨打小记&#xff1a;初阶数据结构专栏 Linux被操作记&#xff1a;Linux专栏 LeetCode刷题掉发记&#xff1a;LeetCode刷题 算法头疼记&#xff1a;算法专栏…

PTE考试预览

目录 pte评分机制是对比 不同水平的人之后跟你匹配 前两次 两个听力题没有写 咯噔一下 无源65分 Headset Check 调试耳机听筒 Microphone zhigeiCheck 调试麦克风 麦克风测试话语 老师说她放在鼻梁骨那里声音很好 自我介绍 读完之后 &#xff0c;立马点next&#xf…

PostgreSQL安装错误:Problem running post-install step

问题描述 安装包&#xff1a;pgpostgresql-14.9-1-windows-x64 postgresql-16.0-1-windows-x64 采取措施 一、 首先安装的是16版本的程序&#xff0c;报错后卸载尝试安装14版本软件&#xff0c;依旧报错。 二、 网上搜索&#xff0c;发现该博客&#xff1a; PostgreSQL安…

基于YOLOv8模型的足球目标检测系统(PyTorch+Pyside6+YOLOv8模型)

摘要&#xff1a;基于YOLOv8模型的足球目标检测系统可用于日常生活中检测与定位足球目标&#xff0c;利用深度学习算法可实现图片、视频、摄像头等方式的目标检测&#xff0c;另外本系统还支持图片、视频等格式的结果可视化与结果导出。本系统采用YOLOv8目标检测算法训练数据集…

.NET与C#未来大流行是必然趋势

python性能差、应用范围有限&#xff0c;虽然目前很流行 C门槛高 C语言只适合底层 java大势已去 c#似乎是综合实力最强的语言&#xff0c;而且开发调试都很简单&#xff0c;适合很多行业&#xff0c;开发成本低。

Web服务器概述及http协议

Web Server&#xff08;网页服务器&#xff09; 一个 Web Server 就是一个服务器软件&#xff08;程序&#xff09;&#xff0c;或者是运行这个服务器软件的硬件&#xff08;计算机&#xff09;。其主要功能是通过 HTTP 协议与客户端&#xff08;通常是浏览器&#xff08;Brow…

远程发送剪切板,屏幕截图

使用场景 线上答题时一般有录屏&#xff0c;局域网内可发送内容到电脑上剪切板&#xff0c;电脑上直接CtrlV双机位看不到屏幕是可以远程截屏 下载 访问地址下载程序&#xff1a;https://gitee.com/guzhengchang/shared-clipboard/releases/tag/v0.2 双击前面下载的程序&am…

牛津大学海外学习:14天的知识与文化之旅

牛津——一个充满学术氛围与古老传统的城市&#xff0c;对于我这次14天的海外学习经验来说&#xff0c;这里每一个角落都隐藏着知识和历史的故事。作为中国的一名学生&#xff0c;能够在这里学习、生活&#xff0c;真是一次难得的机会。 我报名的是《人工智能》课程&#xff0…

嵌入式Linux裸机开发(五)中断管理

系列文章目录 文章目录 系列文章目录前言STM32 中断系统IMX6U中断控制8个中断GIC中断控制器GIC介绍中断IDGIC逻辑分块GIC协处理器 中断使能中断优先级 重点代码分析官方SDK函数start.S文件自行编写中断驱动文件 前言 最近在学习中发现&#xff0c;学Linux嵌入式不仅是对Linux的…

大型组网如何使用BFD提高可靠性

1.当前现网业务存在哪些问题&#xff1f; 2.BFD的工作机制讲解 3.BFD联动静态路由以及OSPF实战 --- BFD - 双向转发检测 - 通用标准 SLA --- 介质无关 协议无关 --- BFD基本原理 --- BFD应用场景配置方式 --- BFD结合OSPF --- BFD结合静态路由 跨网段…

MySQL 面试知识脑图 初高级知识点

脑图下载地址&#xff1a;https://mm.edrawsoft.cn/mobile-share/index.html?uuid18b10870122586-src&share_type1 sql_mode 基本语法及校验规则 ONLY_FULL_GROUP_BY 对于GROUP BY聚合操作&#xff0c;如果在SELECT中的列&#xff0c;没有在GROUP BY中出现&#xff…

【计算机网络】第三章课后习题答案

习题目录&#xff1a; 【3-01】数据链路&#xff08;即逻辑链路&#xff09;与链路&#xff08;即物理链路&#xff09;有何区别&#xff1f;"链路接通了"与"数据链路接通了"的区别何在&#xff1f; 【3-02】数据链路层中的链路控制包括哪些功能&#xf…

Arduino驱动LSM303DLH电子罗盘(惯性测量传感器篇)

目录 1、传感器特性 2、控制器和传感器连线图 3、驱动程序 LSM303DLH电子罗盘传感器集成了3轴磁场,3轴加速度传感器,可以提供倾斜补偿后的输出。LSM303DLH芯片的加速计、磁力计、A/D转化器及信号条理电路集成在一起,通过I2C总线和处理器通信。这样只用一颗芯片就实现了6轴…

Java架构师高并发架构设计

目录 1 导学2 什么是高并发问题3 高并发处理之道4 akf扩展立方体5 细化理念应对高并发5 总结1 导学 本章的主要内容是大型系统架构设计的难点之一,高并发架构设计相关的知识落到实际项目上,就是订单系统的高并发架构设计。我们首先会去学习到底何为高并发问题,先把问题搞清楚…

Typescript 综合笔记:解读一个github中的React 网页

1 repository来源和效果 zhitern/ntu-scse22-0163-web (github.com) 2 核心代码异同&#xff08;相比于初始创建的代码&#xff09; 2.1 index.html 和初始创建的是一样的 2.2 App.css 和初始创建的是一样的 2.3 index.tsx 唯一”不一样“的是紫色部分,tsx文件中多了一个…

【力扣2011】执行操作后的变量值

&#x1f451;专栏内容&#xff1a;力扣刷题⛪个人主页&#xff1a;子夜的星的主页&#x1f495;座右铭&#xff1a;前路未远&#xff0c;步履不停 目录 一、题目描述二、题目分析 一、题目描述 题目链接&#xff1a;执行操作后的变量值 存在一种仅支持 4 种操作和 1 个变量 …

【计算机网络】UDP协议编写群聊天室----附代码

UDP构建服务器 x 预备知识 认识UDP协议 此处我们也是对UDP(User Datagram Protocol 用户数据报协议)有一个直观的认识; 后面再详细讨论. 传输层协议无连接不可靠传输面向数据报 网络字节序 我们已经知道,内存中的多字节数据相对于内存地址有大端和小端之分, 磁盘文件中的…

YOLOv7损失函数改进:SlideLoss,解决简单样本和困难样本之间的不平衡问题

💡💡💡本文改进:SlideLoss,解决简单样本和困难样本之间的不平衡问题,并使用有效感受野的信息来设计Anchor。 SlideLoss| 亲测在多个数据集能够实现涨点,对小目标、遮挡物性能提升也能够助力涨点。 收录: YOLOv7高阶自研专栏介绍: http://t.csdnimg.cn/tYI0c …

JVM面试题:(二)内存结构和内存溢出、方法区的两种实现

内存结构&#xff1a; 方法区和对是所有线程共享的内存区域&#xff1b;而java栈、本地方法栈和程序员计数器是运行是线程私有 的内存区域。 Java堆&#xff08;Heap&#xff09;,是Java虚拟机所管理的内存中最大的一块。Java堆是被所有线程共享的一块内 存区域&#xff0c;在…