车辆选择(继承)

有一个汽车类vehicle,它具有一个需传递参数的构造函数,汽车类vehicle中的数据成员为: 车轮个数wheels和车重weight放在保护段中,汽车类vehicle中的公有成员函数为:get_wheels()(返回车轮个数的值)、get_weight()(返回车重的值)、wheel_load()(返回每个轮胎的载重量的值:weight/wheels)、print()(输出车轮的个数和车重的公斤数);

小车类car是vehicle类的派生类,它具有一个需传递参数的构造函数,小车类car中的私有数据成员为:车载人数passenger_load,小车类car中的公有成员函数为:get_passengers()(返回车载人数的值)、print()(输出小车车轮的个数和车重的公斤数以及车载人数的个数);

卡车类truck是vehicle类的派生类,它具有一个需传递参数的构造函数,卡车类truck中的私有数据成员为:载人数passenger_load和载重量payload,卡车类truck中的公有成员函数为:get_passengers()(返回车载人数的值)、efficiency()(返回卡车的载重效率的值:payload/(payload+weight)、print()(输出卡车车轮的个数和车重的公斤数以及车载人数的个数和卡车的载重效率的值))。

生成上述类并编写主函数,根据输入的车辆基本信息,建立车辆对象,并能计算输出该车辆的基本信息。 输入格式:测试输入包含一个测试用例,每一行给出一个车辆的基本信息,每行的第一个字符处为当前车辆的类型,第二个数字为当前车辆的编号,若车辆为vehicle,后面跟随两个数字分别为wheels和weight,若车辆为car,后面跟随三个数字分别为wheels,weight和车载人数,若车辆为truck,后面跟随四个数字分别是wheels,weight、车载人数和载重量。(以上数字均为整型)。-1表示输入结束,相应结果不要输出。请注意输出格式,按照输入顺序进行编号 说明:本题中轮胎载重量、载重效率若需输出保留小数点后两位。

输入样例:

vehicle 101 4 1900

car 201 4 2000 5

truck 301 6 3000 2 9000

car 202 4 1800 4

-1

输出样例:

The 1st object is Vehicle No. 101: weight 1900 Kg and wheels 4

The 2nd object is Car No. 201: passenger_load 5 weight 2000 Kg and wheels 4

The 3rd object is Truck No. 301: passenger_load 2 weight 3000 Kg wheels 6 and efficiency 0.75

The 4th object is Car No. 202: passenger_load 4 weight 1800 Kg and wheels 4

 

#include<iostream>
#include<iomanip>
using namespace std;
//vehicle
class vehicle{
protected:
    float wheels;
    float weight;
public:
    vehicle(float a,float b):wheels(a),weight(b){};
    float getWheels(){return wheels;}
    float getWheight(){return weight;}
    float wheelLoad(){return weight/wheels;}
    void print(){cout<<"weight "<<weight<<" Kg and wheels "<<wheels<<endl;}
};
//car
class car : public vehicle{
protected:
    int passenger_load;
public:
    car(float a,float b,int c) : vehicle(a,b),passenger_load(c){};
    int getPassenger(){return passenger_load;}
    void print(){cout<<"passenger_load "<<passenger_load<<" weight "<<weight<<" Kg and wheels "<<wheels<<endl;}
};
//truck
class truck : public car{
protected:
    int payLoad;
public:
    truck(float a,float b,int c,int d) : car(a,b,c) , payLoad(d){};
    float efficiency(){return payLoad/(payLoad+weight);}
    void print();
};
void truck::print(){
    cout<<"passenger_load "<<passenger_load 
        <<" weight "<<weight<<" Kg wheels "
        <<wheels<<" and efficiency "
        <<setiosflags(ios::fixed)<<setprecision(2)
        <<efficiency()<<setprecision(0)<<endl;
}
int main()
{
    string type;
    int num, wheels;
    float weight;
    int i=1;
    while(cin>>type)
    {
        if(type=="-1")break;
        cout<<"The "<<i<<"";
          if(i==1) cout<<"st";
        else if(i==2) cout<<"nd";
        else if(i==3) cout<<"rd";            
        else cout<<"th";
          i++;
          cout<<" object is ";
        if(type=="vehicle")
        {
            cin>>num>>wheels>>weight;
            cout<<"Vehicle"<<" No. "<<num<<": ";
            vehicle hello(wheels,weight);
            hello.print();
        }else if(type=="car")
        {
            int pass;
            cin>>num>>wheels>>weight>>pass;
            cout<<"Car"<<" No. "<<num<<": ";
            car hello(wheels,weight,pass);
            hello.print() ;
        }else
        {
            int pass, load;
            cin>>num>>wheels>>weight>>pass>>load;
            cout<<"Truck"<<" No. "<<num<<": ";
            truck hello(wheels,weight,pass,load);
            hello.print();
        }
    }
    return 0;
}

 

欢迎指教,一起学习!

未经本人允许,请勿转载!

谢谢!

转载于:https://www.cnblogs.com/hello-OK/p/6993047.html

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,这是一个简单的汽车出租系统Java程序的示例,使用了继承和数组。 首先,我们定义一个Car来表示汽车: ```java public class Car { private String make; private String model; private int year; private double price; public Car(String make, String model, int year, double price) { this.make = make; this.model = model; this.year = year; this.price = price; } public String getMake() { return make; } public String getModel() { return model; } public int getYear() { return year; } public double getPrice() { return price; } public String toString() { return year + " " + make + " " + model + ", $" + price; } } ``` 然后,我们定义一个RentalCar来表示出租汽车,它继承自Car: ```java public class RentalCar extends Car { private boolean rented; public RentalCar(String make, String model, int year, double price) { super(make, model, year, price); rented = false; } public boolean isRented() { return rented; } public void rent() { rented = true; } public void returnCar() { rented = false; } public String toString() { String status; if (rented) { status = " (rented)"; } else { status = " (available)"; } return super.toString() + status; } } ``` 在RentalCar中,我们添加了一些特定于租车的方法,如rent()和returnCar(),并添加了一个表示是否已被租赁的变量rented。 现在,我们可以创建一个CarRentalSystem,用于管理所有的租车: ```java import java.util.Scanner; public class CarRentalSystem { private RentalCar[] cars; public CarRentalSystem(int size) { cars = new RentalCar[size]; } public void addCar(int index, RentalCar car) { cars[index] = car; } public RentalCar getCar(int index) { return cars[index]; } public void rentCar(int index) { cars[index].rent(); } public void returnCar(int index) { cars[index].returnCar(); } public void displayCars() { for (int i = 0; i < cars.length; i++) { System.out.println(i + ": " + cars[i]); } } public static void main(String[] args) { CarRentalSystem system = new CarRentalSystem(3); system.addCar(0, new RentalCar("Toyota", "Camry", 2020, 50.0)); system.addCar(1, new RentalCar("Honda", "Accord", 2021, 60.0)); system.addCar(2, new RentalCar("Nissan", "Altima", 2019, 40.0)); Scanner scanner = new Scanner(System.in); int choice; do { System.out.println("1. Rent a car"); System.out.println("2. Return a car"); System.out.println("3. Display available cars"); System.out.println("4. Exit"); System.out.print("Enter your choice: "); choice = scanner.nextInt(); switch (choice) { case 1: System.out.print("Enter car number to rent: "); int rentIndex = scanner.nextInt(); system.rentCar(rentIndex); break; case 2: System.out.print("Enter car number to return: "); int returnIndex = scanner.nextInt(); system.returnCar(returnIndex); break; case 3: system.displayCars(); break; case 4: System.out.println("Goodbye!"); break; default: System.out.println("Invalid choice, try again."); break; } } while (choice != 4); } } ``` 在CarRentalSystem中,我们使用了一个RentalCar型的数组来存储所有的租车,并添加了一些方法来管理这些租车。在main()方法中,我们创建了一些RentalCar对象,并添加到CarRentalSystem中。然后,我们使用一个简单的菜单来让用户选择租车、还车或显示可用车辆

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值