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

📄 l8_2.cpp

📁 《C++程序设计教程》-杨国兴-电子教案及例题 C++程序设计PPT课件 html课件 例题
💻 CPP
字号:
#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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -