c++运算符重载

运算符重载

运算符重载是对已有的运算符进行重新定义(operator )

1.加号运算符重载

#include <iostream>
using namespace std;
//运算符重载对已有的运算符重新定义
//加号运算符重载
class person {
public:
	int m_a;
	int m_b;
	//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+ (person& p1, person& p2);
void test() {
	person p1;
	p1.m_a = 10;
	p1.m_b = 20;

	person p2;
	p2.m_a = 10;
	p2.m_b = 20;
	person p3 = p1 + p2;
	cout << p3.m_a << "    "<<p3.m_b << endl;
}
person operator+ (person& p1, person& p2) {
	person temp;
	temp.m_a = p1.m_a + p2.m_a;
	return temp;
}

int main() {
	test();

	system("pause");
	return 0;
}

2.重载左移运算符(<<)

#include <iostream>
using namespace std;
//重载左移(<<)运算符
class person {
public:
	int m_a;
	int m_b;
};
//写成成员函数无法保证cout在左侧
ostream& operator<< (ostream  &cout, person &p) {//cout的类型为ostream 返回引用保证连续输出
	cout << p.m_a << p.m_b;
	return cout;
}
void test() {
	person p;
	p.m_a = 2;
	p.m_b = 4;
	cout << p << "hello slam"<<endl;
}
int main() {
	test();
	system("pause");
	return 0;
}

3.重载++运算符

#include <iostream>
using namespace std;
class myint {
	friend ostream& operator<<(ostream& cout, myint p);
public:
	myint() {
		m_a = 0;
	}
	//重载前置++
	myint& operator++(){//返回引用是为了一直对一个数据进行操作
		m_a += 1;
		return *this;
	}
	//重载后置++ (返回值不作为重载条件) 
	myint operator++(int)//int为占位参数 返回值,不能返回局部变量的引用
	{
		myint temp = *this;//记录当前的值
		m_a++;
		return temp;//将记录的值返回
	}
	friend void test01();
private:
	int m_a;
};
//重载cout运算符
ostream& operator<< (ostream& cout, myint p) {
	cout << p.m_a;
	return cout;
}
void test02() {
	myint p;
	cout << ++(++p)<< endl;
}
void test01() {
	myint p;
	cout << p++ << endl;
	cout << p.m_a << endl;
}
int main() {
	test02();
	system("pause");
	return 0;
}

4.赋值运算符重载(深浅拷贝问题)

#include <iostream>
using namespace std;
//赋值运算符重载
class person {
public:
	person & operator= (person &p) {
		//如果堆区有 先清空
		if (this->m_a != NULL) {
			delete m_a;
			m_a = NULL;
		}
		//提供深拷贝 解决浅拷贝的问题
		this->m_a = new int(*p.m_a);
		return *this;
	}
	person(int a) {
		m_a = new int(a);
	}
	~person() {
		if (m_a != NULL) {
			delete m_a;
			m_a = NULL;
		 }

	}
	int *m_a;
};

void test() {
	person p1(10);
	person p2(20);
	person p3(30);
	p1 = p2=p3;
	cout << "p1= " << *p1.m_a << endl;
	cout << "p2= " << *p2.m_a << endl;
	cout << "p3= " << *p3.m_a << endl;
}
int main() {
	test();
	system("pause");
	return 0;
}

5.关系运算符(==)

#include <iostream>
using namespace std;
class person {
public:
	person(int a) {
		m_a = a;
	}
	int m_a;
	void operator== (person& p) {
		if (this->m_a == p.m_a) {
			cout << "true" << endl;
		}
	}
};

void test() {
	person p1(20);
	person p2(20);
	p1 == p2;
}
int main() {
	test();
	system("pause");
	return 0;
}

6.()重载

//函数调用运算符重载 () 仿函数
#include <iostream>
#include <string>
using namespace std;
class myprint {
public:
	void operator()(string str) {
		cout << str << endl;
	}
};
void test01() {
	myprint myp;
	myp("nisdji你是");
}
class myadd {
public:
	int operator()(int a, int b) {
		return a + b;
	}
};
void test02() {
	myadd mya;
	cout << mya(9, 8) << endl;
	//匿名对象调用
	cout << myadd()(8, 7) << endl;
}
int main() {
	test01();
	test02();
	system("pause");
	return 0;
}
posted @ 2021-08-07 22:28  pleasurea  阅读(42)  评论(0)    收藏  举报