【C++】类和对象-C++运算符重载

运算符重载

【C++】类和对象-C++运算符重载_第1张图片

1.加号运算符重载

【C++】类和对象-C++运算符重载_第2张图片
【C++】类和对象-C++运算符重载_第3张图片
在这里插入图片描述
代码:

#include 
using namespace std;
/******************************************/
//加号运算符重载

class Person
{
public:

	int m_A;
	int m_B;

	//1、成员函数重载+号(不能与下面方式2同时存在,否则代码报错)
	//Person operator+(Person& p) {
	//	Person temp;
	//	temp.m_A = this->m_A + p.m_A;
	//	temp.m_B = this->m_B + p.m_B;
	//	return temp;
	//}
	Person operator+(int num) {
		Person temp;
		temp.m_A = this->m_A + num;
		temp.m_B = this->m_B + num;
		return temp;
	}
};

//2、全局函数重载+号(不能与上面方式1同时存在,否则代码报错)
Person operator+(Person& p1, Person& p2) {
	Person temp;
	temp.m_A = p1.m_A + p2.m_A;
	temp.m_B = p1.m_B + p2.m_B;
	return temp;
}
//Person operator+(Person& p1, int num) {
//	Person temp;
//	temp.m_A = p1.m_A + num;
//	temp.m_B = p1.m_B + num;
//	return temp;
//}

void test01() {
	Person p1;
	p1.m_A = 10;
	p1.m_B = 10;
	Person p2;
	p2.m_A = 10;
	p2.m_B = 10;

	//成员函数重载本质调用
	//Person p3 = p1.operator+(p2);

	//全局函数重载本质调用
	//Person p3 = operator+(p1, p2);

	Person p3 = p1 + p2;

	//运算符重载 ,函数也可以发生重载
	Person p4 = p1 + 100;//Person + int

	cout << "p3.m_A = " << p3.m_A << endl;
	cout << "p3.m_B = " << p3.m_B << endl;

	cout << "p4.m_A = " << p4.m_A << endl;
	cout << "p4.m_B = " << p4.m_B << endl;
}


/******************************************/
int main()
{
	/******************************************/
	test01();

	/******************************************/
	system("pause");

	return 0;
}

结果
【C++】类和对象-C++运算符重载_第4张图片

2.左移运算符重载

【C++】类和对象-C++运算符重载_第5张图片

【C++】类和对象-C++运算符重载_第6张图片
在这里插入图片描述
代码:

#include 
using namespace std;
/******************************************/
//左移运算符重载
class Person {

	friend ostream& operator<<(ostream& cout, Person& p);
public:
	Person(int a, int b) {
		m_A = a;
		m_B = b;
	}

	//利用成员函数重载 左移运算符 p.operator<<(cout) 简化版本 p << cout
	//不会利用成员函数重载<<运算符, 因为无法实现cout在左侧
	//void operator<<( cout ) {

	//}
private:
	int m_A;
	int m_B;
};

//只能利用全局函数重载左移运算符
//ostream对象只能有一个!!!
ostream& operator<<(ostream& cout, Person& p) {  //本质 operator<< (cout, p) 简化 cout << p
	cout << "m_A = " << p.m_A << " m_B = " << p.m_B;
	return cout; //ostream& 加上了& 就是进行了cout引用
}

void test01() {
	Person p(10, 10);
	//p.m_A = 10;
	//p.m_B = 10;

	cout << p << "hello" << endl;//链式编程
}

/******************************************/
int main()
{
	/******************************************/
	test01();

	/******************************************/
	system("pause");

	return 0;
}

3.递增运算符重载

【C++】类和对象-C++运算符重载_第7张图片
在这里插入图片描述
递增++代码:

#include 
using namespace std;
/******************************************/
//递增运算符重载

//自定义整型
class MyInteger {
	friend ostream& operator<<(ostream& cout, MyInteger& myint);//前置++运算符
	//friend ostream& operator<<(ostream& cout, MyInteger  myint);//后置++运算符

public:
	MyInteger() {
		m_Num = 0;
	}

	//重载前置++运算符 返回引用为了一直对一个数据进行递增操作
	MyInteger& operator++() {
		//先进行++运算
		m_Num++;
		//再将自身做返回
		return *this;
	}

	//重载后置++运算符
	//MyInteger operator++(int) int代表占位参数,可以用于区分前置和后置递增
	MyInteger operator++(int) {
		MyInteger temp = *this;
		//先返回自身结果
		m_Num++;
		//再进行++运算
		return temp;
	}


private:
	int m_Num;
};

//重载<<运算符 , 前置++运算符
ostream& operator<<(ostream& cout, MyInteger& myint) {
	cout << "myint输出: " << myint.m_Num;
	return cout;
}

重载<<运算符 , 后置++运算符
//ostream& operator<<(ostream& cout, MyInteger myint) {
//	cout << "myint输出: " << myint.m_Num;
//	return cout;
//}

//void test01() {
//	MyInteger myint;
//	cout << myint++ << endl;
//	cout << myint << endl;
//}

void test02() {
	MyInteger myint;
	cout << ++myint << endl;
	cout << myint << endl;
}
/******************************************/
int main()
{
	/******************************************/
	//test01();
	test02();

	/******************************************/
	system("pause");

	return 0;
}

递减–代码:

#include 
using namespace std;
/******************************************/
//递增运算符重载

//自定义整型
class MyInteger {
	//friend ostream& operator<<(ostream& cout, MyInteger& myint);
	friend ostream& operator<<(ostream& cout, MyInteger myint);

public:
	MyInteger() {
		m_Num = 2;
	}

	MyInteger& operator--() {
		m_Num--;
		return *this;
	}

	MyInteger operator--(int) {
		MyInteger temp = *this;
		m_Num--;
		return temp;
	}

private:
	int m_Num;
};

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

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

//void test01() {
//	MyInteger myint;
//	cout << --myint << endl;
//	cout <<   myint << endl;
//}

void test02() {
	MyInteger myint;
	cout << myint-- << endl;
	cout << myint   << endl;
}
/******************************************/
int main()
{
	/******************************************/
	//test01();
	test02();

	/******************************************/
	system("pause");

	return 0;
}

4.赋值运算符重载

【C++】类和对象-C++运算符重载_第8张图片
【C++】类和对象-C++运算符重载_第9张图片
代码:

#include 
using namespace std;
/******************************************/
//赋值运算符重载
class Person {
public:
	Person(int age) {
		//将年龄数据开辟到堆区
		m_Age = new int(age);
	}

	//重载赋值运算符
	Person& operator=(Person& p) {
		if (m_Age != NULL) {
			delete m_Age;
			m_Age = NULL;
		}
		//编译器提供的代码是浅拷贝
		//m_Age = p.m_Age;

		//提供深拷贝 解决浅拷贝的问题
		m_Age = new int(*p.m_Age);

		//返回本身
		return *this;
	}

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

	//年龄的指针
	int* m_Age;
};

void test01() {
	Person p1(18);

	Person p2(20);

	Person p3(30);

	p3 = p2 = p1; //赋值操作

	cout << "p1的年龄为: " << *p1.m_Age << endl;

	cout << "p2的年龄为: " << *p2.m_Age << endl;

	cout << "p3的年龄为: " << *p3.m_Age << endl;
}
/******************************************/
int main()
{
	/******************************************/
	test01();
	//test02();

	/******************************************/
	system("pause");

	return 0;
}

结果:
【C++】类和对象-C++运算符重载_第10张图片

5.关系运算符重载

【C++】类和对象-C++运算符重载_第11张图片
【C++】类和对象-C++运算符重载_第12张图片

#include 
using namespace std;
/******************************************/
//关系运算符重载
class Person {
public:
	Person(string name, int age) {
		this->m_Name = name;
		this->m_Age = age;
	}

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

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

private:
	string m_Name;
	int m_Age;
};

void test01() {
	Person p1("tom", 18);
	Person p2("tom", 18);

	if (p1 == p2) {
		cout << "p1和p2相等  " << endl;
	}
	else {
		cout << "p1和p2不相等" << endl;
	}

	if (p1 != p2) {
		cout << "p1和p2不相等  " << endl;
	}
	else {
		cout << "p1和p2相等" << endl;
	}
}

void test02();

/******************************************/
int main()
{
	/******************************************/
	test01();
	test02();

	/******************************************/
	system("pause");

	return 0;
}

void test02() {
	Person p1("jack", 18);
	Person p2("tom", 18);

	if (p1 == p2) {
		cout << "p1和p2相等  " << endl;
	}
	else {
		cout << "p1和p2不相等" << endl;
	}

	if (p1 != p2) {
		cout << "p1和p2不相等  " << endl;
	}
	else {
		cout << "p1和p2相等" << endl;
	}
}

6.函数调用运算符重载

【C++】类和对象-C++运算符重载_第13张图片
代码

#include 
using namespace std;
/******************************************/
//函数调用运算符重载
class MyPrint {
public:
	void operator()(string test) {
		cout << "print:" << test << endl;
	}
};

void test01() {
	//重载()操作符 也称为仿函数
	MyPrint myPrint;
	myPrint("hello");

	//匿名对象调用
	MyPrint()("匿名对象调用");
}

class Add {
public:
	int operator()(int num1, int num2) {
		return num1 + num2;
	}
};

void test02() {
	Add add;
	cout << add(10, 20) << endl;

	//匿名对象调用
	cout << Add()(10, 20) << endl;
}

/******************************************/
int main()
{
	/******************************************/
	test01();
	test02();

	/******************************************/
	system("pause");

	return 0;
}

结果
【C++】类和对象-C++运算符重载_第14张图片

你可能感兴趣的:(C++,c++,开发语言)