C++课后习题第四章

本文介绍了C++中派生类和继承的概念,通过多个选择题详细讲解了如何创建和使用派生类,包括构造函数的调用顺序、成员访问权限、多继承和虚继承等知识点。同时,每个题目都提供了答案解析,帮助读者深入理解C++的继承机制。
摘要由CSDN通过智能技术生成

第四章——派生类和继承作业

一、单选题(共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;

<
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

any88888888

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值