C++学习(十一)—运算符重载(一)

加号运算符重载

对于内置数据类型,编译器知道如何进行运算
但是对于自定义数据类型,编译器并不知道该如何运算
利用运算符重载可以解决这个问题
注意:运算符重载也可以发生函数重载

#include<iostream>

using namespace std;

//	加号运算符重载

class Person
{
public:
	Person() {};
	Person(int a, int b) :m_a(a), m_b(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;
	}

	int m_a;
	int m_b;
};

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 a)
{
	Person temp;
	temp.m_a = p1.m_a + a;
	temp.m_b = p1.m_b + a;
	return temp;
}

void test01()
{
	Person p1(10, 10);
	Person p2(20, 20);

	Person p3 = p1 + p2;

	cout << "p3.m_a = " << p3.m_a << endl;
	cout << "p3.m_b = " << p3.m_b << endl;

	//  本质:
	//  成员函数本质:
	//	Person p3 = p1.operator+(p2);
	//	全局函数本质
	//	Person p3 = operator+(p1, p2);
	
	Person p4 = p1 + 10;
	cout << "p4.m_a = " << p4.m_a << endl;
	cout << "p4.m_b = " << p4.m_b << endl;
}



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

左移运算符重载

对于内置数据类型,编译器知道如何运用cout进行<<运算符输出
对于自定义数据类型,无法输出

#include<iostream>

using namespace std;

//	左移运算符重载

class Person
{
	friend ostream & operator<<(ostream &cout, Person &p);
public:
	Person() {};
	Person(int a, int b)
	{
		this->m_a = a;
		this->m_b = b;
	}
	
	//	利用成员函数  实现运算符重载

	/*  void operator<<(ostream &cout)
	{

	}*/
private:
	int m_a;
	int m_b;
};

//	利用全局函数  实现<<运算符重载

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


void test01()
{
	Person p1(10, 10);

	//cout << "p1.m_a = " << p1.m_a << " p1.m_b = " << p1.m_b << endl;

	//cout << p1 << endl;
	//operator<<(cout, p1);
	cout << p1 << endl;
}

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

递增运算符重载

#include<iostream>

using namespace std;

//  递增递减运算重载 ++ --

class MyInt
{
public:
	MyInt()
	{
		m_Num = 0;
	}
	MyInt& operator++()
	{
		m_Num++;
		return *this;
	}
	MyInt operator++(int)
	{
		MyInt temp = *this;
		this->m_Num++;
		return temp;
	}
	int m_Num;
protected:
private:
};

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

/*void operator++()  //前置++  先++ 后返回  
{

}*/

/*void operator++(int)  //后置++  先返回 后++
{

}*/
void test01()
{
	MyInt myint;
	cout << myint << endl;

	

	cout << ++myint << endl;	//	1

	//cout << myint++ << endl;	//  1

	cout << myint << endl;	//	2
}

void test02()
{
	MyInt myint;
	cout << myint << endl;

	 cout << myint++ << endl;
	//	operator<<(cout, myint++);

	cout << myint << endl;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

walkerrev_ll

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值