【id:297】【10分】J. OOP租车系统(多重继承)

#include<bits/stdc++.h>

using namespace std;

class Car{
protected:
    int id;
    string name;
    int zujin;
public:
    Car(){}
    ~Car(){}
    Car(int id_, string name_, int zujin_){
        this->id = id_;
        this->name = name_;
        this->zujin = zujin_;
    }
};

class Keche: public Car{
protected:
    int zaikeliang;
public:
    Keche(){}
    ~Keche(){}
    Keche(int id_, string name_, int zujin_, int zaikeliang_){
        this->id = id_;
        this->name = name_;
        this->zujin = zujin_;
        this->zaikeliang = zaikeliang_;
    }
    int getId(){
        return this->id;
    }
    string getName(){
        return this->name;
    }
    int getZujin(){
        return this->zujin;
    }
    int getZaikeliang(){
        return this->zaikeliang;
    }
};

class Huoche: public Car{
protected:
    double zaihuoliang;
public:
    Huoche(){}
    ~Huoche(){}
    Huoche(int id_, string name_, int zujin_, double zaihuoliang_){
        this->id = id_;
        this->name = name_;
        this->zujin = zujin_;
        this->zaihuoliang = zaihuoliang_;
    }
    int getId(){
        return this->id;
    }
    string getName(){
        return this->name;
    }
    int getZujin(){
        return this->zujin;
    }
    double getZaihuoliang(){
        return this->zaihuoliang;
    }
};

class Pikache:public Keche, public Huoche{
public:
    Pikache(){}
    ~Pikache(){}
    Pikache(int id_, string name_, int zujin_, int zaikeliang_, double zaihuoliang_){
        Keche::id = id_;
        Keche::name = name_;
        Keche::zujin = zujin_;
        this->zaikeliang = zaikeliang_;
        this->zaihuoliang = zaihuoliang_;
    }
    int getId(){
        return Keche::id;
    }
    string getName(){
        return Keche::name;
    }
    int getZujin(){
        return Keche::zujin;
    }
    int getZaikeliang(){
        return this->zaikeliang;
    }
    double getZaihuoliang(){
        return this->zaihuoliang;
    }
};

int main()
{
    Keche keche1(1,"A",800,5);
    Keche keche2(2,"B",400,5);
    Keche keche3(3,"C",800,5);
    Keche keche4(4,"D",1300,51);
    Keche keche5(5,"E",1500,55);
    Pikache pikache6(6,"F",500,5,0.45);
    Pikache pikache7(7,"G",450,5,2.0);
    Huoche huoche8(8,"H",200,3);
    Huoche huoche9(9,"I",1500,25);
    Huoche huoche10(10,"J",2000,35);

    int checknum;
    cin >> checknum;
    if(checknum)
    {
        int N;
        cin >> N;
        int sum_zaikeliang = 0;
        double sum_zaihuoliang = 0;
        int sum_zujin = 0;
        while(N--)
        {
            int m,n;
            cin >> m >> n;
            switch(m)
            {
                case 1:
                    sum_zaikeliang += keche1.getZaikeliang() * n;
                    sum_zujin += keche1.getZujin() * n;
                    break;
                case 2:
                    sum_zaikeliang += keche2.getZaikeliang() * n;
                    sum_zujin += keche2.getZujin() * n;
                    break;
                case 3:
                    sum_zaikeliang += keche3.getZaikeliang() * n;
                    sum_zujin += keche3.getZujin() * n;
                    break;
                case 4:
                    sum_zaikeliang += keche4.getZaikeliang() * n;
                    sum_zujin += keche4.getZujin() * n;
                    break;
                case 5:
                    sum_zaikeliang += keche5.getZaikeliang() * n;
                    sum_zujin += keche5.getZujin() * n;
                    break;
                case 6:
                    sum_zaikeliang += pikache6.getZaikeliang() * n;
                    sum_zaihuoliang += pikache6.getZaihuoliang() * n;
                    sum_zujin += pikache6.getZujin() * n;
                    break;
                case 7:
                    sum_zaikeliang += pikache7.getZaikeliang() * n;
                    sum_zaihuoliang += pikache7.getZaihuoliang() * n;
                    sum_zujin += pikache7.getZujin() * n;
                    break;
                case 8:
                    sum_zaihuoliang += huoche8.getZaihuoliang() * n;
                    sum_zujin += huoche8.getZujin() * n;
                    break;
                case 9:
                    sum_zaihuoliang += huoche9.getZaihuoliang() * n;
                    sum_zujin += huoche9.getZujin() * n;
                    break;
                default:
                    sum_zaihuoliang += huoche10.getZaihuoliang() * n;
                    sum_zujin += huoche10.getZujin() * n;
                    break;
            }
        }
        printf("%d %.2lf %d\n",sum_zaikeliang, sum_zaihuoliang, sum_zujin);

    }
    else
    {
        cout << "0 " << "0.00 " << "0" << endl;
    }
    return 0;
}
好的,下面是使用C++实现的oop租车系统多重继承)的代码示例: ```cpp #include <iostream> #include <string> #include <vector> using namespace std; // 定义车辆类 class Vehicle { public: Vehicle(const string& make, const string& model, int year, int rent_per_day) : make_(make), model_(model), year_(year), rent_per_day_(rent_per_day) {} // 打印车辆信息 void print_info() const { cout << make_ << " " << model_ << " (" << year_ << ") - $" << rent_per_day_ << "/day" << endl; } private: string make_; // 品牌 string model_; // 型号 int year_; // 年份 int rent_per_day_; // 租金/天 }; // 定义客户类 class Customer { public: Customer(const string& name, int age) : name_(name), age_(age) {} // 打印客户信息 void print_info() const { cout << name_ << " (" << age_ << " years old)" << endl; if (rented_vehicle_) { cout << "Rented vehicle: "; rented_vehicle_->print_info(); } } // 租用车辆 void rent_vehicle(Vehicle* vehicle) { rented_vehicle_ = vehicle; cout << name_ << " rented "; rented_vehicle_->print_info(); } private: string name_; // 姓名 int age_; // 年龄 Vehicle* rented_vehicle_ = nullptr; // 租用的车辆 }; // 定义租车公司类 class CarRentalCompany { public: // 初始化车辆和客户 CarRentalCompany() { // 添加车辆 vehicles_.push_back(new Vehicle("Make1", "Model1", 2020, 50)); vehicles_.push_back(new Vehicle("Make2", "Model2", 2020, 60)); vehicles_.push_back(new Vehicle("Make3", "Model3", 2020, 70)); // 添加客户 customers_.push_back(new Customer("Customer1", 25)); customers_.push_back(new Customer("Customer2", 30)); } // 打印所有车辆信息 void print_all_vehicles() const { cout << "All vehicles:" << endl; for (const auto& vehicle : vehicles_) { vehicle->print_info(); } } // 打印所有客户信息 void print_all_customers() const { cout << "All customers:" << endl; for (const auto& customer : customers_) { customer->print_info(); } } private: vector<Vehicle*> vehicles_; // 所有车辆的数组 vector<Customer*> customers_; // 所有客户的数组 }; int main() { // 创建租车公司 CarRentalCompany company; // 打印所有车辆和客户信息 company.print_all_vehicles(); company.print_all_customers(); // 客户1租用第2辆车 company.print_all_customers(); company.print_all_vehicles(); company.print_all_customers(); company.print_all_vehicles(); return 0; } ``` 该代码定义了三个类:Vehicle、Customer 和 CarRentalCompany。其中,Vehicle 和 Customer 分别表示车辆和客户,而 CarRentalCompany 则表示租车公司。使用多重继承,CarRentalCompany 类同时继承了 Vehicle 和 Customer 类的属性和方法。 使用该代码,我们可以创建一个租车公司,并初始化其中的车辆和客户。然后,我们可以打印所有车辆和客户的信息,以及让某个客户租用某辆车。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值