Car

Code by Ireneusz A.
 avatar
user_6919294
c_cpp
17 days ago
1.3 kB
4
Indexable
Never
// DARIA, nazwy zmiennych, klas itd zawsze nazywaj po Angielsku, to reguła :)

// przykład

#include <System.hpp>
#include <vector>

// Car class to represent a vehicle with basic functionality
class Car
{
private:
    String _brand;       // Brand of the car
    String _model;       // Model of the car
    int _year;           // Year of production
    double _speed;       // Current speed of the car

public:
    // Constructor to initialize the car with brand, model, and year
    Car(const String& brand, const String& model, int year)
        : _brand(brand), _model(model), _year(year), _speed(0) {}

    // Method to accelerate the car by a certain amount (Delta)
    void Accelerate(double delta)
    {
        _speed += delta;
        if (_speed < 0)
            _speed = 0;  // Ensure speed doesn't go below 0
    }

    // Method to decelerate (slow down) the car by a certain amount (Delta)
    void Decelerate(double delta)
    {
        _speed -= delta;
        if (_speed < 0)
            _speed = 0;  // Ensure speed doesn't go below 0
    }

    // Method to get the car's information as a formatted string
    String GetInfo() const
    {
        return String().sprintf(L"Car: %s %s, Year: %d, Current speed: %.2f km/h",
            _brand.c_str(), _model.c_str(), _year, _speed);
    }
};
Leave a Comment