哈工大 2004 年 秋 季学期 班号 学号 姓名 五 总分 实验 平时总成绩 《C++程序设计》试 题号 一 二 题(A卷) 四 考试时间 120 分钟 满分 70 三 (20) (10) 分数
一、 填空题。(共15分,每空1分)
1. 面向对象的程序设计方法(可用各种语言实现,如C++、Java、C#等)具有三大特性,它
们分别是 , , 。
2. 已知m = 5, 执行m++;后,表达式的值是 ,m的值是 ,执行++m;后,表达式的值是 ,m的值是 。
3. 已知m = 4, n = 5,则执行语句m > n ? m++ : ++n,m++,m+n;后,表达式的值是 。 4. 循环语句while(int i = 1) { i--; } 执行的循环次数是 次;语句
for(int i(0), j(0); i = j = 0; i++, j--) { ; } 执行的循环次数是 次。 5. 为了使函数能够返回两个或者更多的值,可将形参设置为下列两种方式,它们分别 是 或 ,如果希望实参不被修改,那么需要对形参使用关键字 限制。 6.定义在类体外的函数,如果希望成为内联函数,则必须在类体内的声明中用关键 字 标识;运算符的重载必须用关键字 标识。
二、 判断题,正确的请打√,错误的请打×。(共20分,每题1分)
1. 编译C++源程序的过程中,出现了警告错误,也可以生成可执行文件。( ) 2. C++中,使用#define定义的常量必须给出类型说明,而使用const给出的常量不用给出类型说明。( )
3. 表达式的类型只取决于运算符,与操作数无关。( )
4. break语句可以出现在各种不同循环语句的循环体中,continue语句只能出现在循环体中。( )
5. 函数没有返回值时,可以不加任何类型的说明符;任何一个函数都可以设置默认参数,但是不允许将一个函数的所有参数设置为默认参数。( )
6. 指向函数的指针可以作为函数参数;使用数组名作函数参数时,实参数组与形参数组是共享内存单元的。( )
7. 结构与联合的差别在于结构中的每个成员都有自己的内存地址,且都是不相同的,而联合中的所有成员是共享内存块的,且内存块的大小与成员中的最大内存块相同。( )
《C++ 程序设计-A》(2003级数学系) 班级 学号 姓名 Page 1 of 7
8. 如果类的一个成员函数只存取该类的静态数据成员,可将该成员函数说明为静态成员函数;友元类的所有成员函数都可以对该类中的私有成员进行存取操作。( )
9. 类型转换函数是类的成员函数,它是用来进行强制类型转换的,它不能定义为友元函数,也不能定义为静态的成员函数;带有一个参数的构造函数具有类型转换的作用。( )
10. 在公有继承中,派生类的对象不可以访问基类中的保护成员;在私有继承中,派生类的对象不可以访问基类中的公有成员;在保护继承中,派生类可以访问基类中的保护成员。( )
三、 请分析下列程序,并回答问题。(共30分)
(3分) 阅读下列程序,给出正确的输出结果。(6分) 2、阅读下列程序,给出正确的输出结果。 1、#include
void term_func() { int i = 10, j = 20; { cout<<\ cout<<\
cout<<\ cout<<\ swap(i, j); exit( -1 ); cout<<\}
cout<<\int main() return 0; { } try {
set_terminate( term_func ); void swap(int v1, int& v2) throw \ { }
int tmp = v2; catch( int ) { v2 = v1; cout << \整数异常.\ v1 = tmp; }
return 0; } }
答:1、输出结果如下: 答:2、输出结果如下:
3、阅读下列程序,给出正确的输出结果。(9分) // 第3题(续)。 #include
答:4、输出结果如下: 四、 阅读下列文档,并回答提出的问题。(共25分)。
1、根据下列文档说明,判断所给出的几个问题。用“×”或者“√”在问题后的括号中标出。(6分)
The struct keyword defines a structure type and/or a variable of a structure type. A structure type is a user-defined composite type. It is composed of \structure is the same as a class except that its members are public by default. In C, you must explicitly use the struct keyword to declare a structure. In C++, this is unnecessary once the type has been defined. You have the option of declaring variables when the structure type is defined by placing one or more comma-separated variable names between the closing brace and the semicolon.
1 用struct关键字说明的类型只能由相同的数据类型组成。 ( ) ○
2 struct类型中的所有成员都是作为私有访问类型的。 ( ) ○
3 在C语言中定义结构变量时,○必须显示地给出关键字struct;而在C++中,则可以省略关键字。( ) 2、阅读下列说明文档,回答后面的问题。(4分)
Virtual base classes offer a way to save space and avoid ambiguities in class hierarchies that use multiple inheritance. Each nonvirtual object contains a copy of the data members defined in the base class. This duplication wastes space and requires you to specify which copy of the base class members you want whenever you access them. When a base class is specified as a virtual base, it can act as an indirect base more than once without duplication of its data members. A single copy of its data members is shared by all the base classes that use it as a virtual base. The virtual keyword appears in the base lists of the derived classes, not the base class.
《C++ 程序设计-A》(2003级数学系) 班级 学号 姓名 Page 4 of 7
1 为什么要提出虚基类的概念,它有什么好处? ○
答: 2 如何定义虚基类呢? ○ 答: 3、阅读下列说明文档,回答后面的问题。(15分)
“Virtual functions” are functions that ensure that the correct function is called for an object, regardless of the expression used to make the function call. Suppose a base class contains a function declared as virtual and a derived class defines the same function. The function from the derived class is invoked for objects of the derived class, even if it is called using a pointer or reference to the base class. Functions in derived classes override virtual functions in base classes only if their type is the same. A function in a derived class cannot differ from a virtual function in a base class in its return type only; the argument list must differ as well.
When calling a function using pointers or references, the following rules apply:
? A call to a virtual function is resolved according to the underlying type of object for which it is called. ? A call to a nonvirtual function is resolved according to the type of the pointer or reference.
virtual functions are called only for objects of class types, you cannot declare global or static functions as virtual. The virtual keyword can be used when declaring overriding functions in a derived class, but it is unnecessary; overrides of virtual functions are always virtual. Virtual functions in a base class must be defined unless they are declared using the pure-specifier. The virtual function-call mechanism can be suppressed by explicitly qualifying the function name using the scope-resolution operator (::).
Abstract classes cannot be used for: variables or member data、 argument types、 function return types and types of explicit conversions. Another restriction is that if the constructor for an abstract class calls a pure virtual function, either directly or indirectly, the result is undefined. However, constructors and destructors for abstract classes can call other member functions. ~Derived() 1 根据你的理解,给出下列程序的输出结果。 ○ { cout<<\ } void NameOf(); #include
哈工大2004秋《C++ 程序设计》考试题-A



