一、一元运算符重载




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;		// 身高
};

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


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

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

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

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

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


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

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

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

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




二、完整代码示例



代码示例 :

#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;
};

执行结果 :

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

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

10-05 09:25