第四章——派生类和继承作业
一、单选题(共20题,100分)
1.有如下类定义:
class Father{ //基类
public:
Father(string s):name(s) { }
private:
string name;
};
class Mother{ //基类
public:
Mother(string s):name(s) { }
private:
string name;
};
【1】 //Father和Mother的派生类
public:
Child(string s1,string s2,string s3):Father(s1),Mother(s2),name(s3) { }
private:
string name;
};
若派生类Child从基类Father和基类Mother处公有继承,则程序中【1】处缺失的部分是( )。
A.class Child
B.class Child:Father,Mother
C.class Child:public Father,Mother
D.class Child:public Father,public Mother
我的答案: D正确答案: D
答案解析:一个派生类同时继承两个或者多个基类时称为多重继承,其一般形式为:
class 派生类名:[继承方式]基类名1,[继承方式]基类名2,所以D选项正确。
2.
有如下程序:
#include<iostream>
#include<string>
using namespace std;
class Wheel{
public:
Wheel(string s="W"):name(s) { cout<<name; }
~Wheel() { cout<<name; }
private:
string name;
};
class Bicycle{
public:
Bicycle(string br="G",string f="F",string r="R"):brand(br),rear(r),front(f) { cout<<brand; }
~Bicycle() { cout<<brand; }
private:
Wheel front,rear;
string brand;
};
int main(){
Bicycle bike;
return 0;
}
运行时的输出结果是( )。
A.RFG
B.FRG
C.FRGGRF
D.FRGFRG
我的答案: C正确答案: C
答案解析:定义Bicycle bike时,执行Wheel的构造函数输出FR,然后执行Bicycle的构造函数输出G,最后执行析构函数,一次执行派生类的析构函数输出G,再输出基类的析构函数输出RF,最终输出FRGGRF,选项C正确。
3.有如下程序:
#include <iostream>
using namespace std;
class Point{
int x,y;
public:
Point(int x1=0, int y1=0):x(x1),y(y1) {}
int get() {return x+y;}
};
class Circle{
Point center;
int radius;
public:
Circle(int cx, int cy, int r):center(cx,cy),radius(r) {}
int get() {return center.get()+radius;}
};
int main() {
Circle c(3,4,5);
cout<<c.get()<<endl;
return 0;
}
运行时的输出结果是( )。
A.5
B.7
C.12
D. 9
我的答案: C正确答案: C
答案解析:
本题考查构造函数,题目中定义了Circle类,在类体中又定义了Point的对象,执行Circle c(3,4,5);后输出三个数之和,所以是12,C选项正确。
4. (单选题)
有如下类定义:
class Cup{
public:
void SetPrice(double val=5.8);
double GetPrice() { return price; }
double GetPrice() const { return price; }
private:
double price;
<