• 【C++】运算符重载 ④ ( 一元运算符重载 | 使用 全局函数 实现 前置 ++ 自增运算符重载 | 使用 全局函数 实现 前置 - - 自减运算符重载 )






    一、一元运算符重载




    1、一元运算符列举


    一元运算符 : 又称为 单目运算符

    • 取反运算符 : -
    • 解引用运算符 : *
    • 取地址运算符 : &
    • 自增运算符 : ++ , 该运算符分为 前置 和 后置 两种类型 ;
    • 自减运算符 : – , 该运算符分为 前置 和 后置 两种类型 ;

    2、为下面的类实现运算符重载操作


    本博客中 , 为下面的 Student 类实现 一元运算符 重载操作 ;

    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:
    	// 使用 成员函数 实现 运算符重载 
    	// 重载 - 运算符
    	// 实现两个 Student 对象相加
    	Student operator-(Student& s)
    	{
    		Student student(this->age - s.age, this->height - s.height);
    		return student;
    	};
    
    private:
    	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
    • 28
    • 29
    • 30
    • 31

    3、使用 全局函数 实现 前置 ++ 自增运算符重载


    使用 全局函数 实现 前置 ++ 自增运算符重载 :

    • 首先 , 写出函数名 , 函数名规则为 " operate " 后面跟上要重载的运算符 , 函数名是 operate++ ;
    operate++
    
    • 1
    • 然后 , 根据操作数 写出函数参数 , 参数一般都是 对象的引用 ;
      • 成员函数 : 参数是 1 1 1 个对象的常量引用 , 如 : operate+(const Student& s1)
      • 全局函数 : 参数是 2 2 2 个对象的引用 , 如 : operate+(Student& s1, Student& s2)
    operator++(Student& s)
    
    • 1
    • 再后 , 根据业务完善返回值 , 返回值可以是 引用 / 指针 / 元素 , 如 : 返回值是元素 Student operate+(Student& s1, Student& s2) ; 此处 , 由于 参数中的 Student& s 中的属性发生了变化 , 返回时仍需要返回 Student& s 参数本身 , 因此返回值是 Student& 引用类型 ;
    Student& operator++(Student& s)
    
    • 1
    • 最后 , 实现函数体 , 编写具体的运算符操作业务逻辑 ;
    // 使用 全局函数 实现 前置 ++ 自增运算符重载
    // 重载 前置 ++ 运算符
    // 实现 1 个 Student 对象 自增运算
    // 由于 参数中的 Student& s 中的属性发生了变化 
    // 返回时仍需要返回 Student& s 参数本身
    Student& operator++(Student& s)
    {
    	s.age++;
    	s.height++;
    	return s;
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    为了使全局函数中能访问 Student 类的私有成员 , 需要将该全局函数声明为 友元函数 ;

    	// 使用 全局函数 实现 前置 ++ 自增运算符重载
    	friend Student& operator++(Student& s);
    
    • 1
    • 2

    4、使用 全局函数 实现 前置 - - 自减运算符重载


    使用 全局函数 实现 前置 - - 自减运算符重载 :

    • 首先 , 写出函数名 , 函数名规则为 " operate " 后面跟上要重载的运算符 , 函数名是 operate-- ;
    operate--
    
    • 1
    • 然后 , 根据操作数 写出函数参数 , 参数一般都是 对象的引用 ;
      • 成员函数 : 参数是 1 1 1 个对象的常量引用 , 如 : operate+(const Student& s1)
      • 全局函数 : 参数是 2 2 2 个对象的引用 , 如 : operate+(Student& s1, Student& s2)
    operator--(Student& s)
    
    • 1
    • 再后 , 根据业务完善返回值 , 返回值可以是 引用 / 指针 / 元素 , 如 : 返回值是元素 Student operate+(Student& s1, Student& s2) ; 此处 , 由于 参数中的 Student& s 中的属性发生了变化 , 返回时仍需要返回 Student& s 参数本身 , 因此返回值是 Student& 引用类型 ;
    Student& operator--(Student& s)
    
    • 1
    • 最后 , 实现函数体 , 编写具体的运算符操作业务逻辑 ;
    // 使用 全局函数 实现 前置 -- 自减运算符重载
    // 重载 前置 -- 运算符
    // 实现 1 个 Student 对象 自减运算
    // 由于 参数中的 Student& s 中的属性发生了变化 
    // 返回时仍需要返回 Student& s 参数本身
    Student& operator--(Student& s)
    {
    	s.age--;
    	s.height--;
    	return s;
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    为了使全局函数中能访问 Student 类的私有成员 , 需要将该全局函数声明为 友元函数 ;

    	// 使用 全局函数 实现 前置 -- 自增运算符重载
    	friend Student& operator--(Student& s);
    
    • 1
    • 2




    二、完整代码示例



    代码示例 :

    #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:
    	// 使用 成员函数 实现 运算符重载 
    	// 重载 - 运算符
    	// 实现两个 Student 对象相加
    	Student operator-(Student& s)
    	{
    		Student student(this->age - s.age, this->height - s.height);
    		return student;
    	};
    
    private:
    	// 友元函数 实现 全局函数 运算符重载
    	// 使用 全局函数 实现 + 运算符重载 
    	friend Student operator+(Student& s1, Student& s2);
    
    	// 使用 全局函数 实现 前置 ++ 自增运算符重载
    	friend Student& operator++(Student& s);
    
    	// 使用 全局函数 实现 前置 -- 自增运算符重载
    	friend Student& operator--(Student& s);
    
    private:
    	int age;		// 年龄
    	int height;		// 身高
    };
    
    // 使用 全局函数 实现 运算符重载 
    // 重载 + 运算符
    // 实现两个 Student 对象相加
    Student operator+(Student& s1, Student& s2)
    {
    	Student student(s1.age + s2.age, s1.height + s2.height);
    	return student;
    };
    
    // 使用 全局函数 实现 前置 ++ 自增运算符重载
    // 重载 前置 ++ 运算符
    // 实现 1 个 Student 对象 自增运算
    // 由于 参数中的 Student& s 中的属性发生了变化 
    // 返回时仍需要返回 Student& s 参数本身
    Student& operator++(Student& s)
    {
    	s.age++;
    	s.height++;
    	return s;
    };
    
    // 使用 全局函数 实现 前置 -- 自减运算符重载
    // 重载 前置 -- 运算符
    // 实现 1 个 Student 对象 自减运算
    // 由于 参数中的 Student& s 中的属性发生了变化 
    // 返回时仍需要返回 Student& s 参数本身
    Student& operator--(Student& s)
    {
    	s.age--;
    	s.height--;
    	return s;
    };
    
    int main() {
    	// 自定义类型相加
    	Student s1(10, 120), s2(18, 170);
    	Student s3, s4, s5;
    
    	++s1;
    	s1.print();
    
    	--s1;
    	s1.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
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95

    执行结果 :

    age = 11 , height = 121
    age = 10 , height = 120
    请按任意键继续. . .
    
    • 1
    • 2
    • 3

    在这里插入图片描述

  • 相关阅读:
    触控笔哪个牌子好用?主动电容笔和被动电容笔的区别
    1036 Boys vs Girls
    说说TIME_WAIT和CLOSE_WAIT区别
    剑指Offer 第53题:数字在升序数组中出现的次数
    MySQL read 查询语句5 日期时间相关函数
    如何冻结模型,避免 model.train() 改变模型部分模块
    分布式执行引擎ray入门--(3)Ray Train
    FFmpeg开发笔记(二十)Linux环境给FFmpeg集成AVS3解码器
    Hadoop学习记录4--Maven、HDFS API编程
    java毕业生设计医院病房管理系统计算机源码+系统+mysql+调试部署+lw
  • 原文地址:https://blog.csdn.net/han1202012/article/details/133562448