程序设计与算法语言---C++程序设计基础习题答案 习题8
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
一、选择题
1-5 CDBBC
6-10 DBBAA
11-12 AC 注意:第11小题可以不做,教材未讲虚基类的内容
13 BC
14-16 ADC
注明第14小题:C++中运算符“.”,“::”,“sizeof()”,“?:”,“.*”不能重载
二、程序填空题
1. int sum() x=a y=b num num1(i,j) num1.sum()
2. base::fun()
3. x[i]+a.x[i] t x[i]+a.x[i] (*this)
4.virtual *pa pa=&p
三、分析程序输出结果
1. constructing A!
constructing A!
destructing A!
destructing A!
2. c1:1
第1次调用拷贝构造函数!
c2:1
第2次调用拷贝构造函数!
c3:2
第3次调用拷贝构造函数!
c4:15
3. 2
2
1
4. ABC
5. base::display1()
derived::display2()
四、编写程序
1.
#include
class square
{
private:
float radius;
public:
square(float d=0)
{radius=d;}
float perimeter()
{return 4*radius;}
};
void main()
{ square p(4);
cout<<"正方形p的周长是"<
2.
#include
class building
{
private:
float length,broadth;
int story;
public:
building(float a,float b,int c)
{length=a;broadth=b;story=c;}
float area()
{return length*broadth*story;}
};
void main()
{ building p(30,40,10);
cout<
}
3.
#include
class cube
{
private:
float x;
public:
cube(float d=0)
{x=d;}
float area()
{return 6*x*x;}
float v()
{return x*x*x;}
};
void main()
{ float a;
cin>>a;
cube p(a);
cout<<"体积为"<
cout<<"表面积为"<
}
4.
#include
class point
{
private:
float x,y,z;
public:
point(float a,float b,float c)
{x=a;y=b;z=c;}
point()
{ }
void print()
{cout<<'('< { point B; B.x=x+A.x; B.y=y+A.y; B.z=z+A.z; return B; } friend void operator ++(point &A) { A.x=A.x+1; A.y=A.y+1; A.z=A.z+1; } }; void main() { point A(1,2,3),B(2,6,10),C; C=A+B; C.print(); ++A; A.print(); } 5. #include class Person { protected: char name[20]; int age; public: void input() { cout<<"姓名:"; cin>>name; cout<<"年龄:"; cin>>age; } void disp() { cout< cout< } }; class Student:private Person { private: char cla[20]; int num; public: void input() { Person::input(); cout<<"班级:"; cin>>cla; cout<<"学号:"; cin>>num; } void disp() { Person::disp(); cout< cout< } }; class Teacher:private Person