杭州电子科技大学学生考试卷( B )卷
一. 是非题 (10分)
1. 在设置了参数默认值后,调用函数的对应实参就必须省略。
2. 构造函数和析构函数都不能重载。
3. 对象数组的元素可以是不同类的对象。
4. 在公有继承中,基类的公有成员和私有成员在派生类中都是可见的。
5. 类的多个实例共享静态数据成员。
二. 选择填空 (20分)
1. 下列的 ( ) 是引用调用。
A. 形参是指针,实参是地址值 B. 形参和实参都是变量
C. 形参是数组名,实参是数组名 D. 形参是引用,实参是变量
2. 通常拷贝构造函数的形参是 ( )。
A. 某个对象 B. 某个对象的成员
B. 某个对象的引用 D. 某个对象的指针
3. 下列对派生类的描述中, ( )是错的。
A. 一个派生类可以作为另一个派生类的基类
B. 派生类至少有一个基类
C. 派生类的对象除了它自己的成员外,还包含了它的基类的成员
D. 派生类中继承的基类成员的访问权限到派生类中保持不变
4. 对定义重载函数的下列要求中,( )是错误的
A. 要求参数的个数不同 B. 要求参数中至少有一个类型不同
C. 要求参数个数相同时,参数类型不同 D. 要求函数的返回值不同
5. 下列运算符中, ( ) 运算符不能重载
A. && B. [ ] C. :: D. new
6.拷贝(复制)构造函数的作用是
A.进行数据类型的转换 B.用对象调用成员函数
C.用对象初始化对象 D.用一般类型的数据初始化对象
7. 下列不是描述类的成员函数的是
A.构造函数 B.析构函数
C.友元函数 D.拷贝构造函数
8. 函数重载的目的在于 ( )。
A. 实现内存共享 B. 减少空间
C. 提高速度 D. 使用方便,提高可读性
9. 如果类A被说明成类B的友元,则
A.类A的成员即类B的成员 B.类B的成员即类A的成员
C.类A的成员函数不得访问类B的成员 D.类B不一定是类A的友元
10. 对于任何一个类,析构函数最多有 个
A. 0 B. 1 C. 2 D. n
三. 找出下面程序中的所有错误,并改正 (10分)
#include iostream.h
void Swap (int &a, int &b)
{
int & t;
t = a; a = b; b= t;
return t;
}
int main ()
{
int x = 3, y = 5;
Swap (&x, &y);
return 0;
}
四. 阅读下列程序,分别指出执行结果 (40分)
1. (10 %)
#include <iostream.h>
class CCat {
public :
CCat (int age = 1) : m_age (age) {
cout << “Cat constructor called\n”;
}
~CCat () {
cout << “Cat Destructor called\n”;
}
void GetAge (int &age) {
age = m_age;
}
void SetAge (age) {
m_age = age;
}
void Grow () {
m_age ++;
}
void Meow () {
cout << “Meow\n”;
}
private :
int m_age;
};
void main ()
{
cout << “start …\n”;
CCat frisky (5);
frisky . Meow ();
int age;
frisky. GetAge (age);
cout << “Frisky is a cat who is “ << age << “years old \n”;
frisky . Meow ();
frisky . Grow ();
frisky. GetAge (age);
cout << “Frisky is a cat who is “ << age << “years old \n”;
frisky . Meow ();
frisky . SetAge (10);
frisky. GetAge (age);
cout << “Frisky is a cat who is “ << age << “years old \n”;
cout << “end …\n”;
}
2. class CArray {
public :
CArray (int size = 1000) : m_size (size) {
m_pDatas = new int [m_size];
};
~CArray () {delete [] m_pDatas;}
int & operator [] (int index) {
if (index < 0 || index >= m_size)
throw "OverFlow";
return m_pDatas [index];
}
private:
int m_size;
int *m_pDatas;
};
void main () {
{ CArray A (8);
try {
for (int i= 0; ; i++ ) {
cout.width (5);
cout << i;
A [i] = i;
}
} catch (char * error) {
cout << error << endl;
} catch (…) {
}
}
3 .#include <iostream.h>
class AA {
public :
AA (int i, int j) {
A = i; B = j;
cout << “Constructor.\n”;
}
~AA () {
cout <<”Destructor.\n”;
}
void Print ();
private :
int A, B;
};
void A::Print ()
{
cout <<A<<”,”<<B<<endl;
}
void main ()
{
AA *a1, *a2;
a1 = new AA (1,2);
a2 = new AA (5, 6);
a1->Print ();
a2->Print ();
delete a1;
delete a2;
}
4.#include <iostream.h>
class CShape {
public :
void Move () {
cout << “Move Shape Here” << endl;
}
virtual void Draw () = 0;
};
class CRectangle : CShape {
public :
CRectangle (int width = 1, int length = 1) :
m_width (width), m_length (length) {}
void Move () {
cout << “Move Rectangle Here” << endl;
}
virtual void Draw ();
private :
int m_width, m_length;
};
void CRectangle::Draw ()
{
cout << "Draw a Rectangle Here!" << endl;
}
class CCircle : CShape {
public :
CCircle (int radius = 1) : m_radius (radius) {}
void Move () {
cout << “Move Circle Here” << endl;
}
virtual void Draw ();
private :
int m_radius;
};
void CCircle::Draw ()
{
cout << "Draw a Circle Here!" << endl;
}
void main ()
{
CShape *pShape [2];
pShape [0] = (CShape *) new CRectangle;
pShape [1] = (CShape *) new CCircle;
pShape [0]->Move ();
pShape [0]->Draw ();
pShape [1]->Move ();
pShape [1]->Draw ();
delete pShape [0];
delete pShape [1];
}
五. 程序设计 (20分)
1. 编写完成时间类CTime。(10分)
class CTime {
public:
CTime ();
CTime ( int hour, int minute, int second);
void Add (long seconds); //增加seconds秒
void Display (); //输出格式时:分:秒, 如 12:24:15
public :
int m_hour, m_minute, m_second;//时, 分, 秒
};
2. 下面是一个使用集合类CSet的程序实例及结果,请设计并完成集合类 (10分)
#include <iostream.h>
//集合代码…
void main ()
{
int X [] = {1, 3, 5, 7, 8, 9}, Y [] = { 2, 4, 6, 7, 8, 10, 11};
CSet A (X, 6), B (Y, 7); //构造初始集合
CSet C, D;
C = A.Union (B); //集合并
D = A.Join (B); //集合交
A.Print();
B.Print();
C.Print();
D.Print();
}
执行结果 :
{1, 3, 5, 7, 8, 9}
{2, 4, 6, 7, 8, 10, 11}
{1,3, 5, 7, 8, 9, 2, 4, 6, 10, 11}
{7, 8}