一、判断题
1.In C++, inheritance allows a derived class to directly access all of the functions and data of its base class.
T F
解析:F。继承分为对于不同“等级”的成员的继承,诸如private等。所以在不同的继承等级面前,不一定能访问类中的所有的变量,因此为错误。
2.One class can have more than one super classes.
T F
解析:T。C++中允许一个类有多个父类。
二、填空题
1.write the output of the code below.
(1)the output at //1 is _______
(2)the output at //2 is _______
(3)the output at //3 is _______
(4)the output at //4 is _______
(5)the output at //5 is _______
#include <iostream>
#include <string>
using namespace std ;
class Testing
{
private:
string words;
int number ;
public:
Testing(const string & s = "Testing")
{
words = s ;
number = words.length();
if (words.compare("Testing")==0)
cout << 1;
else if (words.compare("Heap1")==0)
cout << 2;
else
cout << 3;
}
~Testing()
{
cout << 0;
}
void show() const
{
cout << number;
}
};
int main()
{
Testing *pc1 , *pc2;
pc1 = new Testing ; //1
pc2 = new Testing("Heap1"); //2
pc1->show(); //3
delete pc1 ; //4
delete pc2 ; //5
return 0;
}
解析:1、2、7、0、0.对于代码加以分析,我们可以按照main函数的过程来进行“运行”:创建了pc1和pc2两个对象,而由于其构造函数是有缺省值的,在没有给值时words默认为Testing,于是pc1的words为Testing,在构造函数中输出为“1”,pc2的words为Heap1,在构造函数中输出为“2”.而对于pc1执行show函数&#x