c + + operator overloading ------------- 1 plus operator overloading

1 plus operator overload:

1.1 / / member function implementation + operator overload

Implementation statement:

Person operator+( Person& p)//Pass in a new parameter and add it to the previous one.
{//There is no calling relationship. Implicit relationship.
	Person temp;
	temp.m_A = this->m_A + p.m_A;
	temp.m_B = this->m_B + p.m_B;
	return temp;
}

Essential call statement:

Person p3 = p2.operator + (p1);//Intrinsic call of member function.
	cout << "mA:" << p3.m_A << " mB:" << p3.m_B << endl;

1.2 operator overloading can cause function overloading

Implementation statement:

Person operator+(const Person& p1, const Person& p2)
{
	Person temp(0, 0);
	temp.m_A = p1.m_A + p2.m_A;
	temp.m_B = p1.m_B + p2.m_B;
	return temp;
}

Essential call statement:

Person p4 = operator+(p1, p2);
cout << "mA:" << p4.m_A << " mB:" << p4.m_B << endl;

At the beginning, they have general call statements.

Person p4 = p2 + p1;

External ": additional implementation statement: (call mode of class and int type)

Operator overloading can cause function overloading 
Person operator+( const Person& p2, int val)
{
	Person temp;
	temp.m_A = p2.m_A + val;
	temp.m_B = p2.m_B + val;
	return temp;
}

Essential call statement:

 

	Person p4 = operator+(p2, 10);

Total code

```C++
class Person {
public:
	Person() {};
	Person(int a, int b)
	{
		this->m_A = a;
		this->m_B = b;
	}
	//The member function implements the overloading of the + operator
	Person operator+(const Person& p) {
		Person temp;
		temp.m_A = this->m_A + p.m_A;
		temp.m_B = this->m_B + p.m_B;
		return temp;
	}


public:
	int m_A;
	int m_B;
};

//Global function implementation + operator overload
//Person operator+(const Person& p1, const Person& p2) {
//	Person temp(0, 0);
//	temp.m_A = p1.m_A + p2.m_A;
//	temp.m_B = p1.m_B + p2.m_B;
//	return temp;
//}

//Operator overloading can cause function overloading 
Person operator+(const Person& p2, int val)  
{
	Person temp;
	temp.m_A = p2.m_A + val;
	temp.m_B = p2.m_B + val;
	return temp;
}

void test() {

	Person p1(10, 10);
	Person p2(20, 20);

	//Member function mode
	Person p3 = p2 + p1;  //Equivalent to P2 operaor+(p1)
	cout << "mA:" << p3.m_A << " mB:" << p3.m_B << endl;


	Person p4 = p3 + 10; //Equivalent to operator+(p3,10)
	cout << "mA:" << p4.m_A << " mB:" << p4.m_B << endl;

}

int main() {

	test();

	system("pause");

	return 0;
}
```

>Summary 1: the operators of expressions with built-in data types cannot be changed

>Summary 2: do not abuse operator overloading

Possible errors:

You can only have one correlation operator at a time.

2 shift left operator overload

Function: you can output custom data types

#include <iostream>
#include <string>
using namespace std;

class Person
{
	friend	ostream& operator<<(ostream& cout, Person& p);
public:

	Person(int a,int c)
	{
		m_a = a;
		m_b = c;
	}
	
private:
	int m_a;
	int m_b;
	};
ostream& operator<<(ostream& cout, Person& p)
	{
	cout << "p.m_a" <<    p.m_a << endl;
	cout << "p.m_b" <<    p.m_b << endl;
	return cout;
}
void test01()
{

	Person p(10000000,222222);
	cout << p;
}
int main()
	{	//Person p;
	test01();
	return 0;
	}

Declaration: / / the global function implements the left shift overload
/ / ostream objects can only have one
Ostream & operator < < (ostream & out, person & P) {/ / since it is an alias (Reference), it can be used without cout
        out << "a:" << p.m_ A << " b:" << p.m_ B;// Ostream stands for output stream, which is short for "output stream" in English. The common output stream object in C + + is the standard output stream cout,

3 increment operator overload

Total code:

#include <iostream>
#include <string>
using namespace std;

class MyInteger {

	friend ostream& operator<<(ostream& out, MyInteger myint);

public:
	MyInteger() {
		m_Num = 0;
	}
	//Front++
	MyInteger& operator++() {//If Myint & type is not returned, it is not for the same data type + +; Therefore, it cannot be changed to MyInteger return type;
		//First++
		m_Num++;
		//Return again
		return *this;
	}

	//Postposition++
	MyInteger operator++(int) {//Placeholder operator to distinguish between pre and post.
		//Return first
		MyInteger temp = *this; //Record the current value of itself, and then add 1 to the value of itself, but the previous value is returned. Return first and then + +;
		m_Num++;
		return temp;
	}

private:
	int m_Num;
};


ostream& operator<<(ostream& out, MyInteger myint) {
	out << myint.m_Num;
	return out;
}


//Pre + + first + + and then return
void test01() {
	MyInteger myInt;
	cout << ++myInt << endl;
	cout << myInt << endl;
}

//Post + + return first and then++
void test02() {

	MyInteger myInt;
	cout << myInt++ << endl;
	cout << myInt << endl;
}

int main() {

	test01();
	//test02();

	system("pause");

	return 0;
}

Postposition--

	}//Post -;
	MyInteger operator--(int) {//Placeholder operator to distinguish between pre and post.
	//Return first
		MyInteger temp = *this; 
		m_Num--;
		return temp;
	}
void test03()//Post -- return first and then--
{
	MyInteger myInt;
	cout << myInt-- << endl;
	cout << myInt << endl;
}

Front--

	MyInteger& operator--() 
	{
		m_Num--;
		//Return again
		return *this;
	}
void test04()//Front -- then -- return first/
{
	MyInteger myInt;
	cout << --myInt << endl;
	cout << myInt << endl;
}

>Summary: the pre increment returns the reference and the post increment returns the value

. 4 overload of assignment operator

The c + + compiler adds at least four functions to a class

1. Default constructor (no parameters, empty function body)

2. Default destructor (no parameters, empty function body)

3. The default copy constructor copies the value of the attribute

4. The assignment operator operator = copies the value of the attribute

If there are attributes in the class pointing to the heap area, the problem of deep and shallow copy will also occur during assignment

#include <iostream>
#include <string>
using namespace std;
class Person
{
public:

	Person(int age)
	{
		//Opening up age data to the heap area
		m_Age = new int(age);
	}

	//Overload assignment operator / / reference                                                                                                                                   
	Person& operator=(Person& p)
	{
		if (m_Age != NULL)
		{
			delete m_Age;
			m_Age = NULL;
		}
		//The code provided by the compiler is a shallow copy
		//m_Age = p.m_Age;

		//Provide deep copy to solve the problem of shallow copy
		m_Age = new int(*p.m_Age);//Address of the new value.

		//Return to itself
		return *this;
	}


	~Person()
	{
		if (m_Age != NULL)
		{
			delete m_Age;
			m_Age = NULL;
		}
	}

	//Age pointer
	int* m_Age;

};


void test01()
{
	Person p1(18);

	Person p2(20);

	Person p3(30);

	p3 = p2 = p1; //Assignment operation

	cout << "p1 Your age is:" << *p1.m_Age << endl;

	cout << "p2 Your age is:" << *p2.m_Age << endl;

	cout << "p3 Your age is:" << *p3.m_Age << endl;
}

int main() {

	test01();

	//int a = 10;
	//int b = 20;
	//int c = 30;

	//c = b = a;
	//cout << "a = " << a << endl;
	//cout << "b = " << b << endl;
	//cout << "c = " << c << endl;

	system("pause");

	return 0;
}

5 overloading of relational operators

**Function: * * overloaded relational operator, which allows two user-defined type objects to be compared

class Person
{
public:
	Person(string name, int age)
	{
		this->m_Name = name;
		this->m_Age = age;
	};

	bool operator==(Person & p)
	{
		if (this->m_Name == p.m_Name && this->m_Age == p.m_Age)
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	bool operator!=(Person & p)
	{
		if (this->m_Name == p.m_Name && this->m_Age == p.m_Age)
		{
			return false;
		}
		else
		{
			return true;
		}
	}

	string m_Name;
	int m_Age;
};

void test01()
{
	//int a = 0;
	//int b = 0;

	Person a("Sun WuKong", 18);
	Person b("Sun WuKong", 18);

	if (a == b)
	{
		cout << "a and b equal" << endl;
	}
	else
	{
		cout << "a and b Unequal" << endl;
	}

	if (a != b)
	{
		cout << "a and b Unequal" << endl;
	}
	else
	{
		cout << "a and b equal" << endl;
	}
}


int main() {

	test01();

	system("pause");

	return 0;
}

. 6 function call operator overloading

*The function call operator () can also be overloaded

*Because the method used after overloading is very similar to the call of function, it is called imitation function

*Imitation function has no fixed writing method and is very flexible

```C++
class MyPrint
{
public:
	void operator()(string text)
	{
		cout << text << endl;
	}

};
void test01()
{
	//Overloaded () operators are also called functors
	MyPrint myFunc;
	myFunc("hello world");
}


class MyAdd
{
public:
	int operator()(int v1, int v2)
	{
		return v1 + v2;
	}
};

void test02()
{
	MyAdd add;
	int ret = add(10, 10);
	cout << "ret = " << ret << endl;

	//Anonymous object call  
	cout << "MyAdd()(100,100) = " << MyAdd()(100, 100) << endl;
}

int main() {

	test01();
	test02();

	system("pause");

	return 0;
}

Keywords: C++ Back-end

Added by s3rg1o on Sun, 06 Feb 2022 22:41:51 +0200