定义一个名为complex的复数类,其属性数据为复数的实部和虚部,要求构造函数和拷贝构造函数使用,并定义成员函数打印复数的值

来源:学生作业帮助网 编辑:作业帮 时间:2024/05/01 06:19:28
定义一个名为complex的复数类,其属性数据为复数的实部和虚部,要求构造函数和拷贝构造函数使用,并定义成员函数打印复数的值

定义一个名为complex的复数类,其属性数据为复数的实部和虚部,要求构造函数和拷贝构造函数使用,并定义成员函数打印复数的值
定义一个名为complex的复数类,其属性数据为复数的实部和虚部,
要求构造函数和拷贝构造函数使用,并定义成员函数打印复数的值

定义一个名为complex的复数类,其属性数据为复数的实部和虚部,要求构造函数和拷贝构造函数使用,并定义成员函数打印复数的值
//Complex.h
class Complex
{
private:
float Real;
float Imag;
public:
Complex();
Complex(float Rl,float Im);
Complex operator +(Complex &c);
Complex operator -(Complex &c);
Complex operator *(Complex &c);
Complex operator /(Complex &c);
void GetValue();
};
//Complex.cpp
#include
#include "Complex.h"
void main()
{
Complex c1(1,1);
Complex c2(1,-1);
Complex c3;
c3 = c1+c2;
c3.GetValue();
c3 = c1-c2;
c3.GetValue();
c3 = c1*c2;
c3.GetValue();
c3 = c1/c2;
c3.GetValue();
return;
}
////////////////////////////////////////////////////////
Complex::Complex()
{
Real = 0;
Imag = 0;
}
/////////////////////////////////////////////////////////
Complex::Complex(float Rl,float Im)
{
Real = Rl;
Imag = Im;
}
////////////////////////////////////////////////////////
inline Complex Complex::operator +(Complex &c)
{
Complex x;
x.Real = Real+c.Real;
x.Imag = Imag+c.Imag;
return x;
}
///////////////////////////////////////////////////////
inline Complex Complex::operator -(Complex &c)
{
Complex x;
x.Real = Real-c.Real;
x.Imag = Imag-c.Imag;
return x;
}
///////////////////////////////////////////////////////
inline Complex Complex::operator *(Complex &c)
{
Complex x;
x.Real = Real*c.Real-Imag*c.Imag;
x.Imag = Imag*c.Real+c.Imag*Real;
return x;
}
///////////////////////////////////////////////////////
inline Complex Complex::operator /(Complex &c)
{
/*(a+bi)/(c+di)
=(a+bi)*(c-di)/(c+di)*(c-di)
=(ac+adi+bci+bd)/(c*c+d*d)
*/
Complex x;
x.Real = (Real*c.Real-Imag*(-c.Imag))/(c.Real*c.Real+c.Imag*c.Imag);
x.Imag = (Real*(-c.Imag)+Imag*c.Real)/(c.Real*c.Real+c.Imag*c.Imag);
return x;
}
///////////////////////////////////////////////////////
void Complex::GetValue()
{
cout