64,683
社区成员
发帖
与我相关
我的任务
分享
#if ! defined(COMPLEX_H_)
#define COMPLEX_H_
class complex
{
///Attributes:
private:
double real;
double image;
double radius;
double angle;
///Operations:
public:
complex(double real = 0, double image = 0);
complex(const complex &c);
~complex(){};
void setvalue(double real, double image);
double getreal(){return real;}
double getimage(){return image;}
double getradius(){return radius;}
double getangle(){return angle;}
complex operator +(complex comp);
complex operator *(complex comp);
complex operator !();
operator double();
};
#endif
#include "complex.h"
#include <iostream>
#include <cmath>
using std::cout;
using std::endl;
complex::complex(double real, double image)
{
cout << "Construct Function!" << endl;
setvalue(real, image);
}
complex::complex(const complex &c)
{
cout << "Copy Construct Function!" << endl;
setvalue(c.real, c.image);
}
void complex::setvalue(double real, double image)
{
this->real = real;
this->image = image;
this->radius = sqrt(real * real + image * image);
this->angle = atan2(image, real);
}
complex complex::operator +(complex comp)
{
cout << "+ override function" << endl;
return complex(this->real + comp.real, this->image + comp.image);
}
complex complex::operator *(complex comp)
{
///(a+bi)(c+di) = ac + (ad+bc)i - bd = ac - bd + (ad+bc)i
return complex(this->real * comp.real - this->image * comp.image,
this->real * comp.image + this->image * comp.real);
}
///重载!求复数的共轭复数
complex complex::operator !()
{
return complex(this->real, -this->image);
}
///重载double()求复数的模
complex::operator double()
{
return fabs(radius);
}
#include <iostream>
#include "complex.h"
using std::cin;
using std::cout;
using std::endl;
int main(void )
{
double real1,image1,real2,image2;
cout << "Please the first complex:" << endl;
cin >> real1 >> image1;
cout << "Please the second complex:" << endl;
cin >> real2 >> image2;
complex comp1(real1, image1);
complex comp2(real2, image2);
cout << (comp1+comp2).getreal() << endl;
return 0;
}
Please the first complex:
1.1 2.2
Please the second complex:
3.3 4.4
Construct Function!
Construct Function!
Copy Construct Function!
+ override function
Construct Function!
4.4
#include <iostream>
#include <cmath>
using std::cin;
using std::cout;
using std::endl;
class complex
{
///Attributes:
private:
double real;
double image;
double radius;
double angle;
///Operations:
public:
complex(double real = 0, double image = 0);
complex(complex &c);
~complex(){};
void setvalue(double real, double image);
double getreal(){return real;}
double getimage(){return image;}
double getradius(){return radius;}
double getangle(){return angle;}
complex operator +(complex &comp);
complex operator *(complex &comp);
complex operator !();
operator double();
};
complex::complex(double real, double image)
{
cout << "Construct Function!" << endl;
setvalue(real, image);
}
complex::complex(complex &c)
{
cout << "Copy Construct Function!" << endl;
setvalue(c.real, c.image);
}
void complex::setvalue(double real, double image)
{
this->real = real;
this->image = image;
this->radius = sqrt(real * real + image * image);
this->angle = atan2(image, real);
}
complex complex::operator +(complex &comp)
{
cout << "+ override function" << endl;
return complex(this->real + comp.real, this->image + comp.image);
}
complex complex::operator *(complex &comp)
{
///(a+bi)(c+di) = ac + (ad+bc)i - bd = ac - bd + (ad+bc)i
return complex(this->real * comp.real - this->image * comp.image,
this->real * comp.image + this->image * comp.real);
}
///重载!求复数的共轭复数
complex complex::operator !()
{
return complex(this->real, -this->image);
}
///重载double()求复数的模
complex::operator double()
{
return fabs(radius);
}
int main(void )
{
double real1,image1,real2,image2;
cout << "Please the first complex:" << endl;
cin >> real1 >> image1;
cout << "Please the second complex:" << endl;
cin >> real2 >> image2;
complex comp1(real1, image1);
complex comp2(real2, image2);
cout << (comp1+comp2).getreal() << endl;
return 0;
}
这代码可以在你说的那网站上通过,只是没法输出。我在vs2012上也能通过。。我做这个程序的时候,忘记了加const,所以出错了,但是加上就对,而且我在copy构造函数里面也没有更改传递过来参数的值,但是为啥加const就对了呢,这正是我纠结之处,希望前辈能细心研究一下,告之结果,鄙人调试水平很低,不太明白。complex operator +(complex comp);
这传的是复制值,自然在传参数的时候调用了拷贝构造 complex operator +(complex comp);
改成:
complex operator +(complex& comp);