资源描述
《C++面向对象程序设计》习题解答
陈腊梅
目录
第一章 面向对象程序设计概述 错误!未定义书签。
第二章对C语言的基础扩充 错误!未定义书签。
第三章 类和对象的初步探讨 错误!未定义书签。
第四章 类和对象的进一步解析 错误!未定义书签。
第五章 在对象中使用运算符—运算符重载 错误!未定义书签。
第六章 继承与派生 2
第七章 多态性与虚函数 2
第一章 面向对象程序设计概述
一、选择题
1
2
3
4
5
6
7
8
9
10
11
12
A
D
D
D
C
D
C
C
A
A
C
C
二、填空题
1、多态性
2、成员函数
3、抽象
4、处理这些数据的操作或处理这些数据的函数
5、面向对象分析、面向对象分析、面向对象编程、对象对象测试、面向对象维护
第二章对C语言的基础扩充
一、 选择题
1
2
3
4
5
6
7
8
9
10
D
D
C
A
D
C
C
D
A
D
二、 程序阅读题
1答案
a=639,b=78,c=12
2答案
a=5,b=8
a=8,b=5
3答案
10
4答案
x=20.6 y=5 z=A
x=216.34 y=5 z=A
x=216.34 y=2 z=A
x=216.34 y=2 z=E
5答案
ic=11
fc=7.82
ic=5
fc=2.15
三、 判断下列描述的正确性
1
2
3
4
5
6
7
√
×
√
√
×
×
√
第三章 类和对象的初步探讨
一、选择题
1
2
3
4
5
6
C
D
B
B
A
D
二、填空题
1 数据成员 、 成员函数
2 private 、 protected 、 public
3 指向对象的指针、对象的引用
4 属性、行为、属性、行为
5 对象 、 属性和操作这些属性的操作 、 对外隐藏
四、程序改错题
#include <iostream>
using namespace std;
class Clock
{
//public:
void set_clock(void);
void show_clock(void);
//private:
int hour;
int minute;
int second;
};
Clock clock;
int main( )
{
clock.set_clock( );
clock.show_clock ( );
//return 0 ;
}
void Clock::set_clock(void)
{
cin>>hour;
cin>>minute;
cin>>second;
}
void Clock::show_clock(void)
{
cout<<hour<<":"<<minute<<":"<<second<<endl;
}
第四章 类和对象的进一步解析
一、选择题
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
D
B
B
C
D
D
D
C
B
D
B
A
A
C
B
A
17
18
19
20
21
22
23
24
C
C
D
B
A
D
B
B
二、填空题
1 this
2所有成员
3友元类、友元函数
4 friend
5 程序编译、程序结束
三、程序阅读题
1答案
Default constructor called.
constructor called.
0,0
6,8
6,8
Destructor called.
0,0
Destructor called.
2答案
x!=y
3答案
Initalizing default
Initalizing default
0 0
Destructor is active
Destructor is active
4答案
n=2,sum=2
n=3,sum=5
n=5,sum=10
5答案
x=3
6答案
x=1,y=2
x=30,y=40
7答案
1 2 3 4
4
exit main
3 2 1 0
8答案
n=100
9答案
the student is:Li Hu
the teacher is:Wang Ping
10答案
2
11答案
1035,789.504
12答案
13答案
constructing object:x=1
第五章 在对象中使用运算符—运算符重载
一、 选择题
1
2
3
4
5
6
C
D
B
D
D
A
二、 程序完善题
1(1)A A::operator+(A &b)
(2){ A temp;
temp.i = i+b.i;
return temp; }
三、 程序阅读题
1. 答案
n=4
n=3
23
2. 答案
c1=10 c2=20
c1=100 c2=200
c1=11 c2=21
c1=100 c2=200
3.答案
1 2 4 7 11 16
第六章 派生与继承
一、选择题
1(1)
1(2)
2
3
4
5
6
7
8
9
10
11
A
B
C
C
C
D
D
B
C
A
D
D
二、填空题
1 继承
2 具体化、抽象
3 公有继承、保护继承、私有继承
4 子对象
5 public(共有的)、protected(保护的)、不可访问
6 protected(保护的)、protected(保护的)、不可访问的
7 private(私有的)、private(私有的)、不可访问的
8 二义性
三、判断下列描述的正确性
1
2
3
4
5
6
7
8
9
10
11
12
13
14
√
×
×
×
×
×
√
√
×
×
√
√
×
√
1. C++语言中,既允许单继承,又允许多继承。(正确)
2. 派生类是从基类派生出来,它不能再生成新的派生类。(错误)
3. 派生类的继承方式有两种:公有继承和私有继承。(错误)
解析:还有保护继承
4. 在公有继承中,基类中的公有成员和私有成员在派生类中都是可见的。(错误)
解析:基类中的公有成员和保护成员在派生类都是可见的,基类私有成员在派生类中不可见。
5. 在公有继承中,基类中只有公有成员对派生类是可见的。(错误)
解析:保护成员可见
6. 在私有继承中,基类中只有公有成员对派生类是可见的。(错误)
解析:保护成员可见
7. 在私有继承中,基类中所有成员对派生类的对象都是不可见的。(正确)
解析:基类成员public,protected成员都变成派生类private成员
8. 在保护继承中,对于垂直访问同于公有继承,而对于水平访问同于私有继承。(正确?)
9. 派生类是它的基类组合。(错误)
解析:不仅是组合,还有扩充和完善
10. 构造函数可以被继承。(错误)
11. 析构函数不能被继承。(正确)
解析:构造函数、析构函数都不能继承
12. 多继承情况下,派生类的构造函数的执行顺序取决于定义派生类时所指定的各基类的顺序。(正确)
13. 单继承情况下,派生类中对基类成员的访问也会出现二义性。(错误)
解析:优先访问派生类
14. 解决多继承情况下出现的二义性的方法之一是使用成员名限定法。(正确)
解析:严格讲“作用域限定”
四、程序阅读题
1 答案
num: 10010
name: Wang-li
sex: f
age: 19
address: 115 Beijing Road,Shanghai
num: 10011
name: Zhang-fun
sex: m
age: 21
address: 213 Shanghai Road,Beijing
2答案
num:10010
name:Li
age: 17
score:89
3答案
A constructor called
A constructor called
B constructor called
(1,2)(1,1)(3,4)
(2,5)(1,1)(8,11)
destructor B called
destructor A called
destructor A called
4答案
construct M1.1
construct M2.2
construct M1.3
construct M2.4
construct N.3
1
2
3
desstruct N.3
desstruct M2.
desstruct M1.
desstruct M2.
desstruct M1.
5答案
调用基类2的构造函数!
调用基类1的构造函数!
调用派生类的构造函数!
调用派生类的析构函数!
调用基类1的析构函数!
调用基类2的析构函数!
6答案
x=100
y=300
y=200
y=300
7答案
0
400
20
8答案
(1,2)
5,6
(6,9)
9答案
(1,2)
(6,9)
5,6
(6,9)
10答案
{13,22,30,40}
11答案
D2::display( )
pri1=4,pri2=5
pri4=6
pri12=7
D2::display( )
pri1=12,pri2=9
pri4=7
pri12=8
12答案
A: :x = 10
B: :x= 20
B: : x = 20
20
B: : x = 20
13答案
D2::display( )
pri1=1,pri2=4
pri4=6
pri12=7
D2::display( )
pri1=9,pri2=8
pri4=7
pri12=8
五、程序完善题
1(1)protected (2)public (3)C(int x ,int y , int z):A(x),b1(y)
(4)c=z; (5)cout<<a<<”,”<<b1.b<<”,”<<c<<endl;
2(1)A(int x ){ a = x ;} (2)A1(int x , int y):A(y){ b = x ;}
(3)A3(int x, int y , int z ):A1(y,z){ c = x; }
(4)cout<<geta()<<endl;
cout<<geta()<<” ”<<getb()<<endl;
cout<<geta()<<” ”<<getb()<<” ”<<c<<endl;
3(1)Student( n , nam , s )
(2)age = a; (3) cout<<″num: ″<<num<<endl;
(4)10010,Wang-li,f,19 (5)stud1.show();
第七章 多态性与虚函数
一、选择题
1
2
3
4
5
6
7
8
9
10
11
12
B
D
D
C
B
A
C
C
D
B
D
D
二、填空题
1、不同
2、纯虚函数
3、静态多态性、动态多态性
4、virtual
5、抽象类
三、程序阅读题
1答案
D::show ( ) called ,8
B::show () called ,5
D::show ( ) called ,8
B::show () called ,5
2答案
executing Point destructor
3答案
executing Circle destructor
executing Point destructor
4答案
the B version 2336
the D1 info: 4 version 1
the D2 info : 100 version
the D3 info : -25 version 3
the B version 2336
the B version 23361
the B version 2336
the B version 23363
5答案
10209948820
6答案
f+ g0+
7答案
line :: draw ( ) called.
ellipse :: draw ( ) called.
line :: set ( ) called.
ellipse :: set ( ) called.
Redraw the object...
line :: draw ( ) called.
ellipse :: draw ( ) called.
8答案
line :: draw ( ) called.
ellipse :: draw ( ) called.
line :: set ( ) called.
ellipse :: set ( ) called.
Redraw the object...
line :: draw ( ) called.
ellipse :: draw ( ) called.
展开阅读全文