complextype.cpp

来自「data+structures+using+c的源码」· C++ 代码 · 共 77 行

CPP
77
字号

//Implementation file complexType.cpp

#include <iostream>
using namespace std;

#include "complexType.h"

ostream& operator<<(ostream& osObject, const complexType& complex)
{
	osObject<<"(";						//Step a
	osObject<<complex.realPart;			//Step b
	osObject<<", ";						//Step c
	osObject<<complex.imaginaryPart;	//Step d
	osObject<<")";						//Step e

	return osObject;			
}

istream& operator>> (istream& isObject, complexType& complex)
{
	char ch;

	isObject>>ch;                     //Step a
	isObject>>complex.realPart;       //Step b
	isObject>>ch;                     //Step c
	isObject>>complex.imaginaryPart;  //Step d
	isObject>>ch;                     //Step e

	return isObject;			
}


bool complexType::operator==(const complexType& otherComplex) const
{
	return(realPart == otherComplex.realPart && 
	      imaginaryPart == otherComplex.imaginaryPart);
}

void complexType::setComplex(const double& real, const double& imag)
{
	realPart = real;
	imaginaryPart = imag;
}

	//constructor 
complexType::complexType(double real, double imag)
{
     setComplex(real, imag);
}

	//Overload the operator +	
complexType complexType::operator+
 	                (const complexType& otherComplex) const
{
	complexType temp;

	temp.realPart = realPart + otherComplex.realPart;
	temp.imaginaryPart = imaginaryPart 
	                     + otherComplex.imaginaryPart;

	return temp;
}

	//Overload the operator *
complexType complexType::operator*
 				 (const complexType& otherComplex) const
{
   complexType temp;

   temp.realPart = (realPart * otherComplex.realPart) -
                   (imaginaryPart * otherComplex.imaginaryPart);
   temp.imaginaryPart = (realPart * otherComplex.imaginaryPart)  
	                  + (imaginaryPart * otherComplex.realPart);
   return temp;
}

⌨️ 快捷键说明

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