首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >【C++】运算符重载 ④ ( 一元运算符重载 | 使用 全局函数 实现 前置 ++ 自增运算符重载 | 使用 全局函数 实现 前置 - - 自减运算符重载 )

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

作者头像
韩曙亮
发布2023-10-15 17:24:56
发布2023-10-15 17:24:56
5760
举报

一、一元运算符重载

1、一元运算符列举

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

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

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

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

代码语言:javascript
复制
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;		// 身高
};

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

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

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

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

代码语言:javascript
复制
	// 使用 全局函数 实现 前置 ++ 自增运算符重载
	friend Student& operator++(Student& s);

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

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

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

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

代码语言:javascript
复制
	// 使用 全局函数 实现 前置 -- 自增运算符重载
	friend Student& operator--(Student& s);

二、完整代码示例


代码示例 :

代码语言:javascript
复制
#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;
};

执行结果 :

代码语言:javascript
复制
age = 11 , height = 121
age = 10 , height = 120
请按任意键继续. . .
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2023-10-11,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、一元运算符重载
    • 1、一元运算符列举
    • 2、为下面的类实现运算符重载操作
    • 3、使用 全局函数 实现 前置 ++ 自增运算符重载
    • 4、使用 全局函数 实现 前置 - - 自减运算符重载
  • 二、完整代码示例
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档