C++期末练习题答案.docx

上传人:b****2 文档编号:20179399 上传时间:2023-04-25 格式:DOCX 页数:19 大小:20.15KB
下载 相关 举报
C++期末练习题答案.docx_第1页
第1页 / 共19页
C++期末练习题答案.docx_第2页
第2页 / 共19页
C++期末练习题答案.docx_第3页
第3页 / 共19页
C++期末练习题答案.docx_第4页
第4页 / 共19页
C++期末练习题答案.docx_第5页
第5页 / 共19页
点击查看更多>>
下载资源
资源描述

C++期末练习题答案.docx

《C++期末练习题答案.docx》由会员分享,可在线阅读,更多相关《C++期末练习题答案.docx(19页珍藏版)》请在冰豆网上搜索。

C++期末练习题答案.docx

C++期末练习题答案

C++期末练习题

一、选择题

1、使用值传递方式将实参传给形参,下列说法正确的是(A)

A.形参是实参的备份B.实参是形参的备份

C.形参和实参是同一对象C.形参和实参无联系

2、在函数调用时,如某一默认参数要指明一个特定值,则有(B)

A.其之后的参数都必须赋值B.其之前的参数都必须幅值

C.其前后所有参数都必须幅值D.其前后所有参数都不必赋值

3、intFunc(int,int);不可与下列哪个函数构成重载(B)

A.intFunc(int,int,int);B.doubleFunc(int,int);

C.doubleFunc(double,double);D.doubleFunc(int,double);

4、类的私有成员可在何处访问(D)

A.通过子类的对象访问B.本类及子类的成员函数中

C.通过该类对象访问D.本类的成员函数中填空题

5、this指针存在的目的是(B)

A.保证基类公有成员在子类中可以被访问

B.保证每个对象拥有自己的数据成员,但共享处理这些数据成员的代码

一、保证基类保护成员在子类中可以被访问

D.保证基类私有成员在子类中可以被访问

6、在编译指令中,宏定义使用哪个指令(B)

A.#includeB.#defineC.#ifD.#else

7、设类A将其它类对象作为成员,则建立A类对象时,下列描述正确的是(B)

A.A类构造函数先执行B.成员构造函数先执行

C.两者并行执行D.不能确定

8、对类中声明的变量,下列描述中正确的是(C)

A.属于全局变量

B.只属于该类

C.属于该类,某些情况下也可被该类不同实例所共享

D.任何情况下都可被该类所有实例共享

9、构造函数是在(B)时被执行的。

A.程序编译B.创建对象

C.创建类D.程序装入内存

10、假设已经定义好了类Student,现在要定义类Derived,它是从Student私有派生,则定义类Derived的正确写法是(D)

A.classDerived:

:

Studentprivate{//……}

B.classDerived:

:

Studentpublic{//……}

C.classDerived:

:

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

usingnamespacestd;

template

Tmin(Tml,Tm2)

{

return(m1

ml:

m2;

}

voidmain(){

cout<

cout<

}

答案:

12

bA

2、给出下面程序的输出结果。

#include

usingnamespacestd;

classA{

public:

intx;

A(){}

A(inta){x=a;}

intget(inta){returnx+a;}};

voidmain(){

Aa(8);

int(A:

:

*p)(int);

p=A:

:

get;

cout<<(a.*p)(5)<

A*pi=&a;

cout<<(pi->*P)(7)<

答案:

13

15

3、程序的输出结果如下:

1,9

50,30

请根据输出数据在下面程序中的下划线处填写正确的语句。

源程序如下:

#include

usingnamespacestd;

classbase

{

private:

intm;

public:

base(){};

base(inta):

m(a){}

intget(){returnm;}

voidset(inta){m=a;}

};

voidmain()

{

base*ptr=newbase[2];

ptr->set(30);

ptr=____ptr+1;_____;

ptr->set(50);

basea[2]={1,9};

cout<

cout<get()<<″,″;

ptr=ptr-1;

cout<<_____ptr->get()___<

delete[]ptr;

}

4、在下划线处填上缺少的部分。

#include

#include

usingnamespacestd;

classcomplex

{

public:

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;

cout<

}

四、编程题

1、在三角形类triangle实现两个函数,功能是输入三个顶点坐标判断是否构成三角形。

要求使用下方point类来存储三个顶点坐标。

classpoint{

private:

floatx,y;

public:

point(floata,floatb){x=a;y=b;}

point(){x=0;y=0;},

voidset(floata,floatb){x=a;y=b;}

floatgetx(){returnx;}

floatgety(){returny;}

};

(判断三条边能构成三角形的条件:

任意两边之和大于第三边或任意两边之差小于第3边。

答案:

#include

#include

#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)

cout<<“三个顶点能构成三角形”;

returntrue;

}

else{

cout<<“三个顶点坐标不能构成三角形”;

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->next;

temp2->next=temp1;

}

returnhead;

}

3、定义并实现一个字符栈类Stack,数据成员包括一个存放字符的数组stck[]和一个栈指针tos,栈数组的大小由常量SIZE确定。

要求:

1)实现栈的基本操作Push()和Pop(),注意判断栈满和栈空的情况

2)实现c_count()方法计算字符ch出现的次数,如果ch不出现,则返回0

3)实现inverse()方法使数组元素按逆序排列

答案:

constintSIZE=100;

classStack

{

private:

charstck[SIZE];

inttos;

public:

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){

cout<<”\nStackisempty\n”;

return0;

}

tos--;

returnstck[tos];

}

intStack:

:

c_count(charch){

inttemp=tos;

intcount=0;

while(temp>0){

temp--;

if(stck[temp]==ch)

count++;

}

returncount;

}

voidStack:

:

inverse(){

char*p=newchar[tos];

for(inti=0;i<=tos-1;i++)

p[i]=a[i];

for(inti=0;i<=tos-1;i++)

a[i]=a[tos-i-1];

delete[]p;

}

4、小明开了一家面馆,菜单包括:

牛肉面、羊肉面、臊子面、西红柿鸡蛋面等。

请你设计服务员类、厨房类、面条类、牛肉面、羊肉面等,模拟点单制作面条的全过程。

因为菜单上面条种类一直在变化更改,因此要求使用设计模式——工厂模式,来减少后期代码修改的工作量。

要求:

1)要求使用继承实现

2)要求使用工厂设计模式

3)编写main()方法进行测试

答案:

#pragmaonce

#include

usingnamespacestd;

classNoodle

{

private:

stringn_type;

public:

Noodle(string_n_type):

n_type(_n_type){};

virtual~Noodle();

virtualvoidmake();

virtualvoidcooking();

virtualvoidsauce();

};

#include"Noodle.h"

Noodle:

:

~Noodle()

{

}

voidNoodle:

:

make()

{

cout<

}

voidNoodle:

:

cooking()

{

cout<

}

voidNoodle:

:

sauce()

{

cout<

}

 

#pragmaonce

#include"Noodle.h"

classBeefNoodle:

publicNoodle

{

public:

BeefNoodle():

Noodle("牛肉面"){};

~BeefNoodle();

};

#include"BeefNoodle.h"

BeefNoodle:

:

~BeefNoodle()

{

}

#pragmaonce

#include"Noodle.h"

classMuttonNoodle:

publicNoodle

{

public:

MuttonNoodle():

Noodle("羊肉面"){};

~MuttonNoodle();

};

#include"pch.h"

#include"MuttonNoodle.h"

MuttonNoodle:

:

~MuttonNoodle()

{

}

#pragmaonce

#include"Noodle.h"

classSaoziNoodle:

publicNoodle

{

public:

SaoziNoodle():

Noodle("臊子面"){};

~SaoziNoodle();

};

#include"SaoziNoodle.h"

SaoziNoodle:

:

~SaoziNoodle()

{

}

#pragmaonce

#include"Noodle.h"

classTomatoandEggNoodle:

publicNoodle

{

public:

TomatoandEggNoodle():

Noodle("西红柿鸡蛋面"){};

~TomatoandEggNoodle();

};

#include"TomatoandEggNoodle.h"

TomatoandEggNoodle:

:

~TomatoandEggNoodle()

{

}

#pragmaonce

#include"Noodle.h"

classCook

{

public:

Cook();

~Cook();

Noodle*createNoodles(conststd:

:

string&type);

};

#include"Cook.h"

#include"BeefNoodle.h"

#include"MuttonNoodle.h"

#include"SaoziNoodle.h"

#include"TomatoandEggNoodle.h"

Cook:

:

Cook()

{

}

 

Cook:

:

~Cook()

{

}

Noodle*Cook:

:

createNoodles(conststd:

:

string&type)

{

if(type=="牛肉面"){

returnnewBeefNoodle();

}

elseif(type=="羊肉面"){

returnnewMuttonNoodle();

}

elseif(type=="臊子面"){

returnnewSaoziNoodle();

}

elseif(type=="西红柿鸡蛋面"){

returnnewTomatoandEggNoodle();

}

else{

returnNULL;

}

}

#pragmaonce

#include"Cook.h"

#include"Noodle.h"

classWaiter

{

private:

Cook&cook;

public:

Waiter(Cook&_cook):

cook(_cook){};

~Waiter();

Noodle*orderNoodles(conststd:

:

string&type);

};

#include"pch.h"

#include"Waiter.h"

 

Waiter:

:

~Waiter()

{

}

Noodle*Waiter:

:

orderNoodles(conststd:

:

string&type)

{

Noodle*noodle=cook.createNoodles(type);

if(noodle){

noodle->make();

noodle->cooking();

noodle->sauce();

}

returnnoodle;

}

 

#include

#include"Waiter.h"

#include"Cook.h"

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;

}

}

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 表格模板 > 合同协议

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1