• 【C++】运算符重载 ① ( 运算符重载简介 | 运算符重载推衍 | 普通类型数据相加 | 对象类型数据相加 - 普通函数实现 / 运算符重载实现 | 运算符重载调用 - 函数名调用 / 运算符调 )






    一、运算符重载简介




    1、运算符重载引入


    2 2 2 个 int 类型的 基础类型 变量 , 相加 , 就是两个数值相加 ;

    int x = 10, y = 20;
    int z = x + y;
    
    • 1
    • 2

    如果是 2 2 2 个 类 对象 相加 , 那么相加的结果是什么 ?

    • 如果没有进行运算符重载 , 2 2 2 个类对象相加 , 直接报错 ;
    	// 自定义类型相加
    	Student s1(10, 120), s2(18, 170);
    	Student s3;
    	s3 = s1 + s2;
    
    • 1
    • 2
    • 3
    • 4

    报错信息如下 :

    error C2676: 二进制“+:“Student”不定义该运算符或到预定义运算符可接收的类型的转换
    1>已完成生成项目“HelloWorld.vcxproj”的操作 - 失败。
    
    • 1
    • 2

    在这里插入图片描述

    这是因为没有对 自定义类 的 " + " 运算符 , 进行 " 运算符重载 " , 直接使用加法操作 , 会报错 ;


    这里就需要为 类 设置 " 运算符重载 " , 才能进行类的相加操作 , 具体相加的结果 , 需要通过 重载的运算符函数的操作决定 ;


    2、运算符重载简介


    运算符重载 , 可以使 用户自定义数据 , 以 更简洁的方式 运作 ;

    运算符重载 是 C++ 语言中的 一种特殊的语言特性 , 运算符重载 机制允许 开发者 为自定义类型 重新定义 运算符 的行为 ;

    通过运算符重载 , 可以使 自定义类型 以 更自然 和 直观 的方式与 内置类型 进行交互 ;

    要重载一个 运算符 , 需要在 类中 声明一个 成员函数 或 非成员函数 , 并使用 operator 关键字 修饰 要重载的 运算符函数 , 这个函数的 参数 和 返回类型 决定了运算符的行为 ;





    二、运算符重载推衍




    1、普通类型数据相加


    基础数据类型相加 , 如 : int 类型 ;

    可以直接计算 , 获得结果 ;

    	// 基础数据类型相加
    	int x = 10, y = 20;
    	int z = x + y;
    	// 打印计算结果
    	cout << "z = " << z << endl;
    
    • 1
    • 2
    • 3
    • 4
    • 5

    2、对象类型数据相加


    以下面的 Student 对象为例 ;

    class Student
    {
    public:
    	// 带参构造函数 , 为参数设置默认值
    	Student(int age = 1, int height = 1)
    	{
    		this->age = age;
    		this->height = height;
    		cout << "执行 Student 的构造函数" << endl;
    	}
    
    	~Student()
    	{
    		cout << "执行 Student 的析构函数" << endl;
    	}
    
    public:
    	// 打印类数据
    	void print()
    	{
    		cout << "age = " << age << " , height = " << height << endl;
    	};
    
    public:
    	int age;		// 年龄
    	int height;		// 身高
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    2 2 2 个 Student 对象不能直接相加 ,

    	Student s1(10, 120), s2(18, 170);
    	Student s3;
    	s3 = s1 + s2;
    
    • 1
    • 2
    • 3

    强行进行加法计算 , 会报如下错误 , 只能在一个函数中实现对象相加的计算逻辑 ;

    报错信息如下 :

    error C2676: 二进制“+:“Student”不定义该运算符或到预定义运算符可接收的类型的转换
    1>已完成生成项目“HelloWorld.vcxproj”的操作 - 失败。
    
    • 1
    • 2

    3、对象类型数据相加 - 普通函数实现


    定义 addStudent 全局函数 , 接收 2 2 2 个 Student 类型对象 ;

    在上述全局函数中 , 实现 2 2 2 个 Student 对象相加 ;

    将其 age 和 height 成员分别相加即可 ;

    // 使用普通方法实现两个 Student 对象相加
    Student addStudent(Student& s1, Student& s2)
    {
    	Student s(s1.age + s2.age, s1.height + s2.height);
    	return s;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    调用时 , 将 2 2 2 个 Student 类型对象传入 addStudent 函数 , 即可完成 对象相加操作 ;

    	// 自定义类型相加
    	Student s1(10, 120), s2(18, 170);
    	Student s3 = addStudent(s1, s2);
    
    • 1
    • 2
    • 3

    4、对象类型数据相加 - 函数名修改为 operator+


    将上述 addStudent 函数的 函数名修改为 operator+ , 就变成了 运算符重载 ;

    // 使用 运算符重载 实现两个 Student 对象相加
    Student operator+(Student& s1, Student& s2)
    {
    	Student s(s1.age + s2.age, s1.height + s2.height);
    	return s;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    使用 operator+ 调用运算符重载函数

    operator+ 函数 , 可以直接使用 函数的方式调用 ,

    	// 自定义类型相加
    	Student s1(10, 120), s2(18, 170);
    	Student s3, s4;
    
    	// 全局函数实现对象相加
    	s3 = addStudent(s1, s2);
    
    	// 运算符重载实现对象相加
    	s4 = operator+(s1, s2);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    使用 + 运算符 调用运算符重载函数

    直接使用 + 运算符调用 运算符重载函数 ;

    	// 自定义类型相加
    	Student s1(10, 120), s2(18, 170);
    	Student s3, s4, s5;
    
    	// 全局函数实现对象相加
    	s3 = addStudent(s1, s2);
    
    	// 运算符重载实现对象相加
    	s4 = operator+(s1, s2);
    
    	// 运算符重载
    	s5 = s1 + s2;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12




    三、完整代码示例



    代码示例 :

    #include "iostream"
    using namespace std;
    
    class Student
    {
    public:
    	// 带参构造函数 , 为参数设置默认值
    	Student(int age = 1, int height = 1)
    	{
    		this->age = age;
    		this->height = height;
    	}
    
    public:
    	// 打印类数据
    	void print()
    	{
    		cout << "age = " << age << " , height = " << height << endl;
    	};
    
    public:
    	int age;		// 年龄
    	int height;		// 身高
    };
    
    // 使用普通方法实现两个 Student 对象相加
    Student addStudent(Student& s1, Student& s2)
    {
    	Student s(s1.age + s2.age, s1.height + s2.height);
    	return s;
    }
    
    // 使用 运算符重载 实现两个 Student 对象相加
    Student operator+(Student& s1, Student& s2)
    {
    	Student s(s1.age + s2.age, s1.height + s2.height);
    	return s;
    }
    
    int main() {
    	// 基础数据类型相加
    	int x = 10, y = 20;
    	int z = x + y;
    	// 打印计算结果
    	cout << "z = " << z << endl;
    
    	// 自定义类型相加
    	Student s1(10, 120), s2(18, 170);
    	Student s3, s4, s5;
    
    	// 全局函数实现对象相加
    	s3 = addStudent(s1, s2);
    
    	// 运算符重载实现对象相加
    	s4 = operator+(s1, s2);
    
    	// 运算符重载
    	s5 = s1 + s2;
    
    	s3.print();
    
        // 控制台暂停 , 按任意键继续向后执行
        system("pause");
    
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66

    执行结果 :

    z = 30
    age = 28 , height = 290
    age = 28 , height = 290
    age = 28 , height = 290
    请按任意键继续. . .
    
    • 1
    • 2
    • 3
    • 4
    • 5

    在这里插入图片描述

  • 相关阅读:
    [附源码]Python计算机毕业设计SSM街舞公司管理系统(程序+LW)
    源码解析FlinkKafkaConsumer支持周期性水位线发送
    Clickhouse 用户自定义外部函数
    (七)笔记.net core学习之反射、加载dll、读取moudle、类、方法、特性
    NX二次开发-一个简单的连接曲线例子剖析学会如何使用NXOPEN做二次开发
    MATLAB并行基础
    『手撕Vue-CLI』自动安装依赖
    还没用熟 TypeScript 社区已经开始抛弃了
    PDF阅读软件综合评测PDF Expert 、MarginNote、Notability Zotero
    SpringBoot 学习(九)Redis
  • 原文地址:https://blog.csdn.net/han1202012/article/details/133487811