C++(五)——运算符重载

加号运算符重载

#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) {
	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;
	Person p2;
	p1.m_a = 11;
	p1.m_b = 12;
	p2.m_a = 13;
	p2.m_b = 14;
	Person p3 = p1 + p2;
	cout << "p3.m_a = " << p3.m_a << endl;
	cout << "p3.m_b = " << p3.m_b << endl;
	p3 = p3 + 100;
	cout << "p3.m_a = " << p3.m_a << endl;
	cout << "p3.m_b = " << p3.m_b << endl;

}

int main() {
	test01();
	return 0;
}



  

左移运算符重载

#include<iostream>
using namespace std;
//左移运算符重载
class Person {
	friend ostream& operator<< (ostream& cout, Person& p);//友元
public:
	Person() {
		m_a = 0;
		m_b = 0;
	}
private:
	int m_a;
	int m_b;
	//利用成员函数重载左移函数, 通常不会利用成员函数重载左移运算符
	/*void operator<<(Person& p) {


	}*/

};

ostream & operator<<(ostream &cout, Person &p) {
	cout << "m_a = " << p.m_a << " m_b = " << p.m_b;
	return cout;
}

void test01() {
	Person p;
	cout << p << endl;
}

int main() {
	test01();
	return 0;
}

递增运算符重载

#include<iostream>
using namespace std;
class MyInteger {
	friend ostream& operator<<(ostream& out, MyInteger myint);
public:
	MyInteger() {
		m_num = 0;
	}
	//重载前置++运算符 返回&是为了一直对一个数据进行操作
	MyInteger& operator++() {
		m_num++;
		//将自身作为返回
		return *this;
	}
	//重置后置++运算符 int代表占位参数 用于区分前置和后置递增
	//不反回& 因为局部变量temp会被释放
	MyInteger operator++(int) {
		//先记录当时结果
		MyInteger temp = *this;
		//后递增
		m_num++;
		//最后将记录的结果进行返回,因为这是后置++
		return temp;
	}

private:
	int m_num;
};
//重载左移运算符
ostream& operator<<(ostream& out, MyInteger myint) {
	out << myint.m_num;
	return out;
}



void test01() {
	MyInteger myint;
	cout << ++myint;

}

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

int main() {
	//test01();
	test02();
	return 0;
}

赋值运算符重载

#include<iostream>
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 << "p2的年龄:" << *p3.m_age << endl;
}
int main() {

	test01();
	return 0;
}

关系运算符重载

#include<iostream>
#include<cstring>
using namespace std;
class Person {
public:
	Person(string name, int age) {
		m_name = name;
		m_age = age;

	}
	//重载关系运算符
	int operator==(Person& p) {
		if (this->m_age == p.m_age && this->m_name == p.m_name)
			return 1;
		else
			return 0;

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

	}
	string m_name;
	int m_age;
};
void test01() {
	Person p1("tom", 18);
	Person p2("tom", 17);
	if (p1 == p2)
		cout << "p1和p2是相等的" << endl;
	else
		cout << "p1和p2是不相等的" << endl;

}

void test02() {
	Person p1("tom", 18);
	Person p2("tom", 18);
	if (p1 != p2)
		cout << "p1和p2是不相等的" << endl;
	else
		cout << "p1和p2是相等的" << endl;

}


int main() {

	test01();
	test02();
	return 0;
}

函数调用运算符重载

#include<iostream>
#include<cstring>
using namespace std;
//函数调用运算符重载
//打印输出类
class Myprint {
public:
	//重载函数调用运算符
	void operator() (string test) {
		cout << test << endl;
	}


};

void test01() {

	Myprint myprint;
	//由于使用起来非常像函数调用,所以也叫仿函数
	myprint("hello motherfucker");

}
//仿函数非常灵活,没有固定的写法
class Add {
public:
	int operator()(int num1, int num2) {
		return num1 + num2;
	}


};

void test02() {
	Add myadd;
	int res = myadd(100, 111);
	cout << "sum = " << res << endl;
	//匿名函数对象
	cout << Add()(100, 100) << endl;


}

int main() {


	//test01();
	test02();
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值