C++期末练习题答案文档格式.docx
《C++期末练习题答案文档格式.docx》由会员分享,可在线阅读,更多相关《C++期末练习题答案文档格式.docx(19页珍藏版)》请在冰点文库上搜索。
publicStudent{//……}
D.classDerived:
privateStudent{//……}
二、填空题
1、面向对象六大设计原则为:
单一职责原则、接口隔离原则、依赖倒转原则、里氏替换原则、开闭原则、迪米特法则。
2、在C++程序设计中,建立继承关系倒挂的树应使用单一(或单)继承。
3、在一个类中可以对一个操作符进行函数重载。
4、当建立对象时,程序自动调用该类的构造函数。
5、设px是指向一个类动态对象的指针变量,则执行”deletepx;
”语句时,将自动调
用该类的析构函数。
6、若需要把一个函数”voidF();
”定义为一个类AB的友元函数,则应在类AB的定义
中加入一条语句:
friendvoidF();
7、在面向对象的程序设计中,通过类实现数据隐藏;
通过继承实现代码的复用。
8、在C++中,定义重载函数时,应至少使重载函数的参数个数或参数类型不同。
9、在C++类中,有一种不能定义对象的类,这样的类只能被继承,称之为抽象类,定义该类至少具有一个纯虚函数。
10、允许访问一个类的所有对象的私有成员、公有成员和保护成员的函数是该类的成员函数和友员函数。
11、在C++中,用数组、指针、和引用作为函数参数,能够将参数值带回。
12、在C++中,虽然友元提供了类之间数据进行访问的一种方式,但它破坏了面向对象程序设计的封装特性。
13、C++中,对象保存在内存中,栈内存是自动分配和释放的,而堆内存需要用户自己申请和释放。
14、执行完C++程序中的三条语句:
inta,b,*c=&
a;
int*&
p=c;
p=&
b;
后c指向b。
15、在已经定义了整型指针ip后,为了得到一个包括10个整数的数组并由ip所指向,应使用语句ip=newint[10];
。
三、程序阅读
1、给出下面程序的输出结果。
#include<
iostream>
usingnamespacestd;
template<
classT>
Tmin(Tml,Tm2)
{
return(m1<
m2)?
ml:
m2;
}
voidmain(){
cout<
<
min(1,7)<
″\t″<
min(2.0,5.0)<
endl;
min(′y′,′b′<
″\t″<
min(″A″,″a″)<
}
答案:
12
bA
2、给出下面程序的输出结果。
#include<
classA{
public:
intx;
A(){}
A(inta){x=a;
intget(inta){returnx+a;
}};
Aa(8);
int(A:
*p)(int);
p=A:
get;
(a.*p)(5)<
A*pi=&a;
(pi->
*P)(7)<
}
13
15
3、程序的输出结果如下:
1,9
50,30
请根据输出数据在下面程序中的下划线处填写正确的语句。
源程序如下:
classbase
private:
intm;
base(){};
base(inta):
m(a){}
intget(){returnm;
voidset(inta){m=a;
};
voidmain()
base*ptr=newbase[2];
ptr->
set(30);
ptr=____ptr+1;
_____;
set(50);
basea[2]={1,9};
a[0].get()<
″,″<
a[1].get()<
ptr->
get()<
″,″;
ptr=ptr-1;
_____ptr->
get()___<
delete[]ptr;
}
4、在下划线处填上缺少的部分。
fstream>
classcomplex
intreal;
intimag;
complex(intr=0,inti=0)
real=r;
imag=i;
complexoperator+(___complex&
a_____,complex&
b)
intr=a.real+b.real;
inti=a.imag+b.imag;
return____complex(r,i)_____;
voidmain()
complexx(1,2),y(3,4),z;
z=x+y;
z.real<
″+″<
z.imag<
″i″<
四、编程题
1、在三角形类triangle实现两个函数,功能是输入三个顶点坐标判断是否构成三角形。
要求使用下方point类来存储三个顶点坐标。
classpoint{
floatx,y;
point(floata,floatb){x=a;
y=b;
point(){x=0;
y=0;
},
voidset(floata,floatb){x=a;
floatgetx(){returnx;
floatgety(){returny;
(判断三条边能构成三角形的条件:
任意两边之和大于第三边或任意两边之差小于第3边。
)
iostream.h>
math.h>
#include“point.h”
classtriangle{
private:
pointx,y,z;
floats1,s2,s3;
public:
triangle(floatx1,floaty1,floatx2,floaty2,floatx3,floaty3);
//用于输入三个顶点坐标
booltest();
//用于判断是否构成三角形
};
triangle:
triangle(floatx1,floaty1,floatx2,floaty2,floatx3,floaty3)
{
x.set(x1,y1);
y.set(x2,y2);
z.set(x3,y3);
voidtriangle:
test(){
s1=sqrt((x.getx()-y.getx())*(x.getx()-y.getx())+(x.gety()-y.gety())*(x.gety()-y.gety()));
s2=sqrt((x.getx()-z.getx())*(x.getx()-z.getx())+(x.gety()-z.gety())*(x.gety()-z.gety()));
s3=sqrt((y.getx()-z.getx())*(y.getx()-z.getx())+(y.gety()-z.gety())*(y.gety()-z.gety()));
if(((s1+s2>
s3)&
&
(s1+s3>
s2)&
(s2+s3>
s1))||
((abs(s1-s2)<
(abs(s1-s3)<
(abs(s3-s2)<
s1))){
cout<
“三个顶点能构成三角形”;
returntrue;
else{
“三个顶点坐标不能构成三角形”;
returnfalse;
2、若链表结点结构如下:
structnode
intnum;
structnode*next;
请你完成链表逆置函数structnode*reverse(structnode*head)。
structnode*reverse(structnode*head){//head链表头结点
structnode*p,*temp1,*temp2;
if(head==NULL||head->
next==NULL)returnhead;
p=head->
next;
head->
next=NULL;
while(p!
=NULL)
temp1=head;
head=p;
temp2=p;
p=p->
temp2->
next=temp1;
returnhead;
3、定义并实现一个字符栈类Stack,数据成员包括一个存放字符的数组stck[]和一个栈指针tos,栈数组的大小由常量SIZE确定。
要求:
1)实现栈的基本操作Push()和Pop(),注意判断栈满和栈空的情况
2)实现c_count()方法计算字符ch出现的次数,如果ch不出现,则返回0
3)实现inverse()方法使数组元素按逆序排列
答案:
constintSIZE=100;
classStack
charstck[SIZE];
inttos;
Stack():
tos(0){};
voidPush(charch);
charPop();
intc_count(charch);
voidinverse();
};
voidStack:
Push(charch)
if(tos==SIZE)
cout<
”\nStackisfull\n”;
else{
stck[tos]=ch;
tos++;
charStack:
Pop(){
if(tos==0){
”\nStackisempty\n”;
return0;
tos--;
returnstck[tos];
intStack:
c_count(charch){
inttemp=tos;
intcount=0;
while(temp>
0){
temp--;
if(stck[temp]==ch)
count++;
returncount;
inverse(){
char*p=newchar[tos];
for(inti=0;
i<
=tos-1;
i++)
p[i]=a[i];
a[i]=a[tos-i-1];
delete[]p;
4、小明开了一家面馆,菜单包括:
牛肉面、羊肉面、臊子面、西红柿鸡蛋面等。
请你设计服务员类、厨房类、面条类、牛肉面、羊肉面等,模拟点单制作面条的全过程。
因为菜单上面条种类一直在变化更改,因此要求使用设计模式——工厂模式,来减少后期代码修改的工作量。
1)要求使用继承实现
2)要求使用工厂设计模式
3)编写main()方法进行测试
#pragmaonce
#include<
usingnamespacestd;
classNoodle
stringn_type;
Noodle(string_n_type):
n_type(_n_type){};
virtual~Noodle();
virtualvoidmake();
virtualvoidcooking();
virtualvoidsauce();
#include"
Noodle.h"
Noodle:
~Noodle()
voidNoodle:
make()
cout<
n_type.c_str()<
"
制作中,开始擀面条"
<
endl;
cooking()
"
制作中,开始煮面条"
sauce()
制作中,开始调味"
classBeefNoodle:
publicNoodle
BeefNoodle():
Noodle("
牛肉面"
){};
~BeefNoodle();
BeefNoodle.h"
BeefNoodle:
~BeefNoodle()
classMuttonNoodle:
MuttonNoodle():
羊肉面"
~MuttonNoodle();
pch.h"
MuttonNoodle.h"
MuttonNoodle:
~MuttonNoodle()
classSaoziNoodle:
SaoziNoodle():
臊子面"
~SaoziNoodle();
SaoziNoodle.h"
SaoziNoodle:
~SaoziNoodle()
classTomatoandEggNoodle:
TomatoandEggNoodle():
西红柿鸡蛋面"
~TomatoandEggNoodle();
TomatoandEggNoodle.h"
TomatoandEggNoodle:
~TomatoandEggNoodle()
classCook
Cook();
~Cook();
Noodle*createNoodles(conststd:
string&
type);
Cook.h"
Cook:
Cook()
~Cook()
Noodle*Cook:
createNoodles(conststd:
type)
if(type=="
){
returnnewBeefNoodle();
elseif(type=="
){
returnnewMuttonNoodle();
elseif(type=="
returnnewSaoziNoodle();
returnnewTomatoandEggNoodle();
else{
returnNULL;
classWaiter
Cook&
cook;
Waiter(Cook&
_cook):
cook(_cook){};
~Waiter();
Noodle*orderNoodles(conststd:
Waiter.h"
Waiter:
~Waiter()
Noodle*Waiter:
orderNoodles(conststd:
Noodle*noodle=cook.createNoodles(type);
if(noodle){
noodle->
make();
cooking();
sauce();
returnnoodle;
intmain(){
Cookcook;
Waiterwaiter(cook);
Noodle*noodle1=waiter.orderNoodles("
);
if(noodle1){
deletenoodle1;
Noodle*noodle2=waiter.orderNoodles("
if(noodle2){
deletenoodle2;
Noodle*noodle3=waiter.orderNoodles("
if(noodle3){
deletenoodle3;
Noodle*noodle4=waiter.orderNoodles("
if(noodle4){
deletenoodle4;