那位帮我朋友做套题,100分
1.编译器在选择调用哪个版本的重载函数时不以( )作为选择依据。
A.参数个数 B. 参数类型 C. 函数名字 D. 函数返回类型
2.在一个函数中,要求通过函数来实现一种不太复杂的功能,并且要求加快执行速度,此时可选用( )
A.内联函数 B. 重载函数 C.递归调用 D. 嵌套调用
3.( )不是构造函数的特征
A.构造函数的函数名与类名相同
B.构造函数可以重载
C.构造函数可以设置缺省参数
D.构造函数必须指定返回类型说明
4.关于虚函数的描述中.( )是正确的。
A.虚函数是一个static类型的成员函数
B.虚函数是一个非成员函数
C.基类中说明了虚函数后,派生类中将其对应的函数可不必说明为虚函数
D.派生类的虚函数与基类的虚函数具有不同的参数个数和类型
5.派生类的成员函数对它的基类中( )是不可以访问的。
A.公有继承的公有成员
B.公有继承的私有成员
C.公有继承的保护成员
D.私有继承的公有成员
6.在运行时确定调用那个函数的过程称为( )
A.静态联编 B.静止联编 C.动能联编 D.动态联编
7.下列有关抽象类的说法不正确的是( )
A.抽象是不能实例化的
B.抽象类只能做为基类使用
C.抽象类的子类也是抽象类
D.抽象类处于继承类层次结构的较上层
8.一个类的友元函数可以访问该类对象的( )
A.公有成员 B. 私有成员 C. 保护成员 D. 以上皆可
9.下列C++中关键字与继承无关的是( )
A.protectedB. privateC. classD. continue
10.有关析构函数说法正确的是( )
A.析构函数能被继承
B.析构函数名与类名相同
C.定义析构函数时可以指定返回类型为void
D.析构函数在对象生存期结束时被自动调用
三、下面的题目中定义了两个类以及main函数。请写出程序的输出结果并分析结果的正确性。如果有错,如何改正?(10分)
#include <iostream>
using namespace std;
class Instrument
{
public:
Instrument() { cout < < "Constructor of Instrument " < < endl; }
~ Instrument() { cout < < "Destructor of Instrument " < < endl; }
virtual void Play() { cout < < "Ding ding ding… " < < endl; }
};
class Piano : public Instrument
{
public:
Piano() { cout < < "Constructor of Piano " < < endl; }
~ Piano() { cout < < "Destructor of Piano " < < endl; }
void Play() { cout < < "Bang bang bang… " < < endl; }
};
void main()
{
Instrument * p = new Piano();
p-> Play();
delete p;
}
输出结果:
四、下面程序有7行有效代码。请指出其中的错误并加以改正。(10分)
int swap(int x, int y) (1)
{
char t = x; (2)
x = y; y = x;(3)
}
void main()(4)
{
int a = 0, b = 3;(5)
swap(a, b);(6)
return 0;(7)
}
五、请编写一个完整的类来描述二维向量。重载运算符+来求两个向量的和向量,重载运算符*来求两个向量的点积。(注意:两个向量的点积是个实数)(20分)
六、现有一些概念:交通工具、陆上交通工具、飞行器、水上交通工具、汽车、飞机、热气球、船等。请确定它们之间的关系,画出关系图,然后使用继承技术,编写8个类来实现这种关系。(20分)
[解决办法]
1d
2a
3d
4c
5c
6d?
7c
8d
9d
10d
------解决方案--------------------
5b 其余正确.
三输出结果是:自己上机试,原因我也在等
四int swap(int x, int y){char t = x; 中改为int swap(int &x, int &y)){int t = x;
其余太多自己做
[解决办法]
1d
2a
3d
4c
5b
6d
7c
8d
9d
10d
1
Constructor of Instrument
Constructor of Piano
Bang bang bang…
Destructor of Instrument
若果说有错的话也就是吧析构函数改成virtual的,输出结果就会是
Constructor of Instrument
Constructor of Piano
Bang bang bang…
Destructor of Piano
Destructor of Instrument
四
swap应该没有返回值,两个参数应该是引用类型(2个错)
swap的临时变量应该用int,否则会造成数据丢失
交换的最后一步应该是y=t,而不是y=x
main被定义为void,不应该有返回值
五
class Vec
{
public:
Vec(float x,float y)
{
a=x,b=y;
}
public:
float a,b;
}
Vec& operator+(const Vec& a,const Vec& b)
{
Vec c(a.a+b.a,a.b+b.b);
return c;
}
float operator*(const Vec& a,const Vec& b)
{
return a.a*b.a+a.b*b.b;
}
六....