c++添加成员函数,求当前对象与另一对象的和。main函数定义两个复数对象进行求和,把和结果显示出来
#include"string.h"#include"iostream.h"classcomplex{private:floatx,y;public:complex(fl...
#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;}
};
void complex::show( )
{cout<<"此复数是:"<<x<<"+"<<y<<"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;}
};
void complex::show( )
{cout<<"此复数是:"<<x<<"+"<<y<<"i"<<endl;} 展开
2012-12-10 · 知道合伙人软件行家
关注
展开全部
#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 add(complex x1);
~complex( ) {cout<<"析构函数释放对象"<<x<<"+"<<y<<"i"<<endl;}
};
void complex::show( )
{
cout<<"此复数是:"<<x<<"+"<<y<<"i"<<endl;
}
complex complex::add(complex x1)
{
x+= x1.x;
y+= x1.y;
return *this;
}
void main()
{
complex x(3, 4), y(12, 5);
(x.add(y) ).show();
}
#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 add(complex x1);
~complex( ) {cout<<"析构函数释放对象"<<x<<"+"<<y<<"i"<<endl;}
};
void complex::show( )
{
cout<<"此复数是:"<<x<<"+"<<y<<"i"<<endl;
}
complex complex::add(complex x1)
{
x+= x1.x;
y+= x1.y;
return *this;
}
void main()
{
complex x(3, 4), y(12, 5);
(x.add(y) ).show();
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询