⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 c15_10.cpp

📁 it can help you use C++ program we
💻 CPP
字号:
//用成员函数重载运算符"+"
class CComplex
{
public:
	CComplex(){ real = 0.0; image = 0.0; }
	CComplex(double rv) { real = rv; image = 0.0; }
	CComplex(double rv,double iv) { real = rv; image =iv;}
	CComplex  operator + (const CComplex&);		//重载的加运算符
	~CComplex() {};
private:
	double	real;		//复数的实部
	double	image;		//复数的虚部
};

CComplex  CComplex::operator +(const CComplex& c)
{
	CComplex	temp;
	temp.real = real + c.real;
	temp.image = image + c.image;
	return temp;
}

int main( )
{
	CComplex c1(1,5),c2(3);
	c1 = c2+16;		//正确:被编译器解释为c1=c2.operator+(CComplex(16))
	c1 = 16+c2;		//错误:被解释为:c1 = 16.operator+(c2); 

	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -