10.10 作业

news2024/10/6 4:10:05

全局函数实现运算符重载 

#include "05_fun.h"

// 全局函数实现运算符重载

// 算数运算符重载 + - * / %
const Per operator+(const Per &L, const Per &R)
{
    Per temp;
    temp.a = L.a + R.a;
    temp.b = L.b + R.b;
    return temp;
}
const Per operator-(const Per &L, const Per &R)
{
    Per temp;
    temp.a = L.a - R.a;
    temp.b = L.b - R.b;
    return temp;
}
const Per operator*(const Per &L, const Per &R)
{
    Per temp;
    temp.a = L.a * R.a;
    temp.b = L.b * R.b;
    return temp;
}
const Per operator/(const Per &L, const Per &R)
{
    Per temp;
    temp.a = L.a / R.a;
    temp.b = L.b / R.b;
    return temp;
}
const Per operator%(const Per &L, const Per &R)
{
    Per temp;
    temp.a = L.a % R.a;
    temp.b = L.b % R.b;
    return temp;
}

// 关系运算符重载 > >= < <= == !=
bool operator>(const Per &L, const Per &R)
{
    if (L.a > R.a && L.b > R.b)
        return true;
    else
        return false;
}
bool operator>=(const Per &L, const Per &R)
{
    if (L.a >= R.a && R.a >= R.b)
        return true;
    else
        return false;
}
bool operator<(const Per &L, const Per &R)
{
    if (L.a < R.a && L.b < R.b)
        return true;
    else
        return false;
}
bool operator<=(const Per &L, const Per &R)
{
    if (L.a <= R.a && L.b <= R.b)
        return true;
    else
        return false;
}
bool operator==(const Per &L, const Per &R)
{
    if (L.a == R.a && L.b == R.b)
        return true;
    else
        return false;
}
bool operator!=(const Per &L, const Per &R)
{
    if (L.a != R.a && L.b != R.b)
        return true;
    else
        return false;
}

// 赋值运算符重载 += -= *= /= %=
Per &operator+=(Per &L, const Per &R)
{
    L.a += R.a;
    L.b += R.b;
    return L;
}
Per &operator-=(Per &L, const Per &R)
{
    L.a -= R.a;
    L.b -= R.b;
    return L;
}
Per &operator*=(Per &L, const Per &R)
{
    L.a *= R.a;
    L.b *= R.b;
    return L;
}
Per &operator/=(Per &L, const Per &R)
{
    L.a /= R.a;
    L.b /= R.b;
    return L;
}
Per &operator%=(Per &L, const Per &R)
{
    L.a %= R.a;
    L.b %= R.b;
    return L;
}

对应头文件

#ifndef __05FUN_H__
#define __05FUN_H__
#include <iostream>

using namespace std;

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

    // 关系运算符重载 > >= < <= == !=
    friend bool operator>(const Per &L, const Per &R);  // >
    friend bool operator>=(const Per &L, const Per &R); // >=
    friend bool operator<(const Per &L, const Per &R);  // <
    friend bool operator<=(const Per &L, const Per &R); // <=
    friend bool operator==(const Per &L, const Per &R); // ==
    friend bool operator!=(const Per &L, const Per &R); // !=

    // 赋值运算符重载 += -= *= /= %=
    // operator=只能是成员函数
    friend Per &operator+=(Per &L, const Per &R); // +=
    friend Per &operator-=(Per &L, const Per &R); // -=
    friend Per &operator*=(Per &L, const Per &R); // *=
    friend Per &operator/=(Per &L, const Per &R); // /=
    friend Per &operator%=(Per &L, const Per &R); // %=

private:
    int a;
    int b;

public:
    Per() {}
    Per(int a, int b) : a(a), b(b) {}
    void show() { cout << a << ' ' << b << endl; }
};
#endif

成员函数实现运算符重载

#include "04_fun.h"

// 成员函数实现运算符重载

// 算数运算符重载 + - * / %
const Per Per::operator+(const Per &p) const
{
    Per temp;
    temp.a = a + p.a;
    temp.b = b + p.b;
    return temp;
}
const Per Per::operator-(const Per &p) const
{
    Per temp;
    temp.a = a - p.a;
    temp.b = b - p.b;
    return temp;
}
const Per Per::operator*(const Per &p) const
{
    Per temp;
    temp.a = a * p.a;
    temp.b = b * p.b;
    return temp;
}
const Per Per::operator/(const Per &p) const
{
    Per temp;
    temp.a = a / p.a;
    temp.b = b / p.b;
    return temp;
}
const Per Per::operator%(const Per &p) const
{
    Per temp;
    temp.a = a % p.a;
    temp.b = b % p.b;
    return temp;
}

// 关系运算符重载 > >= < <= == !=
bool Per::operator>(const Per &p)
{
    if (a > p.a && b > p.b)
        return true;
    else
        return false;
}
bool Per::operator>=(const Per &p)
{
    if (a >= p.a && b >= p.b)
        return true;
    else
        return false;
}
bool Per::operator<(const Per &p)
{
    if (a < p.a && b < p.b)
        return true;
    else
        return false;
}
bool Per::operator<=(const Per &p)
{
    if (a <= p.a && b <= p.b)
        return true;
    else
        return false;
}
bool Per::operator==(const Per &p)
{
    if (a == p.a && b == p.b)
        return true;
    else
        return false;
}
bool Per::operator!=(const Per &p)
{
    if (a != p.a && b != p.b)
        return true;
    else
        return false;
}

// 赋值运算符重载 += -= *= /= %=
Per &Per::operator=(const Per &p)
{
    if (this != &p)
    {
        a = p.a;
        b = p.b;
    }

    return *this;
}
Per &Per::operator+=(const Per &p)
{
    a += p.a;
    b += p.b;
    return *this;
}
Per &Per::operator-=(const Per &p)
{
    a -= p.a;
    b -= p.b;
    return *this;
}
Per &Per::operator*=(const Per &p)
{
    a *= p.a;
    b *= p.b;
    return *this;
}
Per &Per::operator/=(const Per &p)
{
    a /= p.a;
    b /= p.b;
    return *this;
}
Per &Per::operator%=(const Per &p)
{
    a %= p.a;
    b %= p.b;
    return *this;
}

对应头文件

#ifndef __04FUN_H__
#define __04FUN_H__
#include <iostream>

using namespace std;

class Per // 成员
{
private:
    int a;
    int b;

public:
    Per() {}
    Per(int a, int b) : a(a), b(b) {}
    void show() { cout << a << ' ' << b << endl; }

    // 算数运算符重载 + - * / %
    const Per operator+(const Per &p) const;
    const Per operator-(const Per &p) const;
    const Per operator*(const Per &p) const;
    const Per operator/(const Per &p) const;
    const Per operator%(const Per &p) const;

    // 关系运算符重载 > >= < <= == !=
    bool operator>(const Per &p);
    bool operator>=(const Per &p);
    bool operator<(const Per &p);
    bool operator<=(const Per &p);
    bool operator==(const Per &p);
    bool operator!=(const Per &p);

    // 赋值运算符重载 = += -= *= /= %=
    Per &operator=(const Per &p);
    Per &operator+=(const Per &p);
    Per &operator-=(const Per &p);
    Per &operator*=(const Per &p);
    Per &operator/=(const Per &p);
    Per &operator%=(const Per &p);
};

#endif

主函数

// #include "04_fun.h"
#include "05_fun.h"

int main()
{
    Per p1(1, 1);
    Per p2(2, 2);

    // 算数运算符重载 + - * / %
    Per p3 = p1 + p2;
    p3.show();
    Per p4 = p1 - p2;
    p4.show();
    Per p5 = p1 * p2;
    p5.show();
    Per p6 = p1 / p2;
    p6.show();
    Per p7 = p1 % p2;
    p7.show();

    // 关系运算符重载 > >= < <= == !=
    if (p1 > p2)
    {
        cout << "p1>p2" << endl;
    }
    else if (p1 >= p2)
    {
        cout << "p1>=p2" << endl;
    }
    else if (p1 < p2)
    {
        cout << "p1<p2" << endl;
    }
    else if (p1 <= p2)
    {
        cout << "p1<=p2" << endl;
    }
    else if (p1 == p2)
    {
        cout << "p1==p2" << endl;
    }
    else
    {
        cout << "p1!=p2" << endl;
    }

    // 赋值运算符重载 = += -= *= /= %=
    p1 = p3;
    p1.show();
    p1 += p2;
    p1.show();
    p1 -= p2;
    p1.show();
    p1 *= p2;
    p1.show();
    p1 /= p2;
    p1.show();
    p1 %= p2;
    p1.show();
    return 0;
}

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

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

相关文章

【python海洋专题十六】对大陆周边的数据进行临近插值

前几期内容 画温度、盐度的年平均和季节平均的平面分布图, 对于IAP粗分辨率数据进行处理得到的图像,大陆周围都没有数值。 没能呈现较为漂亮的图像。 甚至是老师或者编辑要求大陆周围不能有空白, 又不想对数据进行全部的分辨率更细的插值。 此时,使用周围临近的数据点代…

GitHub【入门】从入门到会用(千字总结●超详细)

我的个人主页&#xff1a;☆光之梦☆_C语言基础语法&#xff08;超详细&#xff09;,【java入门】语法总结-CSDN博客 创作不易&#xff0c;如果能帮到你就好 特别标注&#xff1a;我的C语言专栏写的超详细&#xff0c;强烈推荐你去看看哦 注&#xff1a;你的 &#x1f44d;点赞…

epoll 定时器

参考&#xff1a; Linux下使用epoll监听定时器-CSDN博客 但是这个用的是gettimeofday。 本人使用的是 #include <stdlib.h> #include<stdio.h> #include <sys/timerfd.h> #include <sys/epoll.h> #include <unistd.h> #include <sys/time.…

实施运维02

一.网线制作 1.所需材料 网线&#xff0c;水晶头&#xff0c;网线钳&#xff0c;水晶头, 路由器或者网络测速仪 网线钳 网线制作标准 T568A标准&#xff08;交叉线&#xff09;&#xff1a;适用链接场合&#xff1a;电脑-电脑、交换机-交换机、集线器-集线器 接线顺序&…

ESXI使用esxtop命令监控存储的性能

1、监控每个hba卡的io性能 a. ssh登录esxi后台&#xff0c;执行esxtop&#xff0c;再按d切换到磁盘视图&#xff08;hba模式&#xff09; b. 要显示完整的设备名称&#xff1a;SHIFTL&#xff0c;输入36 c. 显示其他的性能指标字段&#xff1a;按f&#xff0c;按a-j选择需要的字…

黑马点评-05缓存穿透问题及其解决方案,缓存空字符串或使用布隆过滤器

缓存穿透问题(缓存空) 缓存穿透的解决方案 缓存穿透(数据穿透缓存直击数据库): 缓存穿透是指客户端请求访问缓存中和数据库中都不存在的数据,此时缓存永远不会生效并且用户的请求都会打到数据库 数据库能够承载的并发不如Redis这么高&#xff0c;如果大量的请求同时访问这种…

准备熬夜加班?curllibcurl 高危漏洞明日公布

近日&#xff0c;curl项目的作者bagder(Daniel Stenberg)在GitHub中发布消息称&#xff0c;将在2023年10月11日发布curl的8.4.0版本。同时&#xff0c;他们还将公开两个漏洞&#xff1a;CVE-2023-38545和CVE-2023-38546。如下图所示&#xff1a; 图片来源于互联网 其中CVE-202…

【算法|双指针系列No.4】leetcode11. 盛最多水的容器

个人主页&#xff1a;兜里有颗棉花糖 欢迎 点赞&#x1f44d; 收藏✨ 留言✉ 加关注&#x1f493;本文由 兜里有颗棉花糖 原创 收录于专栏【手撕算法系列专栏】【LeetCode】 &#x1f354;本专栏旨在提高自己算法能力的同时&#xff0c;记录一下自己的学习过程&#xff0c;希望…

十大排序算法Java实现及时间复杂度

文章目录 十大排序算法选择排序冒泡排序插入排序希尔排序快速排序归并排序堆排序计数排序基数排序桶排序时间复杂度 参考资料 十大排序算法 选择排序 原理 从待排序的数据元素中找出最小或最大的一个元素&#xff0c;存放在序列的起始位置&#xff0c; 然后再从剩余的未排序元…

C# Windows 窗体控件中的边距和填充

可以将 Margin 属性、Left、Top、Right、Bottom 的每个方面设置为不同的值&#xff0c;也可以使用 All 属性将它们全部设置为相同的值。 在代码中设置Margin&#xff0c;元素的左边设置为5个单位、上边设置为10个单位、右边设置为15个单位和下边设置为20个单位。 TextBox myT…

10_10C++

X-mid #include <iostream> using namespace std; class Kun {//算术运算符friend const Kun operator(const Kun &k1,const Kun &k2);friend const Kun operator-(const Kun &k1,const Kun &k2);friend const Kun operator*(const Kun &k1,const Ku…

AI能否取代程序员:探讨人工智能在编程领域的角色

引言&#xff1a; 随着人工智能&#xff08;AI&#xff09;技术的快速发展&#xff0c;人们开始思考&#xff1a;AI是否能够取代程序员&#xff1f;这个问题引发了广泛的讨论和辩论。一些人认为&#xff0c;AI的出现将彻底改变编程的面貌&#xff0c;而另一些人则坚信&#xf…

线性代数 --- QR分解,A=QR

矩阵的QR分解&#xff0c;格拉姆施密特过程的矩阵表示 首先先简单的回顾一下Gram-Schmidt正交化过程的核心思想&#xff0c;如何把一组线性无关的向量构造成一组标准正交向量&#xff0c;即&#xff0c;如何把矩阵A变成矩阵Q的过程。 给定一组线性无关的向量a,b,c&#xff0c;我…

【深度学习】Chinese-CLIP 使用教程,图文检索,跨模态检索,零样本图片分类

代码&#xff1a;https://github.com/OFA-Sys/Chinese-CLIP/blob/master/deployment.md 文章目录 安装环境和onnx推理转换所有模型为onnx测试所有onnx模型的脚本onnx cpu方式执行docker镜像 安装环境和onnx推理 安装环境&#xff0c;下载权重放置到指定目录&#xff0c;进行on…

基于Java+SpringBoot+Vue线上医院挂号系统的设计与实现 前后端分离【Java毕业设计·文档报告·代码讲解·安装调试】

&#x1f34a;作者&#xff1a;计算机编程-吉哥 &#x1f34a;简介&#xff1a;专业从事JavaWeb程序开发&#xff0c;微信小程序开发&#xff0c;定制化项目、 源码、代码讲解、文档撰写、ppt制作。做自己喜欢的事&#xff0c;生活就是快乐的。 &#x1f34a;心愿&#xff1a;点…

Swagger-go学习笔记

目录 Swagger的作用背景Swagger介绍 Swagger的基本使用1. 使用步骤2. 添加注释3. 生成接口文档数据4. 引入gin-swagger5. 测试结果6. 使用Token Swagger-go的中文文档通用API信息API操作MIME类型参数类型数据类型 Swagger的作用 背景 在传统的前端端分离的项目中&#xff0c;…

JAXB 使用记录 bean转xml xml转bean 数组 继承

JAXB 使用记录 部分内容引自 https://blog.csdn.net/gengzhy/article/details/127564536 基础介绍 JAXBContext类&#xff1a;是应用的入口&#xff0c;用于管理XML/Java绑定信息 Marshaller接口&#xff1a;将Java对象序列化为XML数据 Unmarshaller接口&#xff1a;将XML数…

数字孪生和数据分析:数字化时代的力量结合

在当今数字化时代&#xff0c;数据是无处不在的。企业、政府和个人不仅生成了大量数据&#xff0c;还寻求从中获取有价值的信息以进行更好的决策。在这个背景下&#xff0c;数字孪生和数据分析成为了迎合这一需求的两个关键概念。本文带大家一起探讨二者之间相辅相成的关系。 一…

黑马店评-04缓存更新策略,保证MySQL数据库中的数据和Redis中缓存的数据一致性

缓存更新策略(数据一致) 更新策略 缓存更新是Redis为了节约内存而设计出来的机制,当我们向Redis插入太多数据时就会导致缓存中的数据过多,所以Redis会对部分数据进行更新即淘汰 低一致性需求(数据长久不发生变化): 使用内存淘汰机制,例如店铺类型信息的查询缓存,因为这部分…

day02_运算符_if

零、今日内容 1.运算符 2.scanner 3.if,ifelse,elseif 复习 学习方法: 睡觉前过电影(1jdk,配置环境变量2idea使用3HelloWorld程序解释 4变量5数据类型6String) 主方法是每次都要写的,因为代码要执行(psvm) 输出语句每次都要写的,因为要看结果(sout) 1.声明变量的语法格式 数据类…