虚函数--交通工具(C++)

【问题描述】

定义一个交通工具vehicle,将他作为基类派生校车类car,卡车类truck和轮船类boat,定义类并定义虚函数来显示各类信息

#include  <iostream>

using namespace std;

class vehicle {
protected:
    double speed;        //速度,公里/小时
    int wheels;          //轮子数
    double weight;  //重量
public:
    vehicle(double speed = 80, int wheels = 4, double weight = 1000);

    virtual void show(void) = 0;
};

vehicle::vehicle(double speed, int wheels, double weight) {
    this->speed = speed;
    this->wheels = wheels;
    this->weight = weight;
}


class car : public vehicle {
    int passenger_load;
public:
    car(double speed = 80, int wheels = 4, double weight = 1000, int passenger_load = 4);
    virtual void show(void);
};

void car::show(void) {
    cout << "Car  message\n";
    cout << speed << " " << wheels << " " << weight << " " << passenger_load << endl;
}

car::car(double speed, int wheels, double weight, int passenger_load) {
    this->speed = speed;
    this->wheels = wheels;
    this->weight = weight;
    this->passenger_load = passenger_load;
}


class truck : public vehicle {
    double rated_load;      //额定载重
public:
    truck(double speed = 80, int wheels = 4, double weight = 2500, double rated_load = 3000);

    virtual void show(void);
};

truck::truck(double speed, int wheels, double weight, double rated_load) {
    this->speed = speed;
    this->wheels = wheels;
    this->weight = weight;
    this->rated_load = rated_load;
}

void truck::show(void) {
    cout << "truck message\n";
    cout << speed << " " << wheels << " " << weight << " " << rated_load << endl;
}


class boat : public vehicle {
    char kind;              //轮船类别,如客轮为'k'
public:
    boat(double speed = 30, int wheels = 0, double weight = 12000, char kind = 'k');

    virtual void show(void);
};

boat::boat(double speed, int wheels, double weight, char kind) {
    this->speed = speed;
    this->wheels = wheels;
    this->weight = weight;
    this->kind = kind;
}

void boat::show(void) {
    cout << "boat message\n";
    cout << speed << " " << wheels << " " << weight << " " << kind << endl;
}


int main() {
    vehicle *unicycle;
    car *BMW;
    unicycle = new car;
    unicycle->show();
    BMW = (car *) unicycle;
    BMW->show();
    delete unicycle;
    unicycle = new truck;
    unicycle->show();
    delete unicycle;
    unicycle = new boat;
    unicycle->show();
    delete unicycle;
    return 0;
}  

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Beep函数是一个Windows API函数,用于在计算机上发出声音。它的原型为:WINBASEAPI WINBOOL WINAPI Beep (DWORD dwFreq, DWORD dwDuration)。\[1\]该函数接受两个参数,dwFreq表示要发出的声音的频率,dwDuration表示声音的持续时间。通过调用Beep函数,可以在程序中产生不同频率和持续时间的声音效果。 在引用\[2\]的代码示例中,可以看到Beep函数被用来产生一段音乐。通过调用Beep函数并传入不同的频率和持续时间参数,可以实现不同音符的发声效果。这段代码使用了一系列的Beep函数调用来演奏了一首曲子。 除了Beep函数,Windows.h头文件中还定义了其他一些函数,比如MessageBox函数。MessageBox函数用于在窗口中显示一个消息框,可以用来向用户显示一些提示信息或者询问用户是否执行某个操作。\[3\]该函数也接受多个参数,包括消息框的标题、消息内容和按钮样式等。 总结起来,Beep函数是一个用于在计算机上发出声音的函数,可以通过调用该函数并传入不同的参数来产生不同的声音效果。而MessageBox函数则是用于在窗口中显示消息框的函数,可以用来向用户显示提示信息或者询问用户是否执行某个操作。 #### 引用[.reference_title] - *1* *2* *3* [C++ Beep、MessageBox函数详解](https://blog.csdn.net/Dpi168/article/details/110731492)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

*OASIS*

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

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

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

打赏作者

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

抵扣说明:

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

余额充值