一、选择题(每题3分)
1.关于虚函数,正确的描述是()
A 构造函数不能是虚函数
B 析构函数不能是虚函数
C 虚函数可以是友元函数
D 虚函数可以是静态成员函数
2.在32位操作系统g++编译器环境下,下面程序的运行结果为()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#include <iostream> using namespace std; class A { public: int b; char c; virtual void print() { cout << "this is father’s fuction! " << endl; } }; class B: public A { public: virtual void print() { cout << "this is children’s fuction! " << endl; } }; int main(int argc, char * argv[]) { cout << sizeof(A) << "" << sizeof(B) << endl; return 0; } |
A 12 12
B 8 8
C 9 9
D 12 16
3. 下面描述正确的是
1 2 |
int *p1 = new int[10]; int *p2 = new int[10](); |
正确答案:
A p1和p2申请的空间里面的值都是随机值
B p1和p2申请的空间里的值都已经初始化
C p1申请的空间里的值是随机值,p2申请的空间里的值已经初始化,都初始化 0
D p1申请的空间里的值已经初始化,p2申请的空间里的值是随机值
4.若char是一字节,int是4字节,指针类型是4字节,代码如下:
1 2 3 4 5 6 7 8 9 10 11 13 |
class CTest { public: CTest():m_chData(‘\0’),m_nData(0) { } virtual void mem_fun(){} private: char m_chData; int m_nData; static char s_chData; }; char CTest::s_chData=’\0’; |
问:
(1)若按4字节对齐sizeof(CTest)的值是多少?
(2)若按1字节对齐sizeof(CTest)的值是多少?
请选择正确的答案。正确答案:
16 4
16 10
12 9
10 10
5.已知obj是一个对象,下面哪一个表达式是不可能出现的?()
[A] obj.100 [B] !obj [C] obj++ [D] obj,100 。
6.下述程序中,类C的public各有哪些属性成员( )。
class B1
{
public:
int i ;
protected :
int k ;
};
class B2
{
public:
int l ;
private:
int m;
protected :
int q ;
};
class B3
{
public:
int p1;
};
class C: public B2, protected B1, private B3
{
public:
int c;
};
[A] i/l/p1 [B] c/l [C] c/l/q [D] i/l/q
7.下述程序运行后,显示结果为( )。
class B1
{
public:
B1(int i) {cout<<"constructing B1 "<<i<<endl;}
~B1( ) {cout<<"destructing B1 "<<endl;}
};
class B2
{
public:
B2(int j) {cout<<"constructing B2 "<<j<<endl;}
~B2( ) {cout<<"destructing B2 "<<endl;}
};
class B3
{
public:
B3( ){cout<<"constructing B3 *"<<endl;}
~B3( ) {cout<<"destructing B3 "<<endl;}
};
class C: public B2, public B1, public B3
{
public:
C(int a, int b, int c, int d):
B1(a),memberB2(d),memberB1(c),B2(b){}
private:
B1 memberB1;
B2 memberB2;
B3 memberB3;
};
void main( )
{ C obj(1,2,3,4); }
[A] constructing B2 2 [B] constructing B1 1
constructing B1 1 constructing B2 2
constructing B3 * constructing B3 *
constructing B1 3 constructing B1 3
constructing B2 4 constructing B2 4
constructing B3 * constructing B3 *
destructing B3 destructing B3
destructing B2 destructing B2
destructing B1 destructing B1
destructing B3 destructing B3
destructing B1 destructing B1
destructing B2 destructing B2
[C] constructing B2 2 [D] constructing B2 2
constructing B1 1 constructing B1 1
constructing B3 * constructing B3 *
constructing B1 3 constructing B3 *
constructing B2 4 constructing B1 3
constructing B3 * constructing B2 4
destructing B3 destructing B3
destructing B2 destructing B2
destructing B1 destructing B1
destructing B3 destructing B3
destructing B2 destructing B1
destructing B1 destructing B2
8.下列关于C++中类的析构函数,说法正确的是( )
[A] 析构函数必须自己实现
[B] 析构函数不能自己实现
[C] 析构函数不需要明确调用,系统会自动调用
[D] 析构函数的调用顺序和构造函数的调用顺序相同
9.如果派生类以proctected方式继承基类,则原基类的protected和public成员在派生类的访问性分别是( )
[A] public和public
[B] public和protected
[C] protected和public
[D] protected和protected
10.有如下头文件:
int F1();//普通的全局函数
static int F2();//静态函数
class CA
{
public:
int F3();//普通的成员函数
static int F4();//静态成员函数
};
在所描述的函数中,具有隐含this指针的是( )
[A] F1 [B] F2
[C] F3 [D] F4
11.已知Value是个类,value是Value的一个对象。下列以非成员函数形式重载的运算符函数原型中,正确的是( )
[A] Value operator+(Value v, int i); [B] Value operator+(Value v=value, int i);
[C] Value operator+(Value v, int i=0); [D] Value operator+(Value v=value, int i=0);
12.有如下类的定义:
Class MyClass
{
int value;
public:
MyClass(int n):value(n){}
int getValue() const {return value;}
};
则类Myclass的构造函数的个数是:( )
[A] 1个 [B] 2个
[C] 3个 [D] 4个
13. 有如下类的定义:
class Constants
{
public:
static double GetPI(void){return 3.14159;}
};
Constants constants;
下列各组语句中,能输出3.14159的是( )
[A] 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();
14. #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;
}
运行时的输出结果是( )
[A] 53 [B] 35
[C] 55 [D] 33
15. 下面说法错误的是:()
- 在C++中多态的实现是通过虚函数表实现的。
- 当类中仅含有虚析构函数,不含有其它虚函数时,不产生虚函数表
- 每个类只有一份虚函数表,所有该类的对象共用同一张虚函数表
- 两张虚函数表中的函数指针可能指向同一个函数
- 执行语句A * p=new A[100];时,类A的构造函数会被调用100次。 //
- 一个类中的成员函数重载(overload)可以用const修饰符、函数原型的参数个数、对应位置的参数的类型和返回值类型为依据。//
-
实例化派生类对象时,一定会调用到基类的某一个构造函数。//
-
如果类A是类B的友元,类D以public继承方式从类B继承,则类A也是类D的友元。//
-
在同一个类中,可以定义重载的成员函数 void f(int& anInt);和void f(int anInt);。//
- 在public继承方式下,基类中被说明为protected和private的成员只能被其派生类的成员函数访问,不能被其它的函数访问。//
- C++中标识符内的大小写字母是没有区别的。//
1.题:若char是1字节 int是4字节 指针是4字节
class CTest
{
public:
CTest():m_chData('\0'),m_nData(0)
{
cout<<"11"<<endl;
}
private:
char m_chData;
int m_nData;
static int a;
};
int CTest::a = 10;
问:
1)若按4字节对齐,sizeof(CTest)值是多少
2)若按1字节对齐,sizeof(CTest)值是多少
2.题:设已经有A B C D四个类的定义,程序中A B C D析构函数的调用顺序为?
C c;
void main()
{
A* pa = new A();
B b;
static D d;
delete pa;
}
答:构造C A B D
析构 A B D C
3.题:
#include <iostream>
using namespace std;
class MyClass
{
public:
MyClass(int i = 0)
{
cout<<i;
}
MyClass(const MyClass& other)//同类型的一个对象初始化另一个对象时调用
{
cout<<2;
}
MyClass &operator=(const MyClass& other)//给对象赋值时调用
{
cout<<3;
return *this;
}
~MyClass()//删除对象时调用
{
cout<<4;
}
};
int main()
{
MyClass obj1(1), obj(2);//
MyClass obj3 = obj1;//
return 0;
}
程序运行结果:
答:
4.题:
#include <iostream>
using namespace std;
class A
{
private:
int value;
public:
A(int n)
{
value = n;
}
A(A other)
{
value = other.value;
}
void Print()
{
std::cout << value << std::endl;
}
};
int main()
{
A a = 10;//隐式转换 调用只有一个int类型参数的构造函数
A b = a;//企图调用拷贝构造函数
b.Print();
return 0;
}
以上代码,分析编译运行的结果
答:
5.题:
#pragma pack(2)
class BU
{
int number;
union UBffer
{
char buffer[13];
int number;
}ubuf;
void foo(){}
typedef char*(*f)(void*);
enum{hdd,ssd,blueray}disk;
}bu;
sizeof(bu)值是多少
答:
6.题:简述引用和指针的不同
答:
7.题:简述对多态的理解
答:
编写程序,编写类String的构造函数、析构函数和赋值函数。