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日

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改