类的实现,用"="重载?求助!!!

sharkzwn 2007-03-22 05:59:19
复数类:
1.按照a+bi的形式显示一个复数
2.有复数z,复数y,能实现复数z+y相加.(要求复数加法可以实现连加)
前两问好办!
3,实现complex z=a+bi, double x=z;(x=a+b)
这一问不会!
...全文
194 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
Aresky 2007-03-22
  • 打赏
  • 举报
回复
对,编译器自己会构造拷贝构造函数
xlbdan 2007-03-22
  • 打赏
  • 举报
回复
嗯是的,本例中不用拷贝构造函数也可以,因为数据成员没有指针,
我写出来是为了更清楚的表达.
chenyu2202863 2007-03-22
  • 打赏
  • 举报
回复
很多入门书上都有
sharkzwn 2007-03-22
  • 打赏
  • 举报
回复
Complex c1(3,4); //产生了3+4i ,即a+bi
Complex z=c1; //调用拷贝构造函数.完成z=a+bi, z=3+4i

就是没有拷贝构造函数好像也可以阿??
Aresky 2007-03-22
  • 打赏
  • 举报
回复
拷贝构造函数也需要 
Complex x = y;//调用拷贝构造函数


Complex x;
x = y;//调用=运算符
Aresky 2007-03-22
  • 打赏
  • 举报
回复
拷贝构造函数也需要 
Complex x = y;//调用拷贝构造函数


Complex x;
x = y;//调用=运算符
Aresky 2007-03-22
  • 打赏
  • 举报
回复
//重载=运算符
Complex &operator= (const Complex &cl)
{
m_ImagePart=cl.m_ImagePart;
m_RealPart=cl.m_RealPart;

return *this;
}
xlbdan 2007-03-22
  • 打赏
  • 举报
回复
定义构造函数
Complex(double RealPart,double ImagePart)
{
m_ImagePart=ImagePart;
m_RealPart=RealPart;
}

加入一个拷贝构造函数
Complex(const Complex& cl)
{
m_ImagePart=cl.m_ImagePart;
m_RealPart=cl.m_RealPart;
}

Complex c1(3,4); //产生了3+4i ,即a+bi
Complex z=c1; //调用拷贝构造函数.完成z=a+bi, z=3+4i

再定义一个转换函数
operator double() //完成第三问后半部分,double x=z, x=a+b,即3+4=7
{
return m_ImagePart+m_RealPart;
}
sharkzwn 2007-03-22
  • 打赏
  • 举报
回复
帖这么多原码并没有实现第三问的问题啊
jixingzhong 2007-03-22
  • 打赏
  • 举报
回复
#include <iostream.h>


class Complex{
private:
double m_ImagePart;
double m_RealPart;
public:
Complex(double real=0,double image=0)
{
m_RealPart=real;
m_ImagePart=image;
}
virtual ~Complex();

friend Complex operator+(Complex n1,Complex n2);
Complex operator+=(Complex& n);
friend Complex operator-(Complex& n1,Complex& n2);
Complex operator-=(Complex& n);
friend bool operator ==(Complex& n1,Complex& n2);
Complex operator*=(Complex& n);
Complex operator=(Complex& n);
friend Complex operator*(Complex& n1,Complex& n2);
friend Complex operator/(Complex& n1,Complex& n2);
Complex operator/=(Complex& n);

void Print()
{
if(m_ImagePart>=0)
cout<<"\nData: "<<m_RealPart<<"+"<<m_ImagePart<<"i\n";
else
cout<<"\nData: "<<m_RealPart<<"+("<<m_ImagePart<<")i\n";
}

bool SetValue(double real,double image)
{
m_RealPart=real;
m_ImagePart=image;

return true;
}

bool GetValue(double* real,double* image)
{
if(!real || !image)return false;

*real=m_RealPart;
*image=m_ImagePart;

return true;
}
};

Complex operator+(Complex n1,Complex n2)
{
double r1,i1,r2,i2;
n1.GetValue(&r1,&i1);
n2.GetValue(&r2,&i2);

return Complex(r1+r2,i1+i2);
}

Complex Complex::operator+=(Complex& n)
{
double real,image;
n.GetValue(&real,&image);
real+=m_RealPart;
image+=m_ImagePart;

m_RealPart=real;
m_ImagePart=image;

return *this;
}

Complex Complex::operator-=(Complex& n)
{
double real,image;
n.GetValue(&real,&image);
real-=m_RealPart;
image-=m_ImagePart;

SetValue(real,image);

return *this;
}

Complex Complex::operator*=(Complex& n)
{
double real,image;
n.GetValue(&real,&image);
real*=m_RealPart;
image*=m_ImagePart;

SetValue(real,image);

return *this;
}

Complex operator-(Complex& n1,Complex& n2)
{
double r1,i1,r2,i2;
n1.GetValue(&r1,&i1);
n2.GetValue(&r2,&i2);

return Complex(r1-r2,i1-i2);
}

bool operator==(Complex& n1,Complex& n2)
{
double r1,i1,r2,i2;
n1.GetValue(&r1,&i1);
n2.GetValue(&r2,&i2);

return (r1==r2&&i2==i1)?true:false;
}

Complex Complex::operator =(Complex& n)
{
double r,i;
n.GetValue(&r,&i);

SetValue(r,i);

return *this;
}

Complex operator *(Complex& n1,Complex& n2)
{
double r1,i1,r2,i2;

n1.GetValue(&r1,&i1);
n2.GetValue(&r2,&i2);

return Complex(r1*r2,i1*i2);
}

Complex operator/(Complex& n1,Complex& n2)
{
double r1,r2,i1,i2;
n1.GetValue(&r1,&i1);
n2.GetValue(&r2,&i2);

return Complex(r1/r2,i1/i2);
}

Complex Complex::operator /=(Complex& n)
{
double real,image;
n.GetValue(&real,&image);

SetValue(m_RealPart/real,m_ImagePart/image);

return *this;
}

Complex::~Complex()
{
m_RealPart=m_ImagePart=0;
}
healer_kx 2007-03-22
  • 打赏
  • 举报
回复
显示是个小问题吧?

65,210

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧