运算符重载

//赋值运算符重载
#include<iostream>
using namespace std;
class complex
{
private:
	int real;
	int imag;
public:
	complex(int r=0, int i=0)//构造函数
	{
		real = r;
		imag = i;
	}
	void Print()//输出函数
	{
		cout << "real=" << real << " " << "imag=" << imag;
	}

	complex& operator=(const complex& rhs);//赋值运算符重载(声明)
};
complex& complex:: operator=(const complex& rhs)//类外定义
{
	this->real = rhs.real;
	this->imag = rhs.imag;
	return *this;
}

int main()
{
	complex a(1,2),b(3,4),c;
	a = b;
	a.Print();
	puts("");
	b.operator=(c);
	c.Print();


}

全部评论

相关推荐

05-20 21:57
已编辑
门头沟学院 Java
喜欢吃卤蛋的悲伤蛙在提需求:建信融通没消息吧,我2说有实习挂简历不理了
点赞 评论 收藏
分享
明天不下雨了_人机版:让我们大声的说出来:以前的未来就是现在
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务