快!(C++高手帮忙)定义一个复数类Complex,重载运算符“-”,使之能用于复数的减法运算。

定义一个复数类Complex,重载运算符“-”,使之能用于复数的减法运算。(如题)
一定要最准确的。
这是减法,不是加法,请注意看。

#include<iostream>
using namespace std;
class complex //复数类声明
{
public: //外部接口
complex(double r=0.0,double i=0.0){real=r;imag=i;} //构造函数
complex operator - (complex c2); //-重载为成员函数
void display(); //输出复数
private: //私有数据成员
double real; //复数实部
double imag; //复数虚部
};
complex complex:: operator -(complex c2) //重载函数实现
{
complex c;
c.real=real-c2.real;
c.imag=imag-c2.imag;
return complex(c.real,c.imag);
}
void complex::display()
{ cout<<"("<<real<<","<<imag<<")"<<endl; }

int main() //主函数
{ complex c1(5,4),c2(2,10),c3; //声明复数类的对象
cout<<"c1="; c1.display();
cout<<"c2="; c2.display();
c3=c1-c2; //使用重载运算符完成复数减法
cout<<"c3=c1-c2=";
c3.display();

}
温馨提示:内容为网友见解,仅供参考
第1个回答  2013-06-29
如果不是作业的话,干嘛不直接用STL的复数类呢?~

#include <complex>,然后自己直接用就是了~

用C++语言设计一个虚数类,要求虚数类中重载运算符加减,主函数定义类...
代码如下:\/*1.复数(实部运算 +虚部运算) 1+2i 1-3i 加:2-i 减 0-5i1 重载实现复数一个输入和输出 普通写法实现输入 调用函数的形式实现输出2. 类重载实现复数的加法和减法 加法:类重载 减法:友元重载*\/#include<iostream>using namespace std;class A{private:int...

定义一个复数类complex。
Complex operator +(Complex &); \/\/重载运算符+ Complex operator -(Complex &); \/\/重载运算符- \/\/实现 Complex Complex::operator +( Complex &c){ Complex t;t.Real = this->Real + c.Real;t.Image = this->Image + c.Image;return t;} Complex Complex::operator -( Complex...

...设计一个完整的复数complex类,要求利用运算符重载实现复数的加法(+...
Complex operator+( const Complex& cc )const\/\/两个复数相加 { return Complex(real+cc.real,image+cc.image);} Complex operator-( const Complex& cc )const\/\/两个复数相减 { return Complex(real-cc.real,image-cc.image);} Complex operator*( const Complex& cc )const\/\/两个复数相加 {...

急!C++定义一个复数类Complex,使下面的代码能够工作?
include <iostream> using namespace std;class Complex { private:double re;double im;public:Complex(double re,double im=0){ this->re = re;this->im = im;} void add(const Complex& c){ this->re += c.re;this->im += c.im;} void sub(const Complex& c){ this->re -= ...

C++如何定义一个复数类,并支持四则运算
cout<<"输入两个复数,即a,b,c,d的值:";cin>>a>>b>>c>>d;complex n1(a,b),n2(c,d);cout<<"两个复数值为:";cout<<"A=";n1.display();cout<<" B=";n2.display();cout<<endl;cout<<endl<<endl<<"两个复数的加法:"<<endl;complex result1;result1=n1+n2;n1.display(...

(C++)定义一个复数类Complex,重载运算符“+”,使之能用于复数的加法运算...
friend ostream& operator << (ostream&,Complex&); \/\/声明重载运算符“<<” friend istream& operator >> (istream&,Complex&); \/\/声明重载运算符“>>”private: double real; double imag;};Complex operator +(Complex &c1,Complex &c2){ return Complex(c1.real+c2.real,c1.imag+c2.i...

c++编写一个复数类Complex
complex(complex &C){ real=C.real;imagi=C.imagi;countP++;} ~complex(){countP--;} void SetReal(double x){real=x;} void SetImagi(double y){imagi=y;} double GetReal(){return real;} double GetImagi(){return imagi;} complex operator + (complex C){ double x,y;x=real+...

C++编写复数计算类
Complex::~Complex(){ real = 0;imag = 0;} void Complex::InputData(){ cout<<"Please input Data:"<<endl;cin>>real>>imag;} Complex operator-(Complex & com1,Complex & com2)\/\/重载+ { Complex temp;temp.real = com1.real - com2.real;temp.imag = com1.imag - com2....

C++编程题 定义并实现一个复数类Complex,使得下面的代码能够工作_百度...
}void Complex::out_view(){ cout<<"该复数的辐角为:"<<atan(real\/imaginary)<<endl; \/\/修改后的 }\/\/void Complex::out_it(float a,float b) 这句也是一样,定义与声明不同 void Complex::out_it(){ cout<<"复数="<<real<<"+"<<imaginary<<"i"<<endl;} void main(){...

C++ 复数加减乘除
请LZ注意1楼的除法运算结果是错的。另外,重载 operator << 仅供参考,实际输出效果不太好。例子参考1楼,但可看出更人性化。include <iostream> using namespace std;class complex { public:complex(double real = 0.0, double image = 0.0) : real(real), image(image) {} complex operator...

相似回答