Untitled

mail@pastecode.io avatar
unknown
c_cpp
5 days ago
1.8 kB
3
Indexable
Never
#include <iostream>
#include <string>

using namespace std;

class Car{
public:
    Car(int y, string m, int max) : yearModel(y), make(m), maxSpeed(max)
    {
        this->speed=0;
        this->refuel();
    }
    int getYearModel() {return this->yearModel;}
    string getMake() {return this->make;}
    int getSpeed() {return this->speed;}
    void accelerate() {
        if(this->fuel == 0) return void();
        this->speed+=(this->speed==this->maxSpeed ? 0 : 5);
        this->fuel--;
    }
    void brake() {
        this->speed-=(this->speed==0 ? 0 : 5);
        this->fuel--;
    }
    void refuel(){
        this->speed=0; //po tankowaniu trzeba przyspieszyć;
        this->fuel = 50;
    }
    int range(){
        return this->fuel*10; //10km/l
    }
    void displayInfo()
    {
        cout << "\nYear: " << this->yearModel
             << "\nMake: " << this->make
             << "\nMax Velocity: " << this->maxSpeed
             << "\nRange: " << this->range();
    }
private:
    int yearModel;
    string make;
    int speed;
    int fuel;
    int maxSpeed;
};

class Truck : public Car{
public:
    Truck(int y, string m, int max, int maxCapacity) : Car(y, m, max), loadCapacity(maxCapacity){}
    void displayInfo()
    {
        Car::displayInfo();
        cout << "\nLoadCapacity: " << this->loadCapacity;
    }
private:
    int loadCapacity;
};

class SportsCar : public Car{
public:
    SportsCar(int y, string m, int max, int turbo) : Car(y, m, max), turboBoost(turbo){}
    void displayInfo()
    {
        Car::displayInfo();
        cout << "\nTurbo: " << this->turboBoost;
    }
private:
    int turboBoost;
};

int main() {
    SportsCar sc(2020, "Mercedes", 400, 50);
    Truck t(2020, "Scania", 180, 1200);

    sc.displayInfo();
    t.displayInfo();

    return 0;
}
Leave a Comment