ex020105.cpp

来自「深入浅出Visual C++入门进阶与应用实例 随书光盘 作者 何志丹」· C++ 代码 · 共 46 行

CPP
46
字号
// Ex020105.cpp : Defines the entry point for the application.
//

#include "stdafx.h"

class CComplex//得数
{
public:
    CComplex() { m_dReal=m_dImag=0; }
    CComplex(double r, double i)
    {
        m_dReal = r ;
		m_dImag = i;
    }
    CComplex operator +(const CComplex &c)//重载运算符加号
	{
		return CComplex(m_dReal + c.m_dReal , m_dImag + c.m_dImag);
	};
   
private:
    double m_dReal ;//实部
	double m_dImag ;//虚部

	friend CComplex operator-(const CComplex &c1,const CComplex &c2) ;
};

CComplex operator-(const CComplex &c1,const CComplex &c2)//重载运算符减号
{
	return CComplex(c1.m_dReal - c2.m_dReal , c1.m_dImag - c2.m_dImag);
}

int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
 	CComplex c1(5,7),c2(9,4);
	CComplex result = c1.operator+(c2);//简略形式c1 + c2 ;
	result = ::operator-(c1,c2);//c1 - c2 ;

	return 0;
}



⌨️ 快捷键说明

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