C++笔试题目-带答案
- 格式:doc
- 大小:97.00 KB
- 文档页数:14
姓名:___________ 时间:___________DCCBB AADAD一、选择题(1*10=10)1.如果派生类以proctected方式继承基类,则原基类的protected和public成员在派生类的访问性分别是:DA.public和public B.public和protectedC.protected和public D.protected和protected解析:通过protected方式继承基类后,原基类的私有成员不可访问,而protected 和public成员均变成protected成员。
答案:D2.有如下头文件:int F1();static int F2();classs CA{public:int F3();static int F4();};在所描述的函数中,具有隐含this指针的是:CA.F1 B.F2C.F3 D.F4本题考查的是this指针。
this指针式一个隐含的指针,它隐含于每个类的非静态成员函数中,它明确地表示出了成员函数当前操作的数据所属的对象。
当对一个对象调用成员函数时,编译程序先将对象的地址赋值给this指针,然后调用成员函数,每次成员函数存取数据成员时,则隐含使用this指针。
this指针是指向对象本身的指针,它只存在于类的非静态成员中。
f1,f2不是成员函数,不存在隐含指针;f4为静态成员函数,也不含有this指针;含有this 指针的函数在调用时按thiscall调用约定调用。
故本题答案为C。
3.派生类的成员函数不能访问基类的:CA.共有成员和保护成员B.共有成员C.私有成员D.保护成员本题考查的是继承的类型。
类的继承方式有公有继承、保护继承和私有继承三种方式。
对于公有继承基类中的成员访问属性不变,对于保护和私有继承基类中的成员转换为相应的访问类型。
但是如果基类成员的访问属性为private的,则不能被继承。
故本题答案为C。
4.按照“后进先出”原则组织数据的数据结构是BA.队列B.栈C.双向链表D.二叉树答案为B。
5.下列关于虚函数的说明中,正确的是:BA.从虚基类继承的函数都是虚函数B.虚函数不得是静态成员函数C.只能通过指针或者引用调用虚函数D.抽象类中的中的成员函数都是虚函数。
答案为B。
6.已知Value是个类,value是Value的一个对象。
下列以非成员函数形式重载的运算符函数原型中,正确的是:AA.Value operator+(Value v, int i); B.Value operator+(Value v=value, int i); C.Value operator+(Value v, int=0); D.Value operator+(Value v=value, inti=0);7.有如下类的定义:Class MyClass{int value;public:MyClass(int n):value(n){}int getValue() const {return value;}};则类Myclass的构造函数的个数是:AA.1个B.2个C.3个D.4个还有默认拷贝构造函数, 应该选B8.有如下类的定义:class Constants{public:static double GetPI(void){return 3.14159;}};Constants constants;下列各组语句中,能输出3.14159的是:BA.cout<<constants->GetPI();和cout<<Constants::GetPI();B.cout<<constants.GetPI();和cout<<Constants.GetPI();C.cout<<constants->GetPI();和cout<<Constants->GetPI();D.cout<<constants.GetPI();和cout<<Constants::GetPI();9.有如下程序:#include <iostream>using namespace std;class VAC{public:int f() const{return 3;}int f(){return 5;}};int main(){VAC v1;const VAC v2;cout<<v1.f()<<v2.f();return 0;}运行时的输出结果是:AA.53 B.35C.55 D.3310.有如下类声明:class Base{protected:int amount;public:Base(int n = 0):amount(n){}int getAmount() const {return amount;}};class Derived:public Base{protected:int value;public:Derived(int m, int n):value(m),Base(n){}int getData() const {return value + amount;}};已知x是一个Derived对象,则下列表达式中正确的是:B A.x.value + x.getAmount(); B.x.getData() + x.getAmount(); C.x.getData() – x.amount; D.x.value + x.amount;二、填空题(8*2=16)400_ 6 4 4 4 4 return *this _ Dog speak Voice1.下列中a的值是___400_____#define AAA 200#define BBB AAA+100int a= BBB*22. 以下为Windows NT下的32位C++程序,请计算sizeof的值。
char str[] = “Hello” ;char *p = str ;int n = 10;请计算sizeof (str ) = ____5_____sizeof ( p ) = ____4___sizeof ( n ) = ____4_ 2___void Func ( char str[100]){//请计算sizeof( str ) =___4____100_ _}void *p = malloc( 100 );//请计算sizeof ( p ) =_____4____3. 补充完整下面的类定义:class XCH{char* a;public:XCH(char* aa){ //构造函数a=new char[strlen(aa)+1];strcpy(a,aa);}XCH& operator=(const XCH& x){ //重载赋值函数delete []a;a=new char[strlen(x.a)+1];strcpy(a,x.a);______;}~XCH(){delete []a;}};____return *this ____________________________________________________________________ 4.请写出下面程序的输出结果#include<iostream>using namespace std;class Animal {public:virtual char* getType() const {return "Animal";}virtual char* getVoice() const {return "Voice";}};class Dog:public Animal{public:char* getType() const {return "Dog";}char* getVoice() const { return "Woof";}};void type(Animal& a) {cout<<a.getType();}void speak(Animal a) {cout<<a.getVoice();}int main(){Dog d;type(d);cout<<" speak ";speak(d);cout<<endl;return 0;}______ Dog speak Voice ______________________________三、问答题(5*10+9+15=74)1.编写类String的拷贝构造函数和赋值函数(可以调用C++/C的字符串库函数)(15)。
已知类String的原型为:class String{public:String(const char *str = NULL); // 普通构造函数String(const String &other); // 拷贝构造函数~ String(void); // 析构函数String & operate =(const String &other); // 赋值函数private:char *m_data; // 用于保存字符串};请编写String的上述4个函数。
// String的析构函数String::~String(void) // 3分{delete [] m_data;// 由于m_data是内部数据类型,也可以写成delete m_data;}// String的普通构造函数String::String(const char *str) // 6分{if(str==NULL){m_data = new char[1]; // 若能加NULL 判断则更好*m_data = ‘\0’;}else{int length = strlen(str);m_data = new char[length+1]; // 若能加NULL 判断则更好strcpy(m_data, str);}}// 拷贝构造函数String::String(const String &other) // 3分{int length = strlen(other.m_data);m_data = new char[length+1]; // 若能加NULL 判断则更好strcpy(m_data, other.m_data);}// 赋值函数String & String::operate =(const String &other) // 13分{// (1) 检查自赋值// 4分if(this == &other)return *this;// (2) 释放原有的内存资源// 3分delete [] m_data;// (3)分配新的内存资源,并复制内容// 3分int length = strlen(other.m_data);m_data = new char[length+1]; // 若能加NULL 判断则更好strcpy(m_data, other.m_data);// (4)返回本对象的引用// 3分return *this;}2.不调用C++/C的字符串库函数,请编写函数strcmp的实现(10)。