Car_2
Code by Ireneusz A.user_6919294
c_cpp
a year ago
3.5 kB
27
Indexable
/*
Oto zoptymalizowana wersja klasy Car. Główne zmiany i ulepszenia to:
- Użycie biblioteki standardowej C++ zamiast System.hpp.
- Zastąpienie String std::string dla lepszej przenośności.
- Usunięcie przedrostków 'F' z nazw pól klasy dla lepszej czytelności.
- Uproszczenie konstruktorów i metod.
- Użycie inicjalizacji in-line dla niektórych pól.
- Zastosowanie std::max w metodzie decelerate.
- Uproszczenie metody getInformation z użyciem std::to_string.
- Usunięcie niepotrzebnych komentarzy dla zwiększenia czytelności.
- Użycie std::move dla optymalizacji wydajności w konstruktorze.
- Ogólne uproszczenie i skrócenie kodu.
*/
#include <string>
#include <vector>
#include <memory>
#include <stdexcept>
class Engine {
public:
virtual double getPower() const = 0;
virtual ~Engine() = default;
};
class PetrolEngine : public Engine {
double power;
public:
explicit PetrolEngine(double p) : power(p) {}
double getPower() const override { return power; }
};
class DieselEngine : public Engine {
double power;
public:
explicit DieselEngine(double p) : power(p) {}
double getPower() const override { return power; }
};
class Car {
public:
enum class DriveType { FrontWheel, RearWheel, AllWheel };
private:
std::string brand, model;
int yearOfManufacture;
double speed = 0, fuel, tankCapacity;
DriveType driveType;
std::unique_ptr<Engine> engine;
std::vector<std::string> equipment;
static std::string driveTypeToString(DriveType type) {
switch (type) {
case DriveType::FrontWheel: return "Front-wheel drive";
case DriveType::RearWheel: return "Rear-wheel drive";
case DriveType::AllWheel: return "All-wheel drive";
default: return "Unknown";
}
}
public:
Car(std::string b, std::string m, int year, double tank, DriveType drive)
: brand(std::move(b)), model(std::move(m)), yearOfManufacture(year),
fuel(tank), tankCapacity(tank), driveType(drive) {}
void setEngine(std::unique_ptr<Engine> e) { engine = std::move(e); }
void addEquipment(const std::string& item) { equipment.push_back(item); }
void accelerate(double delta) {
if (fuel > 0 && engine) {
speed += delta * (engine->getPower() / 100.0);
fuel -= delta * 0.01;
if (speed < 0) speed = 0;
if (fuel < 0) fuel = 0;
}
}
void decelerate(double delta) { speed = std::max(0.0, speed - delta); }
void refuel(double amount) {
if (fuel + amount > tankCapacity) throw std::runtime_error("Tank overflow!");
fuel += amount;
}
std::string getInformation() const {
std::string info = "Car: " + brand + " " + model + "\n" +
"Year: " + std::to_string(yearOfManufacture) + "\n" +
"Speed: " + std::to_string(speed) + " km/h\n" +
"Fuel: " + std::to_string(fuel) + " / " + std::to_string(tankCapacity) + " l\n" +
"Drive: " + driveTypeToString(driveType) + "\n" +
"Engine: " + std::to_string(engine ? engine->getPower() : 0) + " HP\n" +
"Equipment: ";
for (const auto& item : equipment) {
info += item + ", ";
}
if (!equipment.empty()) {
info.pop_back(); info.pop_back(); // Remove last ", "
}
return info;
}
};Editor is loading...
Leave a Comment