l8_6.cpp

来自「《C++程序设计教程》电子教案及例题源码」· C++ 代码 · 共 54 行

CPP
54
字号
#include "iostream.h"
class  CComplex 
{
private:
	double real;
	double imag;
public:
	CComplex(double r=0, double i=0);
	void Print();
	friend CComplex operator +(CComplex c1,CComplex c2);
	friend CComplex operator -(CComplex c1,CComplex c2);
};

CComplex::CComplex (double  r, double i)
{ 
	real = r;
	imag = i;
}

void CComplex::Print()
{
	cout << "(" << real << "," << imag << ")" << endl;
}

CComplex operator +(CComplex c1, CComplex c2)
{
	CComplex temp;
	temp.real = c1.real + c2.real;
	temp.imag = c1.imag + c2.imag;
	return temp;
}

CComplex operator -(CComplex c1, CComplex c2)
{
	CComplex temp;
	temp.real = c1.real - c2.real;
	temp.imag = c1.imag - c2.imag;
	return temp;
}

void main(void)
{
	CComplex  a(1, 2), b(3.0, 4.0), c, d, e; 
	c = a+b;
	d = b-10;
	e = 20+a;
	cout << "c = ";
	c.Print();
	cout << "d = ";
	d.Print();
	cout << "e = ";
	e.Print();
}

⌨️ 快捷键说明

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