继承与多态性习题参考答案

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

继承与多态性习题参考答案
————————————————————————————————作者:————————————————————————————————日期:
C++程序设计语言习题及实验指导
第8章继承与多态性习题参考答案
8.3 习题
8.3.1 选择题
题号 1 2 3 4 5 6 7 8 9 10 答案 C D A D A B C B D D 题号11 12 13 14 15 16 17 18 19 20 答案 D D A C B C C C A C
8.3.2 填空题
1.(1) a、b、c、x、y、z (2) b、y (3) c、z (4) a、x (5) b、c、x、y、z
(6) b、y (7) y (8) b、c、z (9) a、x (10) b、c、x、y、z (11) y
(12) y (13) z (14) a、b、c、x (15) b、c、x、y、z (16) y 2.私有
3.抽象
4.虚基
5.(1) 基类(2) 对象
6.(1) 静态(2) 编译(3) 虚函数
7.(1) 2 1 (2) 0 5
8.(1) 100 200 300 30 (2) 10 100 (3) 20 200 9.(1) 5 10 (2) end. (3) 20 10
10.(1) classA classB (2) end. (3) ~classB ~classA 11.(1) 10 (2) build B (3) build C (4) release A
12.(1) class B (2) class C (3) class D (4) 5
13.(1) 5 5 (2) 20 20
14.(1) 10 (2) 10 20
15.(1) 1 (2) 3 (3) 5 (4) 100 10
16.(1) B::f() (2) B::fun() (3) A::f() (4) B::fun()
17.(1) 姓名陈涛年薪6.8万元。

(2) 姓名李原(3) 姓名李原月工资4000元18.(1) 110 (2) 2220
19.(1) protected 或public (2) Base1(s1),Base2(s2) 或Base2(s2),Base1(s1)
(3) Base1::str (4) test.print()
20.(1) virtual void show()=0 (2) C(int,int,int) (注:参数可有任一名称)
(3) b1(y) (4) &t
8.3.3 编程题
1.求长方形的面积和长方体的表面积、体积
#include<iostream.h>
class Rectangle{
protected:
float L; // 长方形的长
float W; // 长方形的宽
float area; // 长方形的面积
public:
Rectangle(float L1,float W1)
{
L=L1;
W=W1;
}
void function() { area=L*W; }
void show()
{
cout<<"长为"<<L<<",宽为"<<W<<"的长方形面积为"<<area<<endl;
}
};
class Cuboid:public Rectangle{
float H; // 长方体的高
float volume; // 长方体的体积
public:
Cuboid(float a,float b,float c):Rectangle(a,b)
{ H=c; }
void function() { area=(L*W+L*H+W*H)*2; }
void fun() { volume=L*W*H; }
void show()
{
cout<<"长为"<<L<<",宽为"<<W<<",高为"<<H<<"的长方体表面积为"<<area<<",体积为"<<volume<<endl;
}
};
void main()
{
Rectangle r(2,3); r.function(); r.show();
Cuboid c(2,3,4); c.function(); c.fun(); c.show();
}
2.产生并输出圆桌信息
#include<iostream.h>
#include<string.h>
class Circle{
protected:
double radius;
double area;
public:
Circle(double r) { radius=r; }
};
class Table{
protected:
double height;
public:
Table(double h) { height=h; }
};
class Roundtable:public Table,public Circle
{
char *color;
public:
Roundtable(double h,double r,char c[]):Circle(r),Table(h) {
color=new char[strlen(c)+1];
strcpy (color, c);
}
void fun() { area=3.14*radius*radius; }
void show()
{
cout<<"高度:"<<height<<"米"<<endl;
cout<<"面积:"<<area<<"平方米"<<endl;
cout<<"颜色:"<<color<<endl;
}
~Roundtable() { delete []color; }
};
void main()
{
Roundtable rt(0.75,1.1,"黑色");
rt.fun(); rt.show();
}
3.计算企业的经营税
#include<iostream.h>
#include<string.h>
class Tax{
protected:
char name[50]; // 企业名称
double income; // 经营收入
double taxes; // 税金
public:
Tax(char *n,double in)
{
strcpy(name,n);
income=in;
}
virtual void fun()=0;
void show()
{
cout<<name<<"的经营收入为"<<income<<"万元,税金为"<<taxes<<"万元\n";
}
};
class Service:public Tax{
public:
Service(char *n,double in):Tax(n,in)
{ }
void fun()
{
taxes=income*5/100;
}
};
class Fabrication:public Tax{
public:
Fabrication(char *n,double in):Tax(n,in)
{ }
void fun()
{
taxes=income*17/100;
}
};
void print(Tax &t) { t.fun(); t.show(); }
void main()
{
char name[50];
int income;
cout<<"请输入服务性企业名称:"; cin>>name;
cout<<"请输入经营收入(万元):"; cin>>income;
Service s(name,income); print(s);
cout<<"请输入生产性企业名称:"; cin>>name;
cout<<"请输入经营收入(万元):"; cin>>income;
Fabrication f(name,income); print(f);
}
4.把原始体育成绩转换为等第
#include<iostream.h>
class Sports{
protected:
int item; // 项目
char name[20]; // 姓名
double score; // 原始成绩
char grade[10]; // 评定等第
public:
Sports(int i,char *n)
{
item=i;
strcpy(name,n);
cout<<"请输入原始成绩:";
cin>>score;
}
void show()
{
cout<<name<<"的"<<item<<"米成绩为"<<score<<"秒,等第为"<<grade<<endl;
}
virtual void change()=0;
};
class Run50:public Sports{
public:
Run50(int i,char *n):Sports(i,n)
{ }
void change()
{
if(score<=6.50)strcpy(grade,"优秀");
else if(score<=6.70)strcpy(grade,"良好");
else if(score<=7.10)strcpy(grade,"及格");
else strcpy(grade,"不及格");
}
};
class Run100:public Sports{
public:
Run100(int i,char *n):Sports(i,n)
{ }
void change()
{
if(score<=13.10)strcpy(grade,"优秀");
else if(score<=13.70)strcpy(grade,"良好");
else if(score<=14.90)strcpy(grade,"及格");
else strcpy(grade,"不及格");
}
};
class Run1000:public Sports{
public:
Run1000(int i,char *n):Sports(i,n)
{ }
void change()
{
if(score<=203.00)strcpy(grade,"优秀");
else if(score<=213.00)strcpy(grade,"良好");
else if(score<=233.00)strcpy(grade,"及格");
else strcpy(grade,"不及格");
}
};
class Run1500:public Sports{
public:
Run1500(int i,char *n):Sports(i,n)
{ }
void change()
{
if(score<=323.00)strcpy(grade,"优秀");
else if(score<=337.00)strcpy(grade,"良好");
else if(score<=365.00)strcpy(grade,"及格");
else strcpy(grade,"不及格");
}
};
void main()
{
Sports *s;
int it;
cout<<"请输入项目(50/100/1000/1500):"; c in>>it;
char na[20];
cout<<"请输入姓名:"; c in>>na;
if(it==50) { Run50 r50(it,na); s=&r50; s->change(); s->show(); }
else if(it==100) { Run100 r100(it,na); s=&r100; s->change(); s->show(); }
else if(it==1000) { Run1000 r1000(it,na); s=&r1000; s->change(); s->show(); } else if(it==1500) { Run1500 r1500(it,na); s=&r1500; s->change(); s->show(); } else { cout<<"输入项目错误\n"; exit(0); }
}
8.4 实验指导
8.4.1 派生类的定义与使用:设计一个程序求三角函数的值。

#include<iostream.h>
#include<math.h>
class Trigonometric{
protected:
double arc;
double value;
public:
Trigonometric(int t)
{
arc=t%360*3.14159/180;
value=0;
}
};
class Sine:public Trigonometric{
public:
Sine(int t):Trigonometric(t)
{ }
double f1(double x,int n)
{
double t=1;
for(int i=0;i<n;i++)
t*=x;
return t;
}
int f2(int n)
{
if(n==0||n==1)return 1;
else return n*f2(n-1);
}
void function()
{
int i=1,k=1;
double t;
do{
t=k*f1(arc,2*i-1)/f2(2*i-1);
value+=t;
i++;
k*=-1;
}while(fabs(t)>=1e-6);
}
void show()
{
cout<<"sin("<<arc<<")="<<value<<endl;
}
};
void main()
{
int d;
cout<<"请输入度数:";
cin>>d;
Sine sin(d);
sin.function();
sin.show();
}
8.4.2 虚函数实现运行多态性:设计一个程序判断某人是否为优秀教师或优秀学生。

#include<iostream.h>
class People{
protected:
char category[20]; // 人员类别
char name[10]; // 姓名
int num; // 分数或论文数
int result; // 结论,1为优秀,0为不优秀
public:
People( )
{
cout<<"请输入人员类别:"; cin>>category;
cout<<"请输入姓名:"; cin>>name;
}
void show()
{
if(result)cout<<name<<"是优秀"<<category<<endl;
else cout<<name<<"不是优秀"<<category<<endl;
}
virtual void inputnum()=0;
virtual void isgood( )=0;
};
class Student:public People{
public:
void inputnum()
{
cout<<"请输入分数:";
cin>>num;
}
void isgood( ) { result=(num>90)? 1 : 0; }
};
class Teacher:public People{
public:
void inputnum( )
{
cout<<"请输入论文数:";
cin>>num;
}
void isgood( ) { result=(num>5)? 1 : 0 ; }
};
void main()
{
People *p;
Student s;
p=&s;
p->inputnum(); p->isgood(); p->show(); // A
Teacher t;
p=&t;
p->inputnum(); p->isgood(); p->show(); // B
}
11 / 11。

相关文档
最新文档