C++程序设计教程--面向对象分册(郑秋生)完整答案.doc
文本预览下载声明
第1章 类和对象
选择题
1.C 2.B 3.C 4.A 5.C
6.A 7.C 8 C 9A 10 C
二、阅读题
1.x=2,y=3
2.x=2,y=3
x!=y
3.
Cstatic::va1=0
cs1.vaI=1
cs2.val=2
cs1.val=4
cs2.vaI=4
四、改错题
#include string.h
#include iostream.h
class person
{
public:
person(int n,char* nam,char s)
{
num=n;
strcpy(name,nam);
sex=s;
coutConstructor called.endl;
}
~person( )
{
coutDestructor called.endl;
}
void display( )
{
coutnum: numendl;
coutname: nameendl;
coutsex: sexendlendl;
}
private:
int num;
char name[10];
char sex;
};
int main( )
{
person s1(10010,Wang_li,f);
s1.display( );
person s2(10011,Zhang_fun,m);
s2.display( );
return 0;
}
五、编程题
5.1
#include iostream
using namespace std;
class CBox
{
public :
CBox(double l=0,double w=0,double h=0);
double area();
double volume ();
private :
double lengh;
double width;
double high;
};
CBox::CBox(double l,double w,double h)
{
lengh=l;
width=w;
high=h;
}
double CBox::area()
{
return 2*(lengh*width+lengh*high+width*high);
}
double CBox::volume ()
{
return lengh*width*high;
}
void main()
{
CBox box1(4,5,6);
coutbox1.area()endl;
coutbox1.volume()endl;
}
5.2
#include iostream
using namespace std;
class CPoint
{
public :
CPoint(double a=0,double b=0)
{
x=a;
y=b;
}
CPoint(CPoint p)
{
x=p.x;
y=p.y;
}
void print()
{
cout(x,y);
}
private :
double x,y;
};
class CLine
{
public:
CLine(double x1=0,double y1=0,double x2=0,double y2=0):p1(x1,y1),p2(x2,y2)
{
}
CLine(CPoint x,CPoint y):p1(x),p2(y)
{
}
CLine(CLine lin)
{
p1=lin.p1;
p2=lin.p2;
}
void DrawLine()
{
coutLine form;
p1.print();
coutto;
p2.print();
coutendl;
}
void Linedel()
{
coutdelete lineendl;
}
void move(CPoint x,CPoint y)
{
coutmove line
显示全部