l8_2.cpp

来自「《C++程序设计教程》-杨国兴-电子教案及例题 C++程序设计PPT课件 h」· C++ 代码 · 共 65 行

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

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

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

CComplex CComplex::operator +(CComplex c)
{
	CComplex temp;
	temp.real = real + c.real;
	temp.imag = imag + c.imag;
	return temp;
}

CComplex CComplex::operator -(CComplex c)
{
	CComplex temp;
	temp.real = real - c.real;
	temp.imag = imag - c.imag;
	return temp;
}

CComplex CComplex::operator *(CComplex c)
{
	CComplex temp;
	temp.real = real * c.real - imag * c.imag;
	temp.imag = real * c.imag + imag * c.real;
	return temp;
}
void main(void)
{
	CComplex  a(1, 2), b(3.0, 4.0), c, d, e, f;
	c = a+b;
	d = a-b;
	e = a*b;
	f = a+1;
	cout << "c = ";
	c.Print();
	cout << "d = ";
	d.Print();
	cout << "e = ";
	e.Print();
	cout << "f = ";
	f.Print();
}

⌨️ 快捷键说明

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