C++类和对象-C++运算符重载->加号运算符重载、左移运算符重载、递增运算符重载、赋值运算符重载、关系运算符重载、函数调用运算符重载

#include<iostream>
using namespace std;

//加号运算符重载

class Person {
public:
    Person() {};
    Person(int a, int b)
    {
        this->m_A = a;
        this->m_B = b;
    }
    //1.成员函数实现 + 号运算符重载
    Person operator+(const Person& p) {
        Person temp;
        temp.m_A = this->m_A + p.m_A;
        temp.m_B = this->m_B + p.m_B;
        return temp;
    }


public:
    int m_A;
    int m_B;
};

//2.全局函数实现 + 号运算符重载
//Person operator+(const Person& p1, const Person& p2) {
//    Person temp(0, 0);
//    temp.m_A = p1.m_A + p2.m_A;
//    temp.m_B = p1.m_B + p2.m_B;
//    return temp;
//}

//运算符重载 可以发生函数重载
Person operator+(const Person& p2, int val)  
{
    Person temp;
    temp.m_A = p2.m_A + val;
    temp.m_B = p2.m_B + val;
    return temp;
}

void test() {

    Person p1(10, 10);
    Person p2(20, 20);

    //成员函数重载本质调用
    Person p3 = p2 + p1;  //相当于 p2.operaor+(p1)
    cout << "mA:" << p3.m_A << " mB:" << p3.m_B << endl;

    //全局函数重载本质调用
    Person p4 = p1 + p2; //相当于 operator+(p1,p2)
    cout << "mA:" << p4.m_A << " mB:" << p4.m_B << endl;

    //运算符重载 也可以发生函数重载
    Person p5 = p3 + 10; //相当于 operator+(p3,10)
    cout << "mA:" << p5.m_A << " mB:" << p5.m_B << endl;

}

int main() {

    test();

    system("pause");

    return 0;
}

 总结1:对于内置的数据类型的表达式的的运算符是不可能改变的

 总结2:不要滥用运算符重载

#include<iostream>
using namespace std;

//左移运算符重载
class Person
{
    friend ostream& operator<<(ostream& cout, Person& p);

public:

    Person(int a, int b)
    {
        this->m_A = a;
        this->m_B = b;
    }
    //利用成员函数重载 左移运算符
    //成员函数 实现不了  p << cout 不是我们想要的效果
    //void operator<<(Person& p){
    //}

private:
    int m_A;
    int m_B;
};

//只能利用全局函数重载实现左移运算符
//ostream对象只能有一个
ostream& operator<<(ostream& cout, Person& p) //本质 operator<<(cout,p) 简化 cout<<p
{
    cout << "a:" << p.m_A << " b:" << p.m_B;
    return cout;
}

void test()
{
    Person p1(10, 20);
    cout << p1 << " hello world" << endl; //链式编程
}

int main()
{

    test();

    system("pause");

    return 0;
}

总结:重载左移运算符配合友元可以实现输出自定义数据类型

#include<iostream>
using namespace std;
//重载递增运算符
//自定义整型
class MyInteger
{

    friend ostream& operator<<(ostream& out, MyInteger myint);

public:
    MyInteger()
    {
        m_Num = 0;
    }
    //重载前置++运算符  返回引用为了一直对一个数据进行递增操作
    MyInteger& operator++()
    {
        //先++
        m_Num++;
        //再返回
        return *this;
    }

    //重载后置++运算符
    //int代表占位参数,可以用于区分前置和后置递增
    MyInteger operator++(int)
    {
        //先  记录当前结果
        MyInteger temp = *this; //记录当前本身的值,然后让本身的值加1,但是返回的是以前的值,达到先返回后++;
        //后  递增
        m_Num++;
        //最后将记录结果做返回
        return temp;
    }

private:
    int m_Num;
};

//重载<<运算符
ostream& operator<<(ostream& out, MyInteger myint)
{
    out << myint.m_Num;
    return out;
}


//前置++ 先++ 再返回
void test01()
{
    MyInteger myInt;
    cout << ++myInt << endl;
    cout << myInt << endl;
}

//后置++ 先返回 再++
void test02()
{

    MyInteger myInt;
    cout << myInt++ << endl;
    cout << myInt << endl;
}

int main()
{

    //test01();
    test02();

    system("pause");

    return 0;
}

总结: 前置递增返回引用,后置递增返回值

#include<iostream>
using namespace std;

//赋值运算符重载
class Person
{
public:

    Person(int age)
    {
        //将年龄数据开辟到堆区
        m_Age = new int(age);
    }

    //重载赋值运算符
    Person& operator=(Person &p)
    {
        //应该先判断是否有属性在堆区,如果有先释放感觉,然后再深拷贝
        if (m_Age != NULL)
        {
            delete m_Age;
            m_Age = NULL;
        }
        //编译器提供的代码是浅拷贝
        //m_Age = p.m_Age;

        //提供深拷贝 解决浅拷贝的问题
        m_Age = new int(*p.m_Age);

        //返回对象自身
        return *this;
    }


    ~Person()
    {
        if (m_Age != NULL)
        {
            delete m_Age;
            m_Age = NULL;
        }
    }

    //年龄的指针
    int *m_Age;

};


void test01()
{
    Person p1(18);

    Person p2(20);

    Person p3(30);

    p3 = p2 = p1; //赋值操作

    cout << "p1的年龄为:" << *p1.m_Age << endl;

    cout << "p2的年龄为:" << *p2.m_Age << endl;

    cout << "p3的年龄为:" << *p3.m_Age << endl;
}

int main() {

    test01();

    //int a = 10;
    //int b = 20;
    //int c = 30;

    //c = b = a;
    //cout << "a = " << a << endl;
    //cout << "b = " << b << endl;
    //cout << "c = " << c << endl;

    system("pause");

    return 0;
}

#include<iostream>
using namespace std;
#include<string>

//重载关系运算符
class Person
{
public:
    Person(string name, int age)
    {
        this->m_Name = name;
        this->m_Age = age;
    };
    //重载 == 号
    bool operator==(Person & p)
    {
        if (this->m_Name == p.m_Name && this->m_Age == p.m_Age)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    bool operator!=(Person & p)
    {
        if (this->m_Name == p.m_Name && this->m_Age == p.m_Age)
        {
            return false;
        }
        else
        {
            return true;
        }
    }

    string m_Name;
    int m_Age;
};

void test01()
{
    //int a = 0;
    //int b = 0;

    Person a("孙悟空", 18);
    Person b("孙悟空", 18);

    if (a == b)
    {
        cout << "a和b相等" << endl;
    }
    else
    {
        cout << "a和b不相等" << endl;
    }

    if (a != b)
    {
        cout << "a和b不相等" << endl;
    }
    else
    {
        cout << "a和b相等" << endl;
    }
}


int main() {

    test01();

    system("pause");

    return 0;
}

#include<iostream>
using namespace std;
#include<string>

//函数调用运算符重载
//打印输出类
class MyPrint
{
public:
    //重载函数调用运算符
    void operator()(string text)
    {
        cout << text << endl;
    }

};
void myFunc2(string text)
{
    cout << text << endl;
}
void test01()
{
    //重载的()操作符 也称为仿函数
    MyPrint myFunc;
    myFunc("hello world");//由于使用起来非常类似于函数调用,因此称为仿函数
    myFunc2("hello world");
}
//仿函数非常灵活,没有固定的写法
//加法类
class MyAdd
{
public:
    int operator()(int v1, int v2)
    {
        return v1 + v2;
    }
};

void test02()
{
    MyAdd add;
    int ret = add(10, 10);
    cout << "ret = " << ret << endl;

    //匿名函数对象调用  
    cout << "MyAdd()(100,100) = " << MyAdd()(100, 100) << endl;
}

int main() {

    test01();
    test02();

    system("pause");
    return 0;
}

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

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

【有哪些值得计算机专业学生加入的国企?】

&#x1f680; 编辑&#xff1a;“码上有前” &#x1f680;作者丨重庆搬砖喵知乎 &#x1f680; 文章简介 &#xff1a;计算机专业未来出路 &#x1f680; 欢迎小伙伴们 点赞&#x1f44d;、收藏⭐、留言&#x1f4ac; &#x1f680;来源&#xff1a;https://www.zhihu.com/qu…

PHP+vue+mysql校园学生社团管理系统574cc

运行环境:phpstudy/wamp/xammp等 开发语言&#xff1a;php 后端框架&#xff1a;Thinkphp 前端框架&#xff1a;vue.js 服务器&#xff1a;apache 数据库&#xff1a;mysql 数据库工具&#xff1a;Navicat/phpmyadmin 前台功能&#xff1a; 首页&#xff1a;展示社团信息和活动…

相机图像质量研究(18)常见问题总结:CMOS期间对成像的影响--CFA

系列文章目录 相机图像质量研究(1)Camera成像流程介绍 相机图像质量研究(2)ISP专用平台调优介绍 相机图像质量研究(3)图像质量测试介绍 相机图像质量研究(4)常见问题总结&#xff1a;光学结构对成像的影响--焦距 相机图像质量研究(5)常见问题总结&#xff1a;光学结构对成…

MySQL主从环境,主库改端口后,从库如何操作?

主库&#xff1a;mysql-111 从库&#xff1a;mysql-112 主库由3306端口修改成3307后&#xff0c; 从库执行如下命令 mysql> stop slave; mysql> change master to master_port3307; mysql> CHANGE MASTER TO MASTER_HOST192.168.10.111,MASTER_USERbeifen,MASTER_PA…

Hive on Spark配置

前提条件 1、安装好Hive&#xff0c;参考&#xff1a;Hive安装部署-CSDN博客 2、下载好Spark安装包&#xff0c;链接&#xff1a;https://pan.baidu.com/s/1plIBKPUAv79WJxBSbdPODw?pwd6666 3、将Spark安装包通过xftp上传到/opt/software 安装部署Spark 1、解压spark-3.3…

【教程】Kotlin语言学习笔记(二)——数据类型(持续更新)

写在前面&#xff1a; 如果文章对你有帮助&#xff0c;记得点赞关注加收藏一波&#xff0c;利于以后需要的时候复习&#xff0c;多谢支持&#xff01; 【Kotlin语言学习】系列文章 第一章 《认识Kotlin》 第二章 《数据类型》 文章目录 【Kotlin语言学习】系列文章一、基本数据…

从零开始做题:逆向 ret2libc jarvisoj level1

1.题目信息 BUUCTF在线评测 2.原理 篡改栈帧上的返回地址为攻击者手动传入的shellcode所在缓冲区地址&#xff0c;并且该区域有执行权限。 3.解题步骤 3.1 首先使用checksec工具查看它开了啥保护措施 基本全关&#xff0c;栈可执行。 rootpwn_test1604:/ctf/work/9# chec…

【双指针】【C++算法】1537. 最大得分

作者推荐 【深度优先搜索】【树】【图论】2973. 树中每个节点放置的金币数目 本文涉及知识点 双指针 LeetCoce 1537. 最大得分 你有两个 有序 且数组内元素互不相同的数组 nums1 和 nums2 。 一条 合法路径 定义如下&#xff1a; 选择数组 nums1 或者 nums2 开始遍历&…

极其抽象的SpringSecurity理解

原始&#xff1a;A → B Security&#xff1a;A → S → B 太抽象了&#xff0c;看不懂啊T_T 抽象故事 故事大概&#xff1a;C是一个大区&#xff0c;拥有巨大的火力&#xff08;C准备联合B吞并掉A&#xff09;&#xff0c;A得到了这个消息&#xff0c;…

解决‘vue‘ 不是内部或外部命令,也不是可运行的程序(设置全局变量)

发现是没有执行&#xff1a; npm install -g vue/cli 但是发现还是不行 此时&#xff0c;我们安装了 Vue CLI&#xff0c;但是在运行 vue ui 命令时出现了问题。这通常是因为全局安装的 Vue CLI 的路径没有被正确地添加到系统的环境变量中。 可以尝试以下几种方法来解决这个问…

视觉slam十四讲学习笔记(四)相机与图像

理解理解针孔相机的模型、内参与径向畸变参数。理解一个空间点是如何投影到相机成像平面的。掌握OpenCV的图像存储与表达方式。学会基本的摄像头标定方法。 目录 前言 一、相机模型 1 针孔相机模型 2 畸变 单目相机的成像过程 3 双目相机模型 4 RGB-D 相机模型 二、图像…

LEETCODE 315. 计算右侧小于当前元素的个数(归并)

class Solution { public: // 将count声明为publicvector<int> count; vector<int> indexs,tmp;public:vector<int> countSmaller(vector<int>& nums) {//归并int left0;int rightnums.size()-1;//计数// vector<int> count(nums.size()); …

【MATLAB】PSO_BP神经网络回归预测(多输入多输出)算法原理

有意向获取代码&#xff0c;请转文末观看代码获取方式~也可转原文链接获取~ 1 基本定义 PSO-BP神经网络回归预测&#xff08;多输入多输出&#xff09;算法是一种结合粒子群优化算法&#xff08;PSO&#xff09;和反向传播&#xff08;BP&#xff09;神经网络的混合算法。该算…

CSS之选择器、优先级、继承

1.CSS选择器 常用的选择器 <body><div class"parent"><div id"one" style"background: blue" class"child">1<div class"one_one">11</div><div style"background-color: blueviole…

vue3 Element Plus 基于webstorm练习

提要 vue是前端框架&#xff0c;Elemen是组件库。前端框架和组件库的区别与联系 nodejs 脚本语言需要一个解析器才能运行&#xff0c;JavaScript是脚本语言&#xff0c;在不同的位置有不一样的解析器&#xff0c;如写入html的js语言&#xff0c;浏览器是它的解析器角色。而对…

浅谈业务场景中缓存的使用

业务场景中缓存的使用 一、背景二、缓存分类1.本地缓存2.分布式缓存 三、缓存读写模式1.读请求2.写请求 四、缓存穿透1.缓存空对象2.请求校验3.请求来源限制4.布隆过滤器 五、缓存击穿1.改变过期时间2.串行访问数据库 六、缓存雪崩1.避免集中过期2.提前更新缓存 七、缓存与数据…

TMGM公司官网介绍

TMGM主要提供外汇、贵金属、原油、股指等CFD产品&#xff0c;客户可以根据个人的交易习惯选择其中一种或多种进行投资。具体来说&#xff0c;TMGM的金融产品包括但不限于货币对、黄金、原油、股票指数等。此外&#xff0c;TMGM还提供多种账户类型以满足不同客户的交易需求。 请…

error MSB8008: 指定的平台工具集(v143)未安装或无效。请确保选择受支持的 PlatformToolset 值解决办法

右击解决方案&#xff0c;选择属性 将工具集为143的修改为其他&#xff0c;如图 重新编译即可运行

DDC技术:AIGC网络的革命性解决方案

2023年&#xff0c;人工智能生成内容&#xff08;AIGC&#xff09;技术将蓬勃发展&#xff0c;其中ChatGPT作为一个典型案例&#xff0c;在文本生成、代码开发和诗歌创作等多个领域引起行业变革。DDC技术对改变网络格局具有创新和突破性意义&#xff0c;很大程度上提升了效率和…

Python 读取pdf文件

Python 实现读取pdf文件简单示例。 安装命令 需要安装操作pdf的三方类库&#xff0c;命令如下&#xff1a; pip install pdfminer3K 安装过程如下&#xff1a; 引入类库 需要引入很多的类库。 示例如下&#xff1a; import sys import importlib importlib.reload(sys)fr…
最新文章