资源描述
华侨大学 面向对象程序设计( 二) 试卷( B)
系别 考试日期 06月27日
姓名 学号 成绩
一、 填空题( 15分, 每空1分)
1. 类的成员包括____________和_____________。
2. 建立一个类对象时, 系统自动调用____________。
3. 如果希望类的成员为该类所有对象所共享, 能够使用关键字________来修饰。
4. 如果希望完成所谓的深拷贝, 需要重载_______构造函数。
5. 类成员的访问控制包括_________、 _________和__________。
6. 不属于类成员但却能够访问类的私有数据变量的函数是该类的_________。
7. 运算符>>和<<重载只能经过__________函数实现。
8. 类定义中, 默认的访问控制是_______。
9. C++函数中参数值的传递方式有________、 __________、 _________。
10. 继承关系能够是public、 protected和________。
二、 选择题 ( 20分, 每小题2分)
1. 类名称不能以 开头。
A) 小写字母 B) 大写字母 C) 数字 D) 下划线
下列选项中, 不是C++关键字的是______。
A) class B) virtual C) public D) object
2. 对于类Base, 如果没有为其定义构造函数, 系统将自动为我们创立一个形为_____的默认构造函数。
A) Base(const Base&); B) Base(int x=0);
C) void Base(void); D) Base();
3. 对于类Base, 下列选项______是合法的析造函数。
A) void ~Base(){} B) ~Base(){} C) ~Base(int){} (D) Base ~Base(){}
4. 下列语句中, _______不能为我们定义一个常量。
A) #define PI 3.1415926 B) #define PI=3.1414926
C) const double PI=3.1415926; D) const static double PI=3.1415926;
5. 为了提高函数调用的实际运行速度, 能够将较简单的函数定义为______。
A) 递归函数 B) 友元函数 C) 内联函数 D) 成员函数
6. 下列运算符中, ______运算符不能被重载。
A) ?: B) + C) [ ] D) >>
7. 下列关于构造函数的描述中, 错误的是______。
A) 构造函数能够没有参数 B) 构造函数不能够设置默认参数
C) 构造函数能够是内联函数 D) 构造函数能够重载
8. 下面描述中, 表示错误的是_______。
A) 公有继承时基类中的public成员在派生类中仍是public的
B) 公有继承是基类中的private成员在派生类中仍是private的
C) 公有继承是基类中的protected成员在派生类中仍是protected的
D) 私有继承时基类中的public成员在派生类中是private的
9. 运算符重载是对已有的运算符赋予多重含义, 因此_______
A) 能够对基本类型( 如double类型) 的数据, 重新定义”+”运算符的含义
B) 能够改变一个已有运算符的优先级和操作数个数
C) C++中已经有的所有运算符都能够重载
D) 只能重载C++中已有的运算符, 不能定义新运算符
10. 已知类MyInt的定义如下:
class MyInt{
int data;
public:
MyInt(int d) { data = d; }
};
下列对MyInt类对象数组的定义和初始化语句中, 正确的是
A) MyInt myInts[3];
B) MyInt myInts[3] = {MyInt(2)};
C) MyInt myInts[3] = {MyInt(3), MyInt(4), MyInt(5)};
D) MyInt* myInts = new MyInt[3];
三、 阅读以下程序并填空( 填上正确的语法成分) , 使其成为完整的程序( 20分,每空2分)
(1). 已知向量MyVector的定义如下, data存放数据, capacity是当前分配的空间大小, length是data里实际存放的元素数目。( 1) 实现构造函数, 分配大小为n的空间, 并都初始化为0; ( 2) 实现析构函数, 释放分配的空间; ( 3) 重载流插入运算符<<, 将当前data的所有元素都依次打印出来, 格式如3 2 4 5。
class MyVector{
int *data; //指向存放数组数据的空间
int capacity; //当前分配的空间大小
int length; //当前实际的元素数目
public:
MyVector(int n);
~MyVector(){ delete ____(1)______; }
___(2)___ostream& operator<<(ostream& out, const MyVector& mv);
};
MyVector::MyVector(int n){ //实现构造函数
assert(n>0);
data = ______(3)______;
capacity = n;
length = 0;
for(int i = 0; i<n; i++) *(data+i) = 0;
}
ostream& operator<<(ostream& out, const MyVector& mv){//重载运算符<<
for( int i =0; __(4)__; i++)
out << ___(5)______ << ” ”;
out << endl;
return out;
}
(2). 类Derived公共继承于Base。Base的构造函数有一个参数i用于初始化其数据成员v。Derived的构造函数有三个参数val1,val2和val3, 分别用于初始化Base的数据成员v以及Derived的数据成员v1、 v2。
class Base{
int v;
public:
Base(int i):____ (6)______{}
};
class Derived:____(7)______ {
int v1,v2;
public:
Derived(int val1, int val2, int val3):____(8)_____, ____(9)_____,__ (10)______ {}
};
四、 读程序, 写出运行结果( 25分, 每题5分)
1. void f(int i){
static int calledTimes = 0;
cout << "No. " << ++calledTimes << " in f(" << i << ")" << endl;
}
int main(){
int i = 0;
for( int i = 0; i < 5; i++) f(i+1);
cout << "i = " << i << endl;
}
2. class Base{
public:
void print(){cout<<"In Base::print() "<< endl;};
}
class Derived:public Base{
public:
void print(){
Base::print();
cout<<"In Derived::print()"<< endl;
}
};
int main(){
Derived d;
d.print();
return 0;
}
3. class Base {
public:
Base(int i=0,int j=0) {
a=i;
b=j;
}
void print( ) { cout<<”a=”<<a<<”,b=”<<b<<endl;}
private :
int a,b;
};
void main( ){
Base m, n(4,8);
m.print( );
n.print( );
}
4. class MyClass{
public:
MyClass(){ cout << "MyClass()" << endl;}
MyClass(const MyClass& another){ cout << "MyClass(const MyClass& another()" << endl;}
MyClass& operator=(const MyClass& rhs){ cout << "operator=()" << endl; return *this;}
};
int main(){
MyClass mc1;
MyClass mc2 = mc1;
MyClass mc3(mc2);
mc1 = mc3;
return 0;
}
5. class Animal{
public:
Animal(){ cout << "Animal::Animal()" << endl; }
~Animal(){ cout << "Animal::~Animal()" << endl; }
};
class Cat:public Animal{
public:
Cat(){ cout << "Cat::Cat()" << endl;}
~Cat(){ cout << "Cat::~Cat()" << endl;}
};
int main(){
Cat animal;
}
五、 编程题( 共20分)
1. 请实现三个能够支持两个、 三个和n个整数相加的重载函数。函数名统一为add, 返回值统一为int, 例如两个整数相加的版本为 int add(int, int)。
2. 给定类IntegerNumber的定义如下, 要求实现如下五个运算符重载。
class IntegerNumber{
int value;
public:
IntegerNumber(int n = 0){ value = n; }
IntegerNumber operator+(const IntegerNumber& rhs);
IntegerNumber operator-(const IntegerNumber& rhs);
friend IntegerNumber operator++(IntegerNumber& a, int x); //后++
friend IntegerNumber& operator++(IntegerNumber& a); //前++
friend ostream& operator<<(ostream& out, IntegerNumber& rhs);
};
华侨大学 面向对象程序设计( 二) 试卷( B) 答题纸
系别 考试日期 06月27日
姓名 学号 成绩
题号
第一题
第二题
第三题
第四题
第五题
总分
成绩
阅卷人
一、 填空题 ( 15分, 每空1分)
1. 、 2. 3. 4.
5. 、 、 6. 7.
8. 9. 、 、 10.
二、 选择题 ( 20分, 每小题2分)
1. 2. 3. 4. 5.
6. 7. 8. 9. 10.
三、 阅读以下程序并填空( 填上正确的语法成分) , 使其成为完整的程序( 20分,每空2分)
1.______________________ 2.______________________
3.______________________ 4.______________________
5.______________________ 6.______________________
7.______________________ 8.______________________
9.______________________ 10._____________________
四、 读程序, 写出运行结果( 25分, 每题5分)
1. ___________________________________________________________
2. ___________________________________________________________
3. ___________________________________________________________
4. ___________________________________________________________
5. ___________________________________________________________
五、 编程题( 20分)
1.( 10分)
2.( 10分)
展开阅读全文