运算符重载

文章展示了C++中几种常见的运算符重载方法,包括加号运算符、左移运算符、递增运算符、赋值运算符和关系运算符的重载。通过实例详细解释了如何在类内部和作为全局函数实现这些运算符,以及如何使用友元函数来支持运算符重载。此外,还演示了函数调用运算符的重载,即仿函数的使用。
摘要由CSDN通过智能技术生成

几种常见的运算符重载

1、加号运算符重载

#include <iostream>
using namespace std;

class Person
{
public:
	Person() {};
	Person(int a, int b)
	{
		this->m_A = a;
		this->m_B = b;
	}
	//成员函数实现+号运算符重载
	Person operator+(const Person& p)
	{
		Person tmp;
		tmp.m_A = this->m_A + p.m_A;
		tmp.m_B = this->m_B + p.m_B;
		return tmp;
	}
	Person operator+(const int val)
	{
		Person tmp;
		tmp.m_A = this->m_A + val;
		tmp.m_B = this->m_B + val;
		return tmp;
	}

public:
	int m_A;
	int m_B;
};

/*
//全局函数实现+号运算符重载
Person operator+(const Person& p1, const Person& p2)
{
	Person tmp;
	tmp.m_A = p1.m_A + p2.m_A;
	tmp.m_B = p1.m_B + p2.m_B;
	return tmp;
}
Person operator+ (const Person & p1, const int x)
{
	Person tmp;
	tmp.m_A = p1.m_A + x;
	tmp.m_B = p1.m_B + x;
	return tmp;
}
*/

void test()
{
	Person p1(10, 10);
	Person p2(20, 20);
	Person p3 = p1 + p2;
	cout << "p3.m_A = " << p3.m_A << "  p3.m_B = " << p3.m_B << endl;

	Person p4 = p1 + 30;
	cout << "p4.m_A = " << p4.m_A << "  p4.m_B = " << p4.m_B << endl;
}

int main()
{
	test();
	system("pause");
	return 0;
}

2、左移运算符重载

#include <iostream>
using namespace std;

class Person
{
	friend ostream& operator<< (ostream& out, const Person& p);
public:
	Person(int a, int b)
	{
		this->m_A = a;
		this->m_B = b;
	}
private:
	int m_A;
	int m_B;
};

ostream& operator<<(ostream& out, const Person& p)
{
	out << "m_A = " << p.m_A << "  m_B = " << p.m_B << endl;
	return out;
}

void test()
{
	Person p1(10, 20);
	cout << p1 << "hello world" << endl;
}

int main()
{
	test();
	system("pause");
	return 0;
}

3、递增运算符重载

#include <iostream>
using namespace std;

class MyInteger
{
	friend ostream& operator<<(ostream& out, const MyInteger& myint);
public:
	MyInteger()
	{
		m_Num = 0;
	}
	MyInteger& operator++()
	{
		this->m_Num++;
		return *this;
	}
	MyInteger operator++(int)
	{
		MyInteger temp;
		temp = *this;
		this->m_Num++;
		return temp;
	}
private:
	int m_Num;
};

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

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、赋值运算符重载

#include <iostream>
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 = 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(10);
	Person p2(20);
	Person p3(30);
	p3 = p2 = p1;
	cout << "*p1.m_Age = " << *p1.m_Age << endl;
	cout << "*p2.m_Age = " << *p2.m_Age << endl;
	cout << "*p3.m_Age = " << *p3.m_Age << endl;
}

int main()
{
	test01();
	system("pause");
	return 0;
}

5、关系运算符重载

#include <iostream>
using namespace std;

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()
{
	Person a("孙悟空", 10);
	Person b("孙悟空", 20);
	if (a == b){
		cout << "a和b相等" << endl;
	}
	else {
		cout << "a和b不相等" << endl;
	}
	if (a != b) {
		cout << "a和b不相等" << endl;
	}
	else {
		cout << "a和b相等" << endl;
	}	
}

int main()
{
	test01();
	system("pause");
	return 0;
}

6、函数调用运算符重载

#include <iostream>
using namespace std;

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

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

class MyAdd
{
public:
	int operator()(int val1, int val2)
	{
		return val1 + val2;
	}
};

void test02()
{
	MyAdd add;
	int ret = add(10, 10);
	cout << "ret = " << ret << endl;
	//匿名对象调用,通过类型加小括号创建一个匿名对象,当前行执行完后立即被释放
	cout << "MyAdd()(100, 100) = " << MyAdd()(100, 100) << endl;
}

int main()
{
	test01();
	test02();
	system("pause");
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值