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

📄 l8_6.cpp

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