1个回答
展开全部
就是运算符的重载
#include <iostream.h>
//using namespace std;
class complex
{
private:
float real;
float image;
public:
complex(float r=0,float i=0)
{
real=r;
image=i;
}
/*
void display()
{
cout<<real<<"+"<<image<<"i"<<endl;
}
*/
complex operator +(complex &obj)
{
complex c;
c.real=real+obj.real;
c.image=image+obj.image;
return c;
}
complex operator -(complex &obj)
{
complex c;
c.real=real-+obj.real;
c.image=image-obj.image;
return c;
}
complex operator *(complex &obj)
{
complex c;
c.real=real*obj.real-image*obj.image;
c.image=image*obj.real+obj.image*real;
return c;
}
complex operator /(complex &obj)
{
complex c;
c.real=real*obj.real+image*obj.image;
c.image=image*obj.real-obj.image*real;
float A=obj.real*obj.real+obj.image*obj.image;
c.real=c.real/A;
c.image=c.image/A;
return c;
}
friend ostream & operator<<(ostream & ,complex &);
};
ostream & operator<<(ostream &out ,complex &obj)
{
out<<obj.real<<"+"<<obj.image<<"i"<<endl;
return out;
}
//3 ^ 2 ===>9
int main()
{
complex c1(3,4);
complex c2(c1);
//complex c3(-12,30);
cout<<c1<<endl; //.display();
cout<<c2<<endl; //.display();
complex c;
c=c1+c2;
cout<<c<<endl; //.display();
c=c1-c2;
cout<<c<<endl; //.display();c.display();
c=c1*c2;
cout<<c<<endl; //.display();c.display();
c=c1/c2;
cout<<c<<endl; //.display();c.display ();
return 0;
}
#include <iostream.h>
//using namespace std;
class complex
{
private:
float real;
float image;
public:
complex(float r=0,float i=0)
{
real=r;
image=i;
}
/*
void display()
{
cout<<real<<"+"<<image<<"i"<<endl;
}
*/
complex operator +(complex &obj)
{
complex c;
c.real=real+obj.real;
c.image=image+obj.image;
return c;
}
complex operator -(complex &obj)
{
complex c;
c.real=real-+obj.real;
c.image=image-obj.image;
return c;
}
complex operator *(complex &obj)
{
complex c;
c.real=real*obj.real-image*obj.image;
c.image=image*obj.real+obj.image*real;
return c;
}
complex operator /(complex &obj)
{
complex c;
c.real=real*obj.real+image*obj.image;
c.image=image*obj.real-obj.image*real;
float A=obj.real*obj.real+obj.image*obj.image;
c.real=c.real/A;
c.image=c.image/A;
return c;
}
friend ostream & operator<<(ostream & ,complex &);
};
ostream & operator<<(ostream &out ,complex &obj)
{
out<<obj.real<<"+"<<obj.image<<"i"<<endl;
return out;
}
//3 ^ 2 ===>9
int main()
{
complex c1(3,4);
complex c2(c1);
//complex c3(-12,30);
cout<<c1<<endl; //.display();
cout<<c2<<endl; //.display();
complex c;
c=c1+c2;
cout<<c<<endl; //.display();
c=c1-c2;
cout<<c<<endl; //.display();c.display();
c=c1*c2;
cout<<c<<endl; //.display();c.display();
c=c1/c2;
cout<<c<<endl; //.display();c.display ();
return 0;
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询