lpdc99 2022-06-01 13:39 采纳率: 75%
浏览 33
已结题

设计一个不同的交通工具类族

一个汽车类vehicle,包含保护类型数据成员重量weight和车轮数量wheel,包含公有成员函数void set(int we,int wh)分别设置重量和车轮数量,公有成员函数void display()输出汽车的重量和车轮数量。
由类vehicle派生出类car和类truck,类Car增加保护类型数据成员载客数量busload,增加公有成员函数void set1(int m)设置载客数量,重定义公有成员函数void display()显示轿车的重量、车轮数量和载客数量。
类truck增加保护类型数据成员载重量deadweight,增加公有成员函数void set2(int m)设置载重量,定义公有成员函数void display()显示卡车的重量、车轮数量和载重量。然后编写主程序测试这些类。以上所有数据均是整数。
输入输出样例:
Input the weight:2
Input the wheel:4
Input the the busload:40
Input the deadweight:10
The vehicle:
The weight is 2
The wheel is 4
The car:
The weight is 2
The wheel is 4
The busload is 40
The truck:
The weight is 2
The wheel is 4
The deadweight is 10

参考程序模板
#include <iostream>
using namespace std;

//补充代实现类vehicle  的定义

        cout << "The weight is " << weight << endl;
        cout << "The wheel is " << wheel << endl;

//补充代码实现类car 的定义
        cout << "The weight is " << weight << endl;
        cout << "The wheel is " << wheel << endl;
        cout << "The busload is " << busload << endl;

    //补充代码实现类truck 的定义

        cout << "The weight is " << weight << endl;
        cout << "The wheel is " << wheel << endl;
        cout << "The deadweight is " << deadweight << endl;

int main(){
    vehicle v1;
    car car1;
    truck truck1;
    int weight, wheel, busload, deadweight;
    cout << "Input the weight:" ;
    cin >> weight;
    cout << "Input the wheel:" ;
    cin >> wheel;
    cout << "Input the the busload:" ;
    cin >> busload;
    cout << "Input the deadweight:" ;
    cin >> deadweight;
    v1.set(weight, wheel);
    car1.set1(weight, wheel, busload);
    truck1.set2(weight, wheel, deadweight);
    cout<<"The vehicle:"<<endl;
    v1.display();
    cout<<"The car:"<<endl;
    car1.display();
    cout<<"The truck:"<<endl;
    truck1.display();
    return 0;
}

  • 写回答

1条回答 默认 最新

  • ⁢zzkk 2022-06-02 16:58
    关注
    #include <iostream>
    using namespace std;
    
    class vehicle {
    protected:
        int weight;
        int wheel;
    public:
        void set(int we, int wh) {
            weight = we; wheel = wh;
        }
        virtual void display() {
            cout << "The weight is " << weight << endl;
            cout << "The wheel is " << wheel << endl;
        }
    };
    
    class car : public vehicle {
    protected:
        int busload;
    public:
        void set1(int we, int wh, int m) { busload = m; set(we, wh); }
        void display() {
            cout << "The weight is " << weight << endl;
            cout << "The wheel is " << wheel << endl;
            cout << "The busload is " << busload << endl;
        }
    };
    
    class truck : public vehicle {
    protected:
        int deadweight;
    public:
        void set2(int we, int wh, int m) { deadweight = m; set(we, wh); }
        void display() {
            cout << "The weight is " << weight << endl;
            cout << "The wheel is " << wheel << endl;
            cout << "The deadweight is " << deadweight << endl;
        }
    };
    
    int main() {
        vehicle v1;
        car car1;
        truck truck1;
        int weight, wheel, busload, deadweight;
        cout << "Input the weight:";
        cin >> weight;
        cout << "Input the wheel:";
        cin >> wheel;
        cout << "Input the the busload:";
        cin >> busload;
        cout << "Input the deadweight:";
        cin >> deadweight;
        v1.set(weight, wheel);
        car1.set1(weight, wheel, busload);
        truck1.set2(weight, wheel, deadweight);
        cout << "The vehicle:" << endl;
        v1.display();
        cout << "The car:" << endl;
        car1.display();
        cout << "The truck:" << endl;
        truck1.display();
        return 0;
    }
    

    望采纳

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 6月10日
  • 已采纳回答 6月2日
  • 创建了问题 6月1日

悬赏问题

  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测