C++day04(类中特殊成员函数、匿名对象、友元、常成员函数和常对象、运算符重载)

news2024/9/28 17:25:29

1> 思维导图

2> 整理代码

代码:

算术运算符重载:

#include <iostream>

using namespace std;
class Person
{
    //全局函数实现运算符重载需要权限
    friend const Person operator+(const Person L,const Person R);
private:
    int a;
    int b;
public:
    Person() {}
    Person(int a,int b){
        this->a=a;
        this->b=b;
    }
    void show(){
        cout << "a=" << a <<"b=" << b<<endl;
    }
//    成员函数实现运算符重载
//    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;
        temp.a=a/p.a;
        temp.b=b/p.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;
}
int main()
{
    Person p1(10,10);
    Person p2(10,20);
    Person p3=p1+p2;
    Person p4=p1-p2;
    Person p5=p1*p2;
    Person p6=p1/p2;
    p1.show();
    p2.show();
    p3.show();
    p4.show();
    p5.show();
    p6.show();
    return 0;
}

关系运算符重载:

#include <iostream>

using namespace std;
class Person
{
    //全局函数实现运算符重载需要权限
    friend bool operator>(const Person L,const Person R);
private:
    int a;
    int b;
public:
    Person() {}
    Person(int a,int b){
        this->a=a;
        this->b=b;
    }
    void show(){
        cout << "a=" << a <<"b=" << b<<endl;
    }

//    bool operator>(const Person p)const{
//        if(a>p.a && b>p.b){
//            return true;
//        }
//        return false;
//    }
    bool operator==(const Person p)const{
        if(a==p.a && b==p.b){
            return true;
        }
        return false;
    }
    bool operator<(const Person p)const{
        if(a<p.a && b<p.b){
            return true;
        }
        return false;
    }
    bool operator>=(const Person p)const{
        if(a>=p.a && b>=p.b){
            return true;
        }
        return false;
    }
    bool operator<=(const Person p)const{
        if(a<=p.a && b<=p.b){
            return true;
        }
        return false;
    }

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

int main()
{
    Person p1(10,10);
    Person p2(10,20);
    p1.show();
    p2.show();
    if(p2>p1){
        cout << "p2>p1" <<endl;
    }
    if(p2==p1){
        cout << "p2==p1" <<endl;
    }
    if(p2<p1){
        cout << "p2<p1" <<endl;
    }
    if(p2>=p1){
        cout << "p2>=p1" <<endl;
    }
    if(p2<=p1){
        cout << "p2<=p1" <<endl;
    }

    return 0;
}

赋值运算符重载

#include <iostream>

using namespace std;
class Person
{
    //全局函数实现运算符重载需要权限
    friend Person &operator+=(Person &L,const Person &R);
private:
    int a;
    int b;
public:
    Person() {}
    Person(int a,int b){
        this->a=a;
        this->b=b;
    }
    void show(){
        cout << "a=" << a <<"b=" << b<<endl;
    }
//    Person &operator+=(const Person p){
//        a+=p.a;
//        b+=p.b;
//        return *this;
//    }
    Person &operator-=(const Person p){
        a-=p.a;
        b-=p.b;
        return *this;
    }
    Person &operator*=(const Person p){
        a*=p.a;
        b*=p.b;
        return *this;
    }
    Person &operator/=(const Person p){
        a/=p.a;
        b/=p.b;
        return *this;
    }
    Person &operator%=(const Person p){
        a%=p.a;
        b%=p.b;
        return *this;
    }

};
//全局函数实现运算符重载
Person &operator+=(Person &L,const Person &R){
    L.a+=R.a;
    L.b+=R.b;
    return L;
}

int main()
{
    Person p1(10,10);
    Person p2(10,20);
    p1.show();
    p2.show();
//    cout << "+=";
//    p2+=p1;
//    p2.show();

//    cout << "-=";
//    p2-=p1;
//    p2.show();

//    cout << "*=";
//    p2*=p1;
//    p2.show();

//    cout << "/=";
//    p2/=p1;
//    p2.show();

    cout << "%=";
    p2%=p1;
    p2.show();

    return 0;
}

今日思维导图

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

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

相关文章

基于Springboot实现点餐平台网站管理系统项目【项目源码+论文说明】分享

基于Springboot实现点餐平台网站管理系统演示 摘要 随着现在网络的快速发展&#xff0c;网上管理系统也逐渐快速发展起来&#xff0c;网上管理模式很快融入到了许多商家的之中&#xff0c;随之就产生了“点餐平台网站”&#xff0c;这样就让点餐平台网站更加方便简单。 对于本…

泛函的含义,泛函分析

经常有同事和朋友讨论泛函分析是做什么的&#xff0c;所以做个小log 1. 泛函的含义 泛函的含义&#xff0c;笼统说&#xff0c;泛函是符合某种性质的任意函数&#xff1b;因为是任意的&#xff0c;所以就是泛泛的&#xff1b;但也没有不着边际的泛。 2. 泛函的例子 2.1 符合半…

uniapp+vue3+ts+uview-plus搭建项目步骤

创建项目 使用Vue3/Vite版&#xff0c;创建以 typescript 开发的工程 下载仓库 DCloud/uni-preset-vue - Gitee.com node版本&#xff1a;v16.18.0 npm版本&#xff1a; v8.19.2 依赖下载 解压之后&#xff0c;在vscode打开 通过终端运行 npm 命令下载依赖&#xff1a;npm ins…

1. Windows平台下如何编译C++版本的Redis库hiredis

Redis是一个key-value存储系统。和Memcached类似&#xff0c;它支持存储的value类型相对更多&#xff0c;包括string(字符串)、list(链表)、set(集合)、zset(sorted set --有序集合)和hash&#xff08;哈希类型&#xff09;。这些数据类型都支持push/pop、add/remove及取交集并…

Java中的抽象类和接口(Abstract Class and Interface)的区别

在Java面向对象编程中&#xff0c;总会用到接口和抽象类&#xff0c;他们都是对事物的一种抽象&#xff0c;有一些共同点但是也有很多区别。 接口Interface 在Java中接口需要用interface关键字定义&#xff0c;他是对一种行为的抽象&#xff0c;是一种约定的协议&#xff0c;…

微信小程序通过 movable-area 做一个与vuedraggable相似的上下拖动排序控件

因为只是做个小案例 我就直接代码写page页面里了 其实很简单 组件稍微改一下就好了 wxss /* 设置movable-area的宽度 */ .area{width: 100%; }/* a b c 每条元素的样式 */ movable-view {width: 100%;background-color: red;height: 40px;line-height: 40px;color: #FFFFFF;tex…

C语言进阶第五课-----------字符函数和字符串函数

作者前言 &#x1f382; ✨✨✨✨✨✨&#x1f367;&#x1f367;&#x1f367;&#x1f367;&#x1f367;&#x1f367;&#x1f367;&#x1f382; ​&#x1f382; 作者介绍&#xff1a; &#x1f382;&#x1f382; &#x1f382; &#x1f389;&#x1f389;&#x1f389…

库存管理方法有哪些?

本文将为大家讲解&#xff1a;库存管理方法有哪些&#xff1f; 库存管理是企业运营中的核心环节&#xff0c;它涉及到货物的采购、存储、销售和配送。有效的库存管理可以确保企业有足够的货物满足客户的需求&#xff0c;同时避免库存积压和浪费。为了达到这个目标&#xff0c;…

免费开源的非标项目型制造BOM一键导入方案介绍

非标项目型制造&#xff0c;每一个订单都会引入很多新料号、新BoM、新工艺路线。实施ERP/MES系统&#xff0c;实现生产管理数字化&#xff0c;第一步就是要导入这些料号、BoM和工艺。项目型制造&#xff0c;大多数订单只生产一次。但在ERP/MES系统中&#xff0c;订单的料号、Bo…

软件测试之概念篇2(瀑布模型、螺旋模型、增量模型和迭代模型、敏捷模型,V模型、W模型)

目录 开发模型 &#xff08;1&#xff09;瀑布模型 &#xff08;2&#xff09;螺旋模型 &#xff08;3&#xff09;增量模型和迭代模型 &#xff08;4&#xff09;敏捷模型 &#xff08;5&#xff09;测试模型&#xff08;V模型、W模型&#xff09; V模型 W模型 开发模型…

计算机等级考试—信息安全三级真题十

目录 一、单选题 二、填空题 三、综合题 一、单选题

企业文件、图纸加密软件哪个好——推荐【天锐绿盾加密软件】

天锐绿盾加密软件是一款全面、高效、方便的企业文件和图纸加密软件。 PC访问地址&#xff1a; isite.baidu.com/site/wjz012xr/2eae091d-1b97-4276-90bc-6757c5dfedee 以下是这款软件的一些优点和推荐理由&#xff1a; 天锐绿盾加密软件集文件加密、行为监控、权限控制于一体…

身为底层码农,你见过最无理需求是啥?

案例一 20万的项目&#xff0c;已经花了六十万了&#xff0c;客户突然又新提要求做一套百度的搜索系统&#xff0c;我尿了&#xff0c;一顿冥思苦想&#xff0c;然后做了一个搜索页面&#xff0c;把几百张表的每个字段都like一遍在搜索页面输入的查询内容&#xff0c;一次搜索…

五分钟Win11安装安卓(Android)子系统

十分钟&#xff0c;完成win11安装安卓子系统 Step1、地区设置为美国 Wini 进入设置页面&#xff0c;选择时间和语言-语言和区域- 区域-美国 Step2 安装 Windows Subsystem for Android™ with Amazon Appstore 访问如下连接&#xff0c;install即可 安卓子系统 Step3 安…

Ubuntu虚拟机安装教程

镜像下载地址&#xff1a; https://releases.ubuntu.com/22.04/ubuntu-22.04.3-desktop-amd64.iso 选择自己要存放的位置&#xff0c;不要放C盘 双击 选择镜像文件 等待 安装完成 能出网即可

PyTorch CUDA GPU高占用测试

0x00 问题描述 安装完成PyTorch、CUDA后&#xff0c;验证PyTorch是否能够通过CUDA高占用GPU&#xff08;占用>95%&#xff09;&#xff0c;特地使用以下代码测试。 0x01 代码设计 这个代码会持续执行神经网络的训练任务&#xff0c;每次循环都进行前向传播、反向传播和参数…

数据库系统工程师------时间周期

时间周期 计算机各种周期 时钟周期 机器&#xff08;CPU&#xff09;周期 指令周期 总线周期 时钟周期&#xff1a;也称振荡周期&#xff0c;定义为时钟频率的倒数。是计算机中最基本、最小的时间单位。 机器&#xff08;CPU&#xff09;周期&#xff1a;也称CPU周期&…

unity 使用模拟器进行Profiler性能调试

这篇文章主要记录如何实现通过模拟器对打包的app游戏进行Profiler调试。主要记录一些比较重要的点。 准备工作 首先你要能够打包unity的安卓包&#xff0c;如果没有安装安卓组件&#xff0c;请先安装组件。 安装完成以后&#xff0c;会在unity的安装目录找到相应的SDK 这个…

encoding/json vs json-iterator

encoding/json vs json-iterator 100% Compatibility 默认情况下&#xff0c;jsoniter 不会像标准库那样对映射键进行排序。如果你想要 100% 的兼容性&#xff0c;就这样使用 m : map[string]interface{}{"3": 3,"1": 1,"2": 2, } json : json…

10.10作业

运算符重载 #include <iostream>using namespace std;class Per{friend bool operator<(const Per &l, const Per &r);friend const Per operator(const Per &L,const Per &R);friend Per &operator-(Per &l , const Per &r); private:i…