定义一个复数类,其属性为复数的实部和虚部
1个回答
展开全部
#include "string.h"
#include "iostream.h"
class complex
{ private:
float x,y;
public:
complex(float x, float y)
{ this->x=x; this->y=y;
cout<<"通过构造函数创建对象"<<x<<"+"<<y<<"i"<<endl;
}
void set(float x1,float y1) { x=x1; y=y1;}
void show( );
~complex( ) {cout<<"析构函数释放对象"<<x<<"+"<<y<<"i"<<endl;}
friend double add1(complex t1,complex t2);
friend double add2(complex t1,complex t2);
};
double add1(complex t1,complex t2)
{ return t1.x+t2.x; }
double add2(complex t1,complex t2)
{ return t1.y+t2.y; }
void complex::show( )
{cout<<"此复数是:"<<x<<"+"<<y<<"i"<<endl;}
void main( )
{
complex t1(1.0,2.0),t2(3.0,4.0);
cout<<"两个复数的和: "<<add1(t1,t2)<<"+"<<add2(t1,t2)<<"i"<<endl;
}
#include "iostream.h"
class complex
{ private:
float x,y;
public:
complex(float x, float y)
{ this->x=x; this->y=y;
cout<<"通过构造函数创建对象"<<x<<"+"<<y<<"i"<<endl;
}
void set(float x1,float y1) { x=x1; y=y1;}
void show( );
~complex( ) {cout<<"析构函数释放对象"<<x<<"+"<<y<<"i"<<endl;}
friend double add1(complex t1,complex t2);
friend double add2(complex t1,complex t2);
};
double add1(complex t1,complex t2)
{ return t1.x+t2.x; }
double add2(complex t1,complex t2)
{ return t1.y+t2.y; }
void complex::show( )
{cout<<"此复数是:"<<x<<"+"<<y<<"i"<<endl;}
void main( )
{
complex t1(1.0,2.0),t2(3.0,4.0);
cout<<"两个复数的和: "<<add1(t1,t2)<<"+"<<add2(t1,t2)<<"i"<<endl;
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询