C++笔试题目-带答案_第1页
C++笔试题目-带答案_第2页
C++笔试题目-带答案_第3页
C++笔试题目-带答案_第4页
C++笔试题目-带答案_第5页
已阅读5页,还剩12页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、姓名: 时间:DCCBB AADAD一、选择题(1*10=10)1 .如果派生类以proctected 方式继承基类,则原基类的 protected 和public 成员在派生类的访问性分别是:DA. public 和 publicB. public 和 protectedC. protected 和 publicD. protected 和 protected解析:通过protected方式继承基类后,原基类的私有成员不可访问,而 protected 和 public 成员均变成 protected 成员。答案:D2. 有如下头文件:int F1();static int F2();clas

2、ss CApublic:int F3();static int F4();在所描述的函数中,具有隐含this指针的是:CA. F1B. F2C. F3D. F4本题考查的是this指针。this指针式一个隐含的指针,它隐含于每个类的非静态成员函数中,它明确地表示出了成员函数当前操作的数据所属的对象。当对一个对象调用成员函数时,编译程序先将对象的地址赋值给this指针,然后调用成员函数,每次成员函数存取数据成员时,则隐含使用this指针。this 指针是指向对象本身的指针,它只存在于类的非静态成员中。f1 , f2不是成员函数,不存在隐含指针;f4为静态成员函数,也不含有this指针;含 有th

3、is指针的函数在调用时按thiscall 调用约定调用。故本题答案为Co3. 派生类的成员函数不能访问基类的:CA.共有成员和保护成员B.共有成员C.私有成员D.保护成员本题考查的是继承的类型。类的继承方式有公有继承、保护继承和私有继承三种方式。对于公有继承基类中的成员访问属性不变,对于保护和私有继承基类中的成员转换为相应的访问类型。但是如果基类成员的访问属性为 private的,则不能被继承。故本题答案为Co4. 按照“后进先出”原则组织数据的数据结构是BA.队列B.栈C.双向链表答案为BoD.二叉树5.下列关于虚函数的说明中,正确的是: BA.从虚基类继承的函数都是虚函数B.虚函数不得是静

4、态成员函数C.只能通过指针或者引用调用虚函数D.抽象类中的中的成员函数都是虚函数。答案为Bo6 .已知Value是个类,value是Value的一个对象 载的运算符函数原型中,正确的是:AA. Value operator+(Value v, int i); v=value, int i);C. Value operator+(Value v, int=0); v=value, int i=0);7. 有如下类的定义:Class MyClassint value;public:MyClass(i nt n):value( n) int getValue() const return value

5、; ;则类Myclass的构造函数的个数是:AA. 1个C. 3个还有默认拷贝构造函数,应该选B8. 有如下类的定义:class Con sta ntsF列以非成员函数形式重B. Value operator+(ValueD. Value operator+(ValueB. 2个D. 4个public:static double GetPI(void)return 3.14159; ;Con sta nts con sta nts;下列各组语句中,能输出3.14159的是:bA. cout«constants->GetPI();和 cout<<Constants:G

6、etPI();B. cout«constants.GetPI();和 cout<<Constants.GetPI();C. cout«constants->GetPI();和 cout<<Constants->GetPI();D. cout«constants.GetPI();和 cout<<Constants:GetPI();9.有如下程序:#i nclude <iostream> using n amespace std;class VACpublic:int f() con stretur n 3;

7、int f()return 5; ;int main()VAC v1;const VAC v2;cout<<v1.f()vvv2.f();return 0;运行时的输出结果是:AA. 53B. 35C. 55D. 3310.有如下类声明:class Base protected:int amount;public:Base(i nt n = 0):am oun t( n)int getAm oun t() const retur n amoun t; ;class Derived:public Base protected:int value;public:Derived(i nt

8、 m, i nt n ):value(m),Base( n) int getData() const return value + amoun t;已知x是一个Derived对象,则下列表达式中正确的是:BA. x.value + x.getAmount();C. x.getData() -x.amount;B. x.getData() + x.getAmount();D. x.value + x.amount;Dog speak Voice二、填空题(8*2=16)400_ 6 4 4 4 4 return *this _1.下列中a的值是400.#defi ne AAA 200#defi

9、ne BBB AAA+100 int a= BBB*22. 以下为 Windows NT下的32位C+程序,请计算sizeof的值。 char str = Hello ” ;char *p = str ;int n = 10;请计算sizeof (str ) =5sizeof ( p ) =4sizeof ( n ) =4void Func ( char str1OO)/请计算sizeof( str ) =4void *p = malloc( 100 );/请计算sizeof ( p ) =43. 补充完整下面的类定义:class XCHchar* a;public:XCH(char* aa)

10、 /构造函数a=new charstrle n(aa)+1;strcpy(a,aa);XCH & operator=(co nst XCH & x) /重载赋值函数delete a;a=new charstrle n( x.a)+1;strcpy(a,x.a);JXCH()delete a;return *this4请写出下面程序的输出结果#in clude<iostream>using n amespace std;class Animal public:virtual char* getType() const return "An imal"

11、;virtual char* getVoice() const retur n "Voice" ;class Dog:public An imalpublic:char* getType() const retur n "Dog"char* getVoice() const return "Woof" ;void type(A ni mal& a) cout<<a.getType();void speak(A ni mal a) cout<<a.getVoice();int mai n()Dog d;t

12、ype(d);cout<<" speak "speak(d);cout<<e ndl; return 0;Dog speak Voice三、问答题(5*10+9+15=74)1.编写类String的拷贝构造函数和赋值函数 数)(15)。已知类String的原型为:class Stringpublic:Stri ng(co nst char *str = NULL);Strin g(c onst String &other); Strin g(void);String & operate =(c onst String &oth

13、er); private:char *m_data;(可以调用C+/C的字符串库函/普通构造函数/拷贝构造函数/析构函数/赋值函数/用于保存字符串请编写String的上述4个函数。/ String的析构函数/ 3Strin g:Stri ng(void)delete m_data;delete m_data;/由于m_data是内部数据类型,也可以写成 / Stri ng的普通构造函数Stri ng:Stri ng(co nst char *str) / 6String & String:operate =(const String &other) / 13/ 3/( 3)分配

14、新的内存资源,并复制内容if(str=NULL)m_data = new char1; /*m_data =0 'elseint len gth = strle n( str);m_data = new charle ngth+1; / strcpy(m_data, str);/拷贝构造函数String:String(const String &other) / 3 int len gth = strle n( other.m_data); m_data = new charle ngth+1;/strcpy(m_data, other.m_data);/赋值函数/ (1)检查

15、自赋值if(this = &other)return *this;/ (2) 释放原有的内存资源delete m_data;int len gth = strle n( other.m_data); m_data = new charle ngth+1;/strcpy(m_data, other.m_data);/( 4)返回本对象的引用return *this;若能加NULL判断则更好若能加NULL判断则更好分若能加NULL判断则更好 分/ 4分分/ 3 分若能加NULL判断则更好/ 3分2.不调用C+/C的字符串库函数,请编写函数strcmp 的实现(10) oint strcmp

16、 ( const char * src, const char * dst )char *strcpy(char *strDest, const char *strSrc) char *address = strDest;while (*strDest+ = * strSrc+)NULL ;retur n address ;3. F(n)=F(n-1)+F(n-2),F(0)=1,F(1)=1.分别用递归和循环求 F(5)(10)public int R(i nt nu m)if(nu m<=0) num=1;else if(num=1) num=1;else num=R( nu m-1

17、)+R (n um-2); return num; public int c(i nt num) int a = 1;int b = 1;int c = 0;for (i nt i = 0; i < num- 2; i+) c = a + b;a = b;b = c;return c;4. 写一算法,对单链表实现就地逆置(不要构造新结点)(10)node *reserve (no de*head)node*p1,*p2,*p3;if(head=NULL)|(head-> next=NULL)retur n head;p1=head; p2=p1- >n ext;while(p

18、2!=NULL)p3=p2->n ext;p2->n ext=p1;p1=p2;p2=p3;head-> next=NULL;p仁head;retur n head;5. 从冒泡排序、直接插入排序、二分插入排序和选择排序四种排序算法中,选 择其中一种算法,写出它的实现?(10)#i nclude<stdio.h>#in clude<time.h>#in clude<math.h>#i nclude<malloc.h>void BubbleSort(int *L,int N) /冒泡int i,j;int t;for(i=1;i&

19、lt;=N;i+)for(j=N;j>i;j-)if(Lj<Lj-1)t=Lj;Lj=Lj-1;Lj-1=t;int SelectMinKey(int *L,int N,int n)int i,m in=n;for(i=n+1;i<=N;i+)if(Li<Lmi n)min=i;return mi n;void SelectSort(i nt *L,i nt N) /选择int i,j;int t;for(i=1;i<N;i+)j=SelectMi nKey(L,N,i);if(i!=j)t=Li;Li=Lj;Lj=t;void InsertSort(int *L

20、,int N) /插入 int i,j;for(i=2;i<=N;i+)if(Li<Li-1)L0=Li;Li=Li-1;for(j=i-2;L0<Lj;j-)Lj+1=Lj;Lj+1=L0;10.1作了以下修改: 时,插入位置已找到。void ShellI nsert(i nt *L,i nt N, i nt dk) / 对顺序表L作一趟希尔插入排序。本算法对算法/ 1.前后记录位置的增量是dk,而不是1;/ 2. r0只是暂存单元,不是哨兵。当j<=0int i,j;for(i=dk+1;i<=N;+i)if(Li<Li-dk) / 需将L.ri插入有序

21、增量子表暂存在L.r0记录后移,查找插入位置 插入L0=Li;/for(j=i-dk;(j>0&&L0<Lj);j-=dk) Lj+dk=Lj;/Lj+dk=L0;/ / Shelll nsert void ShellSt(int *L,int N, int dlta, int t) / 算法 10.5/ 按增量序列dlta0.t-1对顺序表L作希尔排序。for(int k=O;k<t;+k)ShellInsert(L,N, dltak); /一趟增量为 dltak的插入排序 / ShellSortvoid ShellSort(i nt *L,i nt N)

22、/希尔int t=(i nt)log(N);int k,*dlta;dlta=(i nt*)malloc(t*4);/产生增量序列for(k=0;k<t;k+)dltak=(i nt)pow(2,t-k)-1;ShellSt(L,N,dlta,t);int main()int N=250;int i,j,k;int t;int ti16;int *L;sran d(time(NULL);printf(" 长度t|冒泡t|选择t|插入t|希尔n");printf(”+");for(j=0;N<100000;j+)L=(i nt *)malloc(N+1)

23、*4);t=0;for(i=1;i<=N;i+)Li=ra nd();tit+=clock();BubbleSort(L,N);tit+=clock();for(i=1;i<=N;i+)Li=ra nd();tit+=clock();SelectSort(L,N);tit+=clock();for(i=1;i<=N;i+)Li=ra nd();tit+=clock();In sertSort(L,N);tit+=clock();for(i=1;i<=N;i+)Li=ra nd();tit+=clock();ShellSort(L,N);tit+=clock();prin

24、 tf("n%dt",N); for(k=0;k<4;k+)prin tf("| %dt",(ti2*k+1-ti2*k);N*=5;prin tf("nn");6. 个类中,const类型成员函数的主要作用是什么?在该函数中可以调用该 类的什么类型的成员变量和成员函数?该类的一个非const对象可以调用const成员函数吗? (10)(1 )可以定义const常量,具有不可变性。(2)便于进行类型检查,使编译器对处理内容有更多了解,消除了一些隐患。例如:void f(const int i)编译器就会知道i是一个常量,不允许修改;(3 )可以避免意义模糊的数字出现,同样可以很方便地进行参数的调整和修改。同宏定义一样,可以做到不变则已,一变都变!如(1

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论