面向对象程序设计C++
2010-2011学年 春 季学期 末 考试B试卷答案及评分标准
一、单选题(45分,每题3分)
题号 答案 题号 答案 题号 答案 1 A 6 C 11 D 2 C 7 B 12 D 3 B 8 D 13 A 4 A 9 C 14 B 5 C 10 D 15 A 二、读程序写结果(28分,每题4分)
(1) 10,5 15,5 (2)
调用普通构造函数:4,8 调用复制构造函数:4,8 调用析构函数. 调用析构函数. (3)
创建点(1,1) 创建点(4,5) 两点的距离是:5 (4)
a=3,b=13 a=5,b=13 (5)
v1=(1,2) v2=(3,4)
v3=v1+v2=(4,6) (6)
调用基类Base的构造函数:1
调用派生类Derived的构造函数:2 (7)
7,14,21
第1页
三、编程题(27分)
1、 (14分)
class Complex (2分) {
private:
int real,image; public:
Complex(int x=0,int y=0); void show() const;
Complex operator+(const Complex& other); };
Complex::Complex(int x,int y) (4分,其中默认参数1分) {
real=x; image=y; }
void Complex::show() const (4分) { }
cout<<\输出复数:\
if(real==0&&image==0) cout<<0; if(real!=0) cout<
if(image!=1)
cout<<\ else
cout<<\}
else if(image<0) {
if(image!=-1)
cout< cout<<\} cout< Complex Complex::operator+(const Complex& other) (4分) { Complex temp; temp.real=real+other.real; 第2页 temp.image=image+other.image; return temp; } 2、(13分) class Date (2分) { private: int year,month,day; public: Date(int y,int m,int d); void show() const; }; Date::Date(int y,int m,int d) (4分) { year=y; month=m; day=d; } Date::Date(const Date& other) (4分) { year=other.year; month=other.month; day=other.day; } void Date::show() const (3分) { cout< 第3页