2015-2016年度面向对象程序设计试卷.doc

上传人:scccc 文档编号:13200344 上传时间:2021-12-18 格式:DOC 页数:11 大小:60.50KB
返回 下载 相关 举报
2015-2016年度面向对象程序设计试卷.doc_第1页
第1页 / 共11页
2015-2016年度面向对象程序设计试卷.doc_第2页
第2页 / 共11页
2015-2016年度面向对象程序设计试卷.doc_第3页
第3页 / 共11页
亲,该文档总共11页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

《2015-2016年度面向对象程序设计试卷.doc》由会员分享,可在线阅读,更多相关《2015-2016年度面向对象程序设计试卷.doc(11页珍藏版)》请在三一文库上搜索。

1、华中科技大学研究生课程考试试卷课程名称:面向对象程序设计课程类别公共课专业课考核形式开卷闭卷学生类别试日期20 1 6年_月1 2日 院系学号课教师杨卫东、左峥嵘一、填空(共15分,每空1分)1、 静态多态性是通过函数重载、运算符重载、模板来实现。2、 面向对象的四个基本特性是 抽象、多态、继承 和 封装。3、 假定AB为一个类,则执行“ ABa(10),b1,*p10;”语句时调用该类构造函数的次数为 2。4、C+类成员函数有 public 、private 、protected 三种类型。5、 如果将一个对象说明为常对象,则通过该对象只能调用它的常成员 函数。&为使外部函数可访问类

2、的私有成员,需将该函数声明为该类的友元函数 。7、 类B是由类A以保护方式派生的,则类 A中公有访问属性的数据成员在类 B中的 访问属性为保护 。8、 +、=、-等四种运算符中,可采用友元函数重载的运算符是_+。9、抽象类是指具有 纯虚函数 的类,它只能作为 基类 来使用。二、简答题(共30分,每小题6分,其中4、5题任选其一作答即可)1 什么是类模板,类模板声明的一般形式是什么?答案:类模板是对一批仅仅成员数据类型不同的类的抽象,程序员只要为这一批类所组成的整个类家族创建一个类模板,给出一套程序代码,就可以用来生成多种具体的类,(这类可以看作是类模板的实例),从而大大提高编程的效率。定义类模

3、板的一般形式是:template 类型名参数名1,类型名参数名2,class 类名类声明体;2 什么是this指针?它的主要作用是什么? 答案:this指针:隐含在非静态成员函数中的特殊指针,它是当前正在调用此成员函数的 对象的指针。作用:主要是用于保证访问不能跨界和用于区分不同的对象。成员函数对成员变量 的引用实际是通过this指针访问的,也就是说:成员变量this-成员变量;另外, 如果成员函数需要访问当前对象,也可以通过this指针,*this就是当前对象。3 什么是多继承?多继承时,构造函数和析构函数执行顺序是怎样的?答案:多继承是指派生类具有多个基类,派生类与每个基类之间的关系仍可看

4、作是一个单 继承。派生类构造函数的执行顺序是先执行所有基类的构造函数(顺序按照定义派生类时 指定的各基类顺序),再执行派生类的构造函数,析构函数执行顺序,与构造函数完 全相反。4写出下面程序的运行结果答案:con structor B2.2con structor B1.1con structor B3.3con structor A.4124,3#in clude<iostream> using n amespace std;class B1int b1;public:B1(i nt i) b1 = i;cout <<"c on structor B1.&q

5、uot;<<i<<e ndl; void prin t() cout<<b1<<e ndl; ;class B2int b2;public:B2(i nt i) b2 = i;cout<<"co nstructor B2."<<i<<e ndl; void prin t() cout<<b2<<e ndl; ;class B3int b3;public:B3(i nt i) b3 = i; cout<<"c on structor B3.&quo

6、t;<<i<<e ndl;in t getb3() return b3; ;class A:public B2, B1int a; B3 bb;public:A(int i,int j, int k, int l):B1(i),bb(k),B2(j) a=l;cout<<"c on structor A."<<l<<e ndl; void prin t()B1:pri nt();B2:pri nt();cout<vavv","vvbb.getb3()vve ndl; ;int main()

7、A aa(1,2,3,4);aa.pri nt();return 0;5写出下面程序的运行结果答案:6外部静态对象g_sta_Obj构造7外部对象g_glb_Obj构造4 main动态分配对象 m_all_Obj构造4 ma in动态分配对象m_all_Obj5 main调用fun函数1 fun2 fun自动对象fun_Obj构造3 fun静态对象fun_sta_Obj构造2 fun自动对象fun_Obj析构4 main动态分配对象 m_all_Obj析构3 fun静态对象fun_sta_Obj析构#i nclude <iostream>#i nclude <stri ng.

8、h>using n amespace std;class A char stri ng80;public :void show();A(char * st);A();A:A(char * st) strcpy(stri ng, st);cout << stri ng << " 构造"<< en dl;A:A() cout << string << " 析构"<< endl; void A:show()cout << stri ng << en dl;vo

9、id fun()cout << "1 fun" << endl;A fun_Obj("2 fun自动对象 fun_Obj");static A fun_sta_Obj("3 fun静态对象 fun_sta_Obj"); 一一 一一void mai n()A *ptrA = new A("4 main动态分配对象 m_all_Obj");if(ptrA=NULL) retur n;ptrA->show();cout<<"5 main 调用 fun 函数"&

10、lt;< endl;fun();delete ptrA;static A g_sta_Obj("6 外部静态对象 g_sta_Obj");A g_glb_Obj("7外部对象 g_glb_Obj");6. 写出下面程序的输出结果答案:a = 2 , b = 4a = 2 , b = 6#i nclude <iostream> using n amespace std;class Apublic:virtual void Prin t(i nt a, int b=4) cout << "a = " <

11、< a << " , b = " << b << en dl; ;class B : public A public:virtual void Prin t(i nt a) cout << "a = " << a << en dl; virtual void Prin t(i nt a, double d) cout << "a = " << a << " , d = " << d <

12、< en dl; ;void Show(A * p)p -> Print( 2 );p -> Print( 2, 6.9 );void mai n()A * pa = new A;B * pb = new B;Show(pa);Show(pb);delete pa;delete pb;三、改错题(共25分,每小题5分)1 指出下面的代码中的一处错误,并更正后写出运行结果#i nclude <iostream>using n amespace std;template <type name T> class Aprivate:T x,y,s;public

13、:A(T a,T b) x=a,y=b;s=x+y;void show() cout<v"x+y="vvsvve ndl; ;void mai n()A add(1.5,5.3); add.show();答案:A <float> add(1.5,5.3);或 <i nt>x+y=6.82改正以下程序类定义部分中的错误。#i nclude <iostream>using n amespace std;class Pointprivate:int x,y;public:Poin t( int i,i nt j) x=i,y=j;void

14、 setxy(i nt i,i nt j) x=i;y=j; int getx()retur n x;int gety()return y;void mai n()Point pt;pt.setxy(10,10);cout<vpt.getx()v<e ndl;答案:Poi nt pt(1,1);103指出以下程序代码中的错误,并改正。#i nclude <iostream>using n amespace std;class Containerprotected:double area;public:virtual double GetArea()=0;class Sp

15、here:public Containerprivate:double r;public:Sphere(double a) r=a;virtual double GetArea()area = 4*3.14159*r*r;class Cube:public Containerprivate:double h;public:Cube(double a) h=a;virtual double GetArea()area = h*h*6;void mai n()Cube C(10), *ptr;Sphere S(10);ptr = &C;cout<<"Cube'

16、s area is "<<ptr->GetArea()<<e ndl;ptr = &S;cout«"Sphere's area is "<vptr->GetArea()v<e ndl;答案:virtual double GetArea()area = 4*3.14159*r*r; return area; virtual double GetArea()area = h*h*6; retur n area; ptr = (Cube*) &S;Cube's area is 60

17、0Sphere's area is 1256.644.指出以下程序代码中的错误,并改之。#i nclude <iostream>using n amespace std;class A1public:virtual cal();class A2: public A1void prin t();class A3: public A2public:virtual cal()cout<<"A3:cal()"<<e ndl;void mai n()A3 a3;a3.cal();答案:virtual cal()=0;A3:cal() 5.以

18、下程序可动态分配二维数组并释放存,且可实现二维指针和一维指针复用,试 问程序能否正常运行,如不能运行找出原因并改正之。#i nclude <iostream>#i nclude <stri ng.h>using n amespace std;void * fspace_2d(i nt row,i nt col,i nt le nth)char *p = (char *)calloc(le nth, row*col);void *b = (void *)calloc(sizeof(void *),row);for(int i=0;i<row;i+)bi = (voi

19、d*) (p + i*col*le nth);return(b);void ffree_2d(void *a,i nt row)for( int i=0;i<row;i+) free(ai);free(a);void mai n()in t r=5, c=10;float * pArray2D = (float *) fspace_2d( r, c, sizeof(float); ffree_2d( (void*) pArray2D, r);答案:编译可通过,运行存访问错。ffree_2d函数体改写为void ffree_2d(void *a, i nt row) _free(a0);free(a);四、写程序题(共30分)1、 建立一个字符串类用于存储和处理字符串,采用成员函数对操作符+'进行重 载,以实现两个字符串的合并功能。(10分)2、 写一个具有动态多态性的学生和教师信息输入和显示程序,学生数据有编号、 班号和成绩,教师数据有编号、职称和部门。要求将编号、输入和显示设计成一 个抽象类person的成员,并将person类作为学生数据操作类student和教师操作 类teacher的基类。程序应能根据待输入人员的类别完成对应人员信息的输入和显示功能。(20分)

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

当前位置:首页 > 社会民生


经营许可证编号:宁ICP备18001539号-1