Article directory
1. Unary operator overloading
1. List of unary operators
Unary operator: also known as unary operator
- Negation operator: -
- Dereference operator: *
- Get address operator: &
- Auto-increment operator: ++ , this operator is divided into two types: prefix and postfix;
- Decrement operator: – , this operator is divided into two types: prefix and postfix;
2. Implement operator overloading operations for the following classes
In this blog, the unary operator overloading operation is implemented for the following Student class;
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. Use global functions to implement prefix ++ auto-increment operator overloading
Use global functions to implement prepended ++ increment operator overloading:
- First, write the function name. The function name rule is "operate" followed by the operator to be overloaded . The function name is
operate++
;
operate++
- Then, write the function parameters according to the operands, and the parameters are generally references to objects;
- Member function: The parameter is 1 1A constant reference to an object, such as:
operate+(const Student& s1)
- Global function: parameters are 2 2References to 2 objects, such as:
operate+(Student& s1, Student& s2)
- Member function: The parameter is 1 1A constant reference to an object, such as:
operator++(Student& s)
- Then, according to the business improvement of the return value, the return value can be a reference/pointer/element, such as: the return value is an element
Student operate+(Student& s1, Student& s2)
; here, because the attributes in the Student& s parameter have changed, the Student& s parameter still needs to be returned when returning. itself, so the return value is Student& reference type;
Student& operator++(Student& s)
- Finally, implement the function body and write specific operators to operate the business logic;
// 使用 全局函数 实现 前置 ++ 自增运算符重载
// 重载 前置 ++ 运算符
// 实现 1 个 Student 对象 自增运算
// 由于 参数中的 Student& s 中的属性发生了变化
// 返回时仍需要返回 Student& s 参数本身
Student& operator++(Student& s)
{
s.age++;
s.height++;
return s;
};
In order to enable the global function to access the private members of the Student class, the global function needs to be declared as a friend function;
// 使用 全局函数 实现 前置 ++ 自增运算符重载
friend Student& operator++(Student& s);
4. Use global functions to implement prepositioning - overloading of decrement operators
Use global functions to implement prefix--decrement operator overloading:
- First, write the function name. The function name rule is "operate" followed by the operator to be overloaded . The function name is
operate--
;
operate--
- Then, write the function parameters according to the operands, and the parameters are generally references to objects;
- Member function: The parameter is 1 1A constant reference to an object, such as:
operate+(const Student& s1)
- Global function: parameters are 2 2References to 2 objects, such as:
operate+(Student& s1, Student& s2)
- Member function: The parameter is 1 1A constant reference to an object, such as:
operator--(Student& s)
- Then, according to the business improvement of the return value, the return value can be a reference/pointer/element, such as: the return value is an element
Student operate+(Student& s1, Student& s2)
; here, because the attributes in the Student& s parameter have changed, the Student& s parameter still needs to be returned when returning. itself, so the return value is Student& reference type;
Student& operator--(Student& s)
- Finally, implement the function body and write specific operators to operate the business logic;
// 使用 全局函数 实现 前置 -- 自减运算符重载
// 重载 前置 -- 运算符
// 实现 1 个 Student 对象 自减运算
// 由于 参数中的 Student& s 中的属性发生了变化
// 返回时仍需要返回 Student& s 参数本身
Student& operator--(Student& s)
{
s.age--;
s.height--;
return s;
};
In order to enable the global function to access the private members of the Student class, the global function needs to be declared as a friend function;
// 使用 全局函数 实现 前置 -- 自增运算符重载
friend Student& operator--(Student& s);
2. Complete code example
Code example:
#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;
};
Results of the :
age = 11 , height = 121
age = 10 , height = 120
请按任意键继续. . .