m0_59696848 2021-06-25 20:29 采纳率: 100%
浏览 90
已采纳

设计一个交通工具类族

设计交通工具类族: 开发一个名为Vehicle 的类的层次体系。创建两个类Taxi 和Truck,均以公有模式从类Vehicle 中继承而来。Taxi 类中应包含一个数据成员passenger说明其是否载客。Truck类应包含一个数据成员cargo说明其是否载货。根据题后附的测试程序输出结果 为类Vehicle添加必要的数据成员,并为所有类添加必要的函数来控制和访问类的数据。编写一段测试程序,将Vehicle对象、Truck 对象和Taxi对象打印到屏幕。(30分)
测试程序输出实例为:
Vehicle
              Number of doors: 2
              Number of cylinders: 6
              Transmission type: 3
              Color: blue
              Fuel level: 14.6
Taxi
              Number of doors: 4
              Number of cylinders: 6
              Transmission type: 5
              Color: yellow
              Fuel level: 3.3
              The taxi has no passengers.
Truck
              Number of doors: 2
              Number of cylinders: 16
              Transmission type: 8
              Color: black
              Fuel level: 7.54
              The truck is carrying cargo.

  • 写回答

1条回答 默认 最新

  • CSDN专家-link 2021-06-25 20:31
    关注

    就是测试多态性呗,有门啥的一些属性

    class Vehicle
    {
        private:
            int doors;
            int cylinders;
            int transmission;
            string color;
            float fuel;
        public:
            Vehicle() {}
            Vehicle(int d,int c,int t,string col,float f)
            {
                doors = d;
                cylinders = c;
                transmission = t;
                color = col;   
                fuel = f;
            }
            virtual void print()
            {
                cout<<Number of doors:"<<doors<<endl;
                cout<<Number of cylinders:"<<cylinders<<endl;
                cout<<Transmisson type:"<<transmission<<endl;
                cout<<Color:"<<color<<endl;
                cout<<"Fuel level:"<<fuel<<endl;
            }
    };
    
    class Taxi : public Vehicle
    {
        private:
            int passagers;
        public:
            Taxi() {}
            Taxi(int d,int c,int t,string col,float f,int p) : Vehicle(d,c,t,col,f)
            {
                passagers = p;
            }
            virtual void print()
            {
                Vehicle::Print();
                if(passagers == 0)
                    cout<<"the taxi has no passengers"<<endl;
                else
                    cout<<"Ther taxi is carrying passengers."<<endl;
            }
    };
    
    class Trunk: public Vehicle
    {
        private:
            int cargo;
        public:
            Trunk() {}
            Trunk(int d,int c,int t,string col,float f,int car) : Vehicle(d,c,t,col,f)
            {
                cargp = cargo;
            }
            virtual void print()
            {
                Vehicle::Print();
                if(cargo == 0)
                    cout<<"the trunk has no cargo"<<endl;
                else
                    cout<<"Ther trunk is carrying cargo."<<endl;
            }
    };
    
    int main()
    {
        Vehicle *p = new Vehicle(2,6,3,"blue",14.6);
        p->print();
        delete p;
        p = new Taxi(4,6,5,"yellow",3.3,0);
        p->print();
        delete p;
        p = new Trunk(2,16,8,"black",7.5,10);
        p->print();
        delete p;
    }
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

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