习题10答案
一、选择题、填空题
答案:
1. C B
2. D
3. D
4. D
sigg5. B
synergy6. C
7. B
8. A
9. B 10. C
11. A 12. B 13. A 14. C
二、找错、运行结果
1.
错误原因:具有两个缺省构造函数(重复定义缺省构造函数)
2.
运行结果:
Default constructor called.
Constructor called.
0,0
6,8
6,8
Destructor called.
0,0
Destructor called.
3.
答案(版本1):(使用左框中的程序产生的结果)
0,0,Constructor called.
6,8,Constructor called.
6,8,Copy constructor called.
0,0,Constructor called.
6,8,Copy constructor called.
6,8,
3,7,Copy constructor called.
3,7,Destructor called.
3,7,Destructor called.
3,7,6,8,
3,7,Destructor called.
6,8,Destructor called.
6,8,Destructor called.
0,0,Destructor called.
答案(版本2):(使用右框中的程序产生的结果)
0,0,Constructor called.
6,8,Constructor called.
6,8,Copy constructor called. 0,0,Constructor called.
6,8,
3,7,3,7,
3,7,Destructor called.
memorial day3,7,Destructor called.
6,8,Destructor called.
0,0,Destructor called.
4.
运行结果:
0,0,Constructor called.
6,8,Constructor called.
6,8,
3,7,
3,7,Destructor called.
3,7,Destructor called.
6,8,Destructor called.
0,0,Destructor called.
5.
运行结果:
内训课
(3,0)Construct
(4,0)Construct
(0,0)Construct
(5,6)Construct
(0,0)Construct
(3,0)
(4,0)
(0,0)
(5,6)
(0,0)
Exit main( )
(0,0)Destruct
(5,6)Destruct
(0,0)Destruct
(4,0)Destruct
(3,0)Destruct
6.运行结果:
8,0x0012FF7C
9,0x0012FF78
7.
运行结果:
1,2,3
4,5,6,7,8
Different! copied!
1,2,3
1,2,3
Different! copied!
Different! copied!
1,2,3
1,2,3
三、编程题
1.
答案:
#include<iostream.h>
class complex
{
double real,image;
public:
void treal( double r)
{ real=r; }
pelivoid timage(double i)
{ image=i; }
double getreal( )
{ return(real); }
double getimage( )
{ return(image); }
void output()
{ if(image<0)
cout<<real<<image<<'i';
el if(image>0)
cout<<real<<'+'<<image<<'i';
el
cout<<real;
cout<<'\n';
}
};
void main()
{ complex c1,c2;
c1.timage(2);
cout<<"c1=";
c1.output();
c2.timage(4);
cout<<"c2=";
c2.output();
double a,b;
al();
image();
cout<<a<<','<<b<<endl;
}
2.
答案:
#include<iostream.h>
#include<math.h>
class Line
{
private:
int x1,y1,z1,x2,y2,z2;
negro的复数
public:
海啸奇迹 豆瓣
void SetPoint1(int x,int y,int z)
{ x1=x;y1=y;z1=z; }
void SetPoint2(int x,int y, int z)
{ x2=x;y2=y;z2=z; }
double Length()
{ return (sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)+(z1-z2)*(z1-z2))); } void GetPoint1(int *px,int *py,int *pz)
{ *px=x1; *py=y1; *pz=z1; }
void GetPoint2(int &x,int &y, int &z)
{ x=x2; y=y2; z=z2; }
void Output()
{
cout<<"端点的坐标为:"<<"("<<x1<<","<<y1<<","<<z1<<")"<<","
<<"("<<x2<<","<<y2<<","<<z2<<")"<<endl;
cout<<"线段长度为:"<<Length()<<endl;
}
};
void main()
{ Line line;
int x,y,z;
cout<<"请输入第1点坐标:";
cin >> x >> y >> z ;
line.SetPoint1(x,y,z);
cout<<"请输入第2点坐标:";
cin >> x >> y >> z ;
line.SetPoint2(x,y,z);
line.Output();
int a,b,c;
line.GetPoint1(&a,&b,&c);
bvcout<<a<<','<<b<<','<<c<<endl;
line.GetPoint2(a,b,c);
cout<<a<<','<<b<<','<<c<<endl; }
3.
答案:
#include<iostream.h>
#include<math.h>
class Point
{
int x,y;
public:
void SetPoint(int x1, int y1)
{ x=x1; y=y1; }
void GetPoint(int &x1, int &y1)
{ x1=x; y1=y; }
void Setx(int xx)
{ x=xx; }
void Sety(int yy)
{ y=yy; }
int Getx()
{ return x; }
int Gety()
{ return y; }
};
class Triangle
{
Point p[3];
public:
void SetPoint1(int xx, int yy)
{ p[0].SetPoint(xx,yy); }
void SetPoint2(int xx, int yy)
{ p[1].SetPoint(xx,yy); }
void SetPoint3(int xx, int yy)英语人才
{ p[2].SetPoint(xx,yy); }
double Distance(int i, int j)
{
int x1,x2,y1,y2;
unesco是什么意思
p[i-1].GetPoint(x1,y1);