C++ 运算符重载

C++ 运算符重载

加号运算符:+ 重载

#include<iostream>
#include<string>
 
using namespace std;
 
//加号运算符重载
 
class Person {
public:
 
	//1.通过成员函数重载运算符加号
	/*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 p3 = p1.operator+(p2);
 
 
	int m_A;
	int m_B;
 
 
 
};
 
//2.通过全局函数重载运算符加号
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 p3 = operator+(p1,p2);
 
//3.运算符重载 也可以发生函数重载
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 + p2;
 
 
	Person p4 = p1 + 20;
//运算符重载 也可以发生函数重载
	cout << "p3.m_A是:" << p3.m_A << "\np3.m_B是:" << p3.m_B << endl;
	cout << "p4.m_A是:" << p4.m_A << "\np4.m_B是:" << p4.m_B << endl;
}
 
//1.通过成员函数重载运算符加号
 
//2.通过全局函数重载运算符加号
 
int main() {
	test01();
	return 0;
}

左移运算符:<< 重载

#include<iostream>
#include<string>
 
using namespace std;
 
//左移运算符重载
 
class Person {
	friend void test01();
	friend ostream& operator<<(ostream& cout, Person p);
public:
	//利用成员函数重载 左移运算符 p.operator<<(cout) 简化版本 p<<cout
	//所以通常不会利用成员函数重载 左移运算符 因为无法实现cout在左侧
	//void operator<<(cout) {
 
	//}
private:
	int m_A;
	int m_B;
 
	
};
 
//只能利用全局函数重载左移运算符
ostream& operator<<(ostream &cout,Person p) {//本质 operator<<(cout,p) 简化 cout << p
	cout << "m_A=" << p.m_A << "\nm_B=" << p.m_B ;
	return cout;
}
 
void test01()
{
	Person p;
	p.m_A = 10;
	p.m_B = 10;
 
 
	cout << p << endl;;
	
}
 
 
 
 
 
 
 
 
 
 
 
 
int main() {
 
	test01();
 
 
 
 
	system("pause");
	return 0;
 
}

递增运算符:++ 重载

#include<iostream>
#include<string>
 
using namespace std;
 
 
 
class MyInteger {
 
	friend ostream& operator<<(ostream& cout, MyInteger myint);
public:
 
 
	//构造函数初始化属性m_Num
	MyInteger() {
		m_Num = 0;
	}
 
	//前置++ 把自身做一个返回
	MyInteger& operator++() {
		m_Num++;
		return *this;
	}
 
	//后置++   占位参数int
	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;
}
 
 
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<iostream>
#include<string>
 
using namespace std;
 
//重载关系运算符
 
class Person {
public:
	string m_Name;
	int m_Age;
 
 
	Person(string name,int age) {
		m_Name = name;
		m_Age = age;
	}
 
	//重载   ==   号
	bool operator==(Person& p) {
		if (this->m_Age == p.m_Age && this->m_Name == p.m_Name) {
			return true;
		}
		return false;
	}
 
	//重载   !=   号
	bool operator!=(Person& p) {
		if (this->m_Age == p.m_Age && this->m_Name == p.m_Name) {
			return false;
		}
		return true;
	}
 
};
 
 
 
 
 
void test01() {
	Person p1("Tom", 18);
 
	Person p2("Jerry", 18);
 
	if (p1 == p2) {
		cout << "p1和p2是相等的!" << endl;
	}
	else if (p1 != p2){
		cout << "p1和p2是不相等的!" << endl;
	}
}
 
 
 
 
 
 
 
 
 
 
 
int main() {
 
 
	test01();
 
	system("pause");
	return 0;
 
 
}

赋值运算符:= 重载

#include<iostream>
#include<string>
 
using namespace std;
 
//赋值运算符的重载
 
class Person {
public:
 
	Person(int age) {
		m_Age = new int(age);
	}
 
	~Person() {
		if (m_Age != NULL) {
			delete m_Age;
			m_Age = NULL;
		}
	}
	//堆区的内存重复释放 程序崩溃
	//解决方案 深拷贝解决浅拷贝问题
 
	//重载赋值运算符
	Person& operator=(Person& p) {
		//编译器提供浅拷贝
		//m_Age=p.m_Age;
 
		//应该先判断是否有属性在堆区,如果有先释放干净,然后再深拷贝
		if (m_Age != NULL) {
			delete m_Age;
			m_Age = NULL;
		}
 
		//深拷贝
		m_Age = new int(*p.m_Age);
 
		//返回对象本身
		return *this;
 
	}
 
	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();
 
	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;
 
 
}

函数调用运算符:() 重载

#include<iostream>
#include<string>
 
using namespace std;
 
//函数调用运算符  ()  重载
 
//打印输出类
class MyPrint
{
public:
	//重载函数调用运算符
	void operator()(string test) {
		cout << test << endl;
	}
 
};
 
void test01() {
 
	MyPrint myprint;
 
	myprint("Hello World");//由于使用起来非常像函数 因此称为仿函数
}
 
class MyAdd {
public:
	int operator()(int num1,int num2) {
		return num1 + num2;
	}
 
};
 
void test02() {
	MyAdd myadd;
	cout << "myadd的值是:" << myadd(202, 30) << endl;
 
	//匿名函数对象
	cout << MyAdd()(100, 100) << endl;//当前行执行完,立即释放
	
}
 
 
 
//伪函数非常灵活 没有固定的写法
 
int main() {
 
	test01();
	test02();
 
	system("pause");
	return 0;
 
 
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值