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;
    }
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能