第一范文网 - 专业文章范例文档资料分享平台

面向对象程序设计期末复习题及答案1

来源:用户分享 时间:2025/8/25 1:59:50 本文由loading 分享 下载这篇文档手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:xxxxxxx或QQ:xxxxxx 处理(尽可能给您提供完整文档),感谢您的支持与谅解。

C++面向对象程序设计复习

试题类型 1、单项选择题(在每小题的四个备

选答案中,选出一个正确答案,并将正确答案的序号填在题干的括号内。15题,每小题2分,共30分) [主要从作业题目中抽出来]

2. 填空题。(10题,每小题3分,共30分)

3、阅读程序,写出程序运行结果。【也称计算题】( 3题,每小题5分,共15分)

4、程序编制题【也称综合分析题】。 (第1、2题每题8分,第3题9分,共25分) 重点复习内

打*号是重点,打▲号是编程题出题范围 * 基本概念,对象,消息,类。

面向对象系统特性,封装性,继承性,多态性。 *▲ C++类的构成,类与对象,构造与析构,动态存储,类嵌套。

静态成员,对象数组,友元。 *函数重载。 运算符重载。

*引用定义与引用参数 * 基类与派生类的定义。

* 基类及派生类的访问权(含派生类对基类的访问、通过派生类对象对基类的访问)和初始化。 多继承与虚基类。 *多态性与虚函数。 纯虚函数、抽象类。 *▲函数模板。

*▲使用类来定义对象并在程序中的应用 题型样题

填空题 1. 假定AB为一个类,则执行“AB a[10];”语句时,系统自动调用该类的构造函数的次数为_________。 答案:10

2.可以用p.a的形式访问派生类对象P的基类成员a,其中a是_________。 答案:公有继承的公有成员

3.能作为重载函数的调用的依据是_________。 答案:参数个数 、参数类型

- 1 -

4.在进行完任何C++流的操作后,都可以用C++流的有关成员函数检测流的状态;其中只能用于检测输入流是否结束状态的操作函数名称是_________ 答案:eof

5.函数重载是指_________。

答案:两个或两个以上的函数取相同的函数名,但形参的个数或类型不同

6.在派生类中重新定义虚函数时必须在 _________ 方面与基类保持一致。

答案:参数个数

阅读程序例题【计算题】

1.分析以下程序的执行结果 #include class Sample {

int x,y; public:

Sample() {x=y=0;}

Sample(int a,int b) {x=a;y=b;} ~Sample() {

if(x==y)

cout<<“x=y”<

cout<<“x!=y”<

void disp() {

cout<<“x=”<

void main() {

Sample s1,s2(2,3); s1.disp(); s2.disp(); }

解: x=0,y=0 x=2,y=3 x!=y x=y

2.分析以下程序的执行结果 #include int add(int x,int y) {

return x+y; }

int add(int x,int y,int z) {

return x+y+z; }

void main() {

int a=4,b=6,c=10;

cout<

10,20 3.分析以下程序执行结果 #include int add(int x,int y) {

return x+y; }

double add(double x,double y) {

return x+y; }

void main() {

int a=4,b=6;

double c=2.6,d=7.4;

cout<

4.分析以下程序执行的结果 #include class Sample {

int x,y; public:

Sample(){x=y=0;}

Sample(int a,int b){x=a;y=b;} void disp() {

cout<<\} };

void main() {

Sample s1,s2(1,2),s3(10,20); Sample *pa[3]={&s1,&s2,&s3}; for(int i=0;i<3;i++) pa[i]->disp(); }

解:

x=0,y=0 x=1,y=2

x=10,y=20 5. 分析以下程序的执行结果: #include class base {

int n; public: base(){}; base (int a) {

cout << \base class\<< endl; n=a;

cout << \}

~base() { cout << \base class\<< endl; } };

class subs : public base {

int m; public:

subs(int a, int b) : base(a) {

cout << \m=b;

cout << \}

~subs() { cout << \sub class \endl; } };

void main () {

subs s(1,2); }

解:

constructing base class n= 1

constructing sub class m= 2

destructing sub class destructing base class 6.分析以下程序的执行结果:

- 2 -

#include class Sample {

protected: int x; public:

Sample() { x=0; }

Sample(int val) { x=val; } void operator++() { x++; } };

class Derived:public Sample {

int y; public:

Derived():Sample(){ y=0; } Derived(int val1,int val2):Sample(val1){ y=val2; } void operator--(){ x--;y--;} void disp() {

cout<<\} };

void main () {

Derived d(3,5); d.disp(); d++;

d.disp (); d--; d--;

d.disp(); }

解: x=3,y=5 x=4,y=5 x=2,y=3

7 分析以下程序执行结果:

#include class A {

public:

A(char *s) { cout << s << endl; } ~A() {}

- 3 -

};

class B:public A {

public:

B(char *sl,char *s2) :A(sl) {

cout << s2 << endl; } };

class C:public A {

public:

C(char *sl,char *s2) :A(sl) {

cout << s2 << endl; } };

class D:public B,public C {

public: D(char *sl,char *s2,char *s3,char *s4) :B(sl,s2),C(sl,s3) {

cout << s4 << endl; } };

void main () {

D d(\A\B\C\D\}

解: class A class B class A class C

class D 补充例题

1.分析以下程序的执行结果 #include template T max(T x,T y)

{ return (x>y?x:y); } void main()

{ cout<

<

解答:5,3.5 2.分析以下程序的执行结果 #include void main() {

int a[]={10,20,30,40},*pa=a; int *&pb=pa; pb++;

cout<<*pa<

解答:

输出为:20 3.分析以下程序的执行结果 #include template T abs(T x)

{ return (x>0?x:-x); } void main()

{ cout<

解答:输出为:3,2.6

4.分析以下程序的执行结果 #include class Sample {

char c1,c2; public:

Sample(char a){c2=(c1=a)-32;} void disp()

{ cout<

void main() {

Sample a('a'),b('b'); a.disp(); b.disp(); }

解答:

a转换为A

b转换为B 5.分析以下程序的执行结果 #include void main() {

int &b=a; // 变量引用 b=10;

cout<<“a=”<

解答:

输出为:a=10 6.分析以下程序的执行结果 #include class Sample {

int x; public:

Sample(){};

Sample(int a){x=a;}

Sample(Sample &a){x=a.x++ +10;}

void disp(){cout<<“x=”<

void main()

{ Sample s1(2),s2(s1);

s1.disp(); s2.disp(); }

解答:

x=3 // ++运算的结果 x=12 // 2+10

7.分析以下程序的执行结果 #include class Sample {

int x; public:

Sample(){};

Sample(int a){x=a;}

Sample(Sample &a){x=a.x+1;}

void disp(){cout<<“x=”<

void main()

{ Sample s1(2),s2(s1);

s2.disp(); }

解答:

输出为:x=3。

程序编制题例题【综合分析题】

例1,写出一个梯形类,操作有求面积和周长

- 4 -

面向对象程序设计期末复习题及答案1.doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.diyifanwen.net/c0s0pz7wakd1j03u4hzdl_1.html(转载请注明文章来源)
热门推荐
Copyright © 2012-2023 第一范文网 版权所有 免责声明 | 联系我们
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:xxxxxx 邮箱:xxxxxx@qq.com
渝ICP备2023013149号
Top