题目
(13 )有如下复数类的声明,请补充完整。
class complex{
double real; // 实部
double imag; // 虚部
public:
complex(double x , double y){
real=x;
imag=y;
}
perator+(complex c){// 重载加法运算符 “ + ”
return complex(___________)
}
};
第1题
(13 )补充完整下面的类定义:
const double PI=3 .14;
class Circle{ // 圆形物体的抽象基类
protected:
double r; // 半径
public:
Circle (double radius=0 ) : r (radius ) {}
【 13 】 ; // 计算圆形物体表面积的纯虚函数声明
};
class Cylinder:public Circle { // 圆柱体类
double h; // 高度
public:
Cylindr (double radius=0, doubli height=0 ) :
Circle (radius ) , h (height ) {}
Virtual double Area () { // 计算圆柱体的表面积
return 2*PI*r* (r+h ) ;
}
};
第3题
有如下类定义,请将Sample类的拷贝构造函数补充完整。
class Sample{
public:
Sample(){)
~Sample(){if(p)delete p;)
Sample(const Sample& s){
______
}
void SetData(int data) {p=new int(data);}
private:
int*p;
};
第4题
有如下类定义,请将Sample类的复制构造函数补充完整。
class Sample{
public:
Sample(){ }
~Sample(){if(P)delete P;)
Sample(const Sample&s){______}
void SetData(int datA) {P=new int(datA) ;}
private:
int*P:
};
第5题
(1)定义类Coordinate的保护数据成员x和y,它们都是int型的数据,代表二维坐标的横纵坐标值。请在注释“//**1 **”之后添加适当的语句。
(2)根据类Coordinate定义后的成员函数Display的实现,补充该函数在类Coordinate定义体内的声明,Display为二维坐标类的虚函数。请在注释“//**2**。”之后添加适当的语句。
(3)完成二维坐标类Coordinate的构造函数,将参数a和b分别赋值给数据成员x和y。请在注释“//**3**”之后添加适当的语句。
(4)根据ThreeDCoord类构造函数的声明,补充ThreeDCoord构造函数的实现,参数a和b通过调用基类的构造函数来初始化基类的数据成员x和y,c赋值给数据成员2。请在注释“//**4**”之后添加适当的语句。
输出结果如下;
[1,2]
[3,4,5]
注意:除在指定位置添加语句之外,请不要改动程序中的其他内容。
源程序文件tese9_3.cpp清单如下:
include<iostream.h>
class Coordinate
{protected:
//** 1 **
public:
Coordinate(int a=0, int b=0);
//** 2 **
};
Coordinate::Coordinate(int a, int b)
{
//** 3 **
}
void Coordinate::Display() const
{
cout << '[' << x << ", "<< y << ']' << end1;
}
class ThreeDCoord:public Coordinate
{
int z;
public:
ThreeDCoord(int a=0, int b=0, int c=0);
virtual void Display() const;
};
//** 4 **
void ThreeDCoord::Display() const
{
cout << '[' << x << ", "<< y << ", "<< z << ']' << end1;
}
void main ()
{
Coordinate c(1, 2);
ThreeDCoord t(3, 4, 5);
c.Display ();
t.Display ();
}
第6题
阅读以下说明和Java 码,将应填入(n)处的字名写在的对应栏内。
[说明] 编写一个完整的JavaApplet 程序使用复数类Complex 验证两个复数1+2i 和3+4i 相加产生一个新的复数4+6i。
复数类Complex 必须满足如下要求:
(1) 复数类Complex 的属性有:
RealPart: int 型,代表复数的实数部分
ImaginPart: int 型,代表复数的虚数部分
(2) 复数类Complex 的方法有:
Complex():构造函数,将复数的实部和虚部都置0
Complex (intr,inti):构造函数,形参r为实部的初值,i为虚部的初值。
ComplexeomplexAdd (Complexa):将当前复数对象与形参复数对象相加,所得的结果仍是一个复数值,返回给此方法的调用者
String ToString():把当前复数对象的实部、虚部组合成s+ bi 的字符串形式,其中a和b分别为实部和虚部的数据。
importjava. applet. * ;
importjava. awt. * ;
publicclassabcextends Applet
{
Complex a, b, c;
publi cvoid init()
{
a = newComplex(1,2);
b = newComplex(3,4);
c = newComplex();
}
publievoidpaint (Graphicsg)
{
(1)
g. drawstring(“第一个复数:” + a. toString(), 10,50);
g. drawstring(“第二个复数:” + b. toString(), 10,70 );
g. drawstring(“两复之和:” + c. toString(), 10,90);
}
}
class Complex
{
int RealPart;
int ImaginPart;
Complex() { (2) }
Complex(intr , inti)
{ (3) }
ComplexeomplexAdd (Complexa)
{
Complextemp = newComplex();
temp. BealPart = RealPart + a. BealPart;
(4)
returntemp;
}
public StringtoString()
{ return(RealPart + " + " + ImaginPart + " i "); }
}
第7题
(13 )在 MyClass 类的定义中,对赋值运算符 = 进行重载。请将画线处缺失的部分补充完整。
【 13 】 MyClass::operator=(const MyClass& rhs)
{
if (this == &rhs) return *this;
value = rhs.value;
return *this;
}
第8题
class complex
{
private:
int real;
int imag;
public:
complex(int r=0,int i=0):real(r),imag(i){}
void show ()
{
cout<<real<<(imag<0?"-":"+")<<imag<<'i';
}
______;
};
complex& operator -- (complex &c)
{
c.real --;
return c;
}
第9题
class complex
{
double real; // 实部
double imag; // 虚部
public:
complex(double r,double i):real(r),imag(i){}
complex operator+(complex a)
{
return complex(【 14 】 );
}
};
第10题
class my(21ass{
private:
int data:
public::
MyClass(int value);//构造函数
MyClass(const______anotherObject); //复制构造函数
为了保护您的账号安全,请在“赏学吧”公众号进行验证,点击“官网服务”-“账号验证”后输入验证码“”完成验证,验证成功后方可继续查看答案!