[C++] Operator overloading ③ (Binary operator overloading | Operator overloading steps | Global function to implement operator overloading | Member function to implement operator overloading | Friend function to implement operator overloading)





1. Operator overloading steps




1. Description of operator overloading steps


Operator overloading steps:

  • First, write the function name. The function name rule is "operate" followed by the operator to be overloaded , such operate+as overloading the plus operator;
  • 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)
  • Then, according to the business perfect return value, the return value can be a reference/pointer/element, such as: the return value is an elementStudent operate+(Student& s1, Student& s2)
  • Finally, implement the function body and write specific operators to operate the business logic;

2. Operator overloading class


The following takes the Student class as an example to write member function/global function implementation operator overloading code;

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

3. Global functions implement operator overloading


Use global functions to implement operator overloading and overload the + operator;


Global functions implement 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)
operate+(Student& s1, Student& s2)
  • Then, according to the business perfect return value, the return value can be a reference/pointer/element, such as: the return value is an elementStudent operate+(Student& s1, Student& s2)
Student operator+(Student& s1, Student& s2)
  • Finally, implement the function body and write specific operators to operate the business logic;
// 使用 全局函数 实现 运算符重载 
// 重载 + 运算符
// 实现两个 Student 对象相加
Student operator+(Student& s1, Student& s2)
{
    
    
	Student student(s1.age + s2.age, s1.height + s2.height);
	return student;
};

4. Member functions implement operator overloading


Use member functions to implement operator overloading, overloading - operator;


Member functions implement 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)
operator-(Student& s)
  • Then, perfect the return value according to the business, and the return value can be a reference/pointer/element;
Student operator-(Student& s)
  • Finally, implement the function body and write specific operators to operate the business logic;
public:
	// 使用 成员函数 实现 运算符重载 
	// 重载 - 运算符
	// 实现两个 Student 对象相加
	Student operator-(Student& s)
	{
    
    
		Student student(this->age - s.age, this->height - s.height);
		return student;
	};

5. 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:
	int age;		// 年龄
	int height;		// 身高
};

// 使用 全局函数 实现 运算符重载 
// 重载 + 运算符
// 实现两个 Student 对象相加
Student operator+(Student& s1, Student& s2)
{
    
    
	Student student(s1.age + s2.age, s1.height + s2.height);
	return student;
};

int main() {
    
    
	// 自定义类型相加
	Student s1(10, 120), s2(18, 170);
	Student s3, s4, s5;

	s3 = s1 + s2;
	s3.print();

	s4 = s1 - s2;
	s4.print();

    // 控制台暂停 , 按任意键继续向后执行
    system("pause");

    return 0;
};

Results of the :

age = 28 , height = 290
age = -8 , height = -50
请按任意键继续. . .

Insert image description here





2. Friend functions implement operator overloading




1. Friend functions implement operator overloading


If the members in the class are all private members,

In operator overloading, private members need to be accessed for calculation,

In member functions, private members can be accessed normally,

But in the global function, private members cannot be accessed;


At this time, you need to declare the global function as a friend function of the class, so that you can access private members in the global function (friend function);


Member variables in a class are private members;

private:
	int age;		// 年龄
	int height;		// 身高

A global function is defined, which accesses private members in the class,

// 使用 全局函数 实现 运算符重载 
// 重载 + 运算符
// 实现两个 Student 对象相加
Student operator+(Student& s1, Student& s2)
{
    
    
	Student student(s1.age + s2.age, s1.height + s2.height);
	return student;
};

The global function needs to be declared as a friend function. At this time, the global function is used to implement normal execution of operator overloading;

private:
	friend Student operator+(Student& s1, Student& s2);

2. Code example - Friend function implements operator overloading


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);

private:
	int age;		// 年龄
	int height;		// 身高
};

// 使用 全局函数 实现 运算符重载 
// 重载 + 运算符
// 实现两个 Student 对象相加
Student operator+(Student& s1, Student& s2)
{
    
    
	Student student(s1.age + s2.age, s1.height + s2.height);
	return student;
};

int main() {
    
    
	// 自定义类型相加
	Student s1(10, 120), s2(18, 170);
	Student s3, s4, s5;

	s3 = s1 + s2;
	s3.print();

	s4 = s1 - s2;
	s4.print();

    // 控制台暂停 , 按任意键继续向后执行
    system("pause");

    return 0;
};

Results of the :

age = 28 , height = 290
age = -8 , height = -50
请按任意键继续. . .

Insert image description here

Guess you like

Origin blog.csdn.net/han1202012/article/details/133542596