其他分享
首页 > 其他分享> > 运算符重载

运算符重载

作者:互联网

代码-A

/*
#include<iostream>
using namespace std;
class Zhu {
public:Zhu() { a = 0, b = 0; }
	   Zhu(double c, double d) { a = c; b = d; }
	   Zhu operator + (Zhu &u);//重载运算符+
	   void display();
private:double a;
		double b;
};
Zhu Zhu::operator + (Zhu &u)
{
	Zhu q;
	q.a = a + u.a;
	q.b = b + u.b;
	return q;
}
void Zhu::display()
{
	cout << "(" << a << ","  << b << "i)" << endl;
}
int main()
{  
	Zhu first(2,-8), second(2.5, 4), third;
	third = first + second;
	cout << "First=";
	first.display();
	cout << "Second=";
	second.display();
	cout << "First+Second=";
	third.display();
	cin.get();
	return 0;
}
*/


#include<iostream>
using namespace std;
class Zhu {
public:Zhu() { a = 0, b = 0; }
	   Zhu(double c, double d) { a = c; b = d; }
	   Zhu operator ++();
	   void display();
private:double a;
		double b;
};
Zhu Zhu::operator++()
{
	Zhu i;
	i.a = a++;
	i.b = b++;
	return i;	
}
void Zhu::display()
{
	cout << "(" << a << "," << b << "i)" << endl;
}
int main()
{
	Zhu first(2, -8), second(2.5, 4), third;
	++third;
	cout << "First=";
	first.display();
	cout << "Second=";
	second.display();
	cout << "First+Second=";
	third.display();
	cin.get();
	return 0;
}

End

~
记录留存


嗨~辰 发布了24 篇原创文章 · 获赞 0 · 访问量 338 私信 关注

标签:Zhu,++,double,void,运算符,operator,重载,display
来源: https://blog.csdn.net/weixin_44228006/article/details/104095373