#include <iostream>
#include <string>
// Base class for all locomotives
class Locomotive {
protected:
std::string name; // Name of the locomotive
int yearBuilt; // Year the locomotive was built
public:
// Constructor
Locomotive(std::string name, int yearBuilt) : name(name), yearBuilt(yearBuilt) {}
// Getter and Setter for Name
std::string getName() const { return name; }
void setName(const std::string& newName) { name = newName; }
// Getter and Setter for Year Built
int getYearBuilt() const { return yearBuilt; }
void setYearBuilt(int newYear) {
if (newYear > 0) {
yearBuilt = newYear;
} else {
std::cerr << "Invalid year. Year must be greater than 0." << std::endl;
}
}
};
// Derived class for steam locomotives
class SteamLocomotive : public Locomotive {
private:
std::string boilerType; // Type of boiler (e.g., fire-tube, water-tube)
double maxSpeedKmph; // Maximum speed in kilometers per hour
double currentSpeedKmph; // Current speed in kilometers per hour
bool isOperational; // Indicates if the locomotive is operational
double waterLevelPercentage; // Current water level in the boiler as a percentage
double coalLevelPercentage; // Current coal level as a percentage
public:
// Constructor
SteamLocomotive(std::string name, int yearBuilt, std::string boilerType, double maxSpeedKmph)
: Locomotive(name, yearBuilt), boilerType(boilerType), maxSpeedKmph(maxSpeedKmph),
currentSpeedKmph(0.0), isOperational(false), waterLevelPercentage(0.0), coalLevelPercentage(0.0) {
if (maxSpeedKmph <= 0) {
std::cerr << "Invalid max speed. Max speed must be greater than 0." << std::endl;
}
}
// Getter and Setter for Boiler Type
std::string getBoilerType() const { return boilerType; }
void setBoilerType(const std::string& newType) { boilerType = newType; }
// Getter and Setter for Max Speed
double getMaxSpeedKmph() const { return maxSpeedKmph; }
void setMaxSpeedKmph(double newMaxSpeed) {
if (newMaxSpeed > 0) {
maxSpeedKmph = newMaxSpeed;
} else {
std::cerr << "Invalid max speed. Max speed must be greater than 0." << std::endl;
}
}
// Getter and Setter for Current Speed
double getCurrentSpeedKmph() const { return currentSpeedKmph; }
void setCurrentSpeedKmph(double newSpeed) {
if (newSpeed >= 0 && newSpeed <= maxSpeedKmph) {
currentSpeedKmph = newSpeed;
} else {
std::cerr << "Invalid speed. Speed must be between 0 and " << maxSpeedKmph << " km/h." << std::endl;
}
}
// Operational Status
bool getIsOperational() const { return isOperational; }
void setIsOperational(bool status) {
isOperational = status;
if (!isOperational) {
currentSpeedKmph = 0.0; // Automatically set speed to 0 if not operational
}
}
// Water Level
double getWaterLevelPercentage() const { return waterLevelPercentage; }
void setWaterLevelPercentage(double percentage) {
if (percentage >= 0 && percentage <= 100) {
waterLevelPercentage = percentage;
} else {
std::cerr << "Invalid water level percentage. Must be between 0% and 100%." << std::endl;
}
}
// Coal Level
double getCoalLevelPercentage() const { return coalLevelPercentage; }
void setCoalLevelPercentage(double percentage) {
if (percentage >= 0 && percentage <= 100) {
coalLevelPercentage = percentage;
} else {
std::cerr << "Invalid coal level percentage. Must be between 0% and 100%." << std::endl;
}
}
// Method to display locomotive's status
void displayStatus() const {
std::cout << "Locomotive Name: " << getName() << std::endl;
std::cout << "Year Built: " << getYearBuilt() << std::endl;
std::cout << "Boiler Type: " << boilerType << std::endl;
std::cout << "Max Speed: " << maxSpeedKmph << " km/h" << std::endl;
std::cout << "Current Speed: " << currentSpeedKmph << " km/h" << std::endl;
std::cout << "Operational Status: " << (isOperational ? "Operational" : "Not Operational") << std::endl;
std::cout << "Water Level: " << waterLevelPercentage << "%" << std::endl;
std::cout << "Coal Level: " << coalLevelPercentage << "%" << std::endl;
}
// Example of an action: Increase speed
void increaseSpeed(double increment) {
if (isOperational) {
setCurrentSpeedKmph(currentSpeedKmph + increment);
} else {
std::cout << "Cannot increase speed. Locomotive is not operational." << std::endl;
}
}
// Example of an action: Decrease speed
void decreaseSpeed(double decrement) {
if (isOperational) {
setCurrentSpeedKmph(currentSpeedKmph - decrement);
if (currentSpeedKmph < 0) { // Ensure speed doesn't go below 0
setCurrentSpeedKmph(0.0);
}
} else {
std::cout << "Cannot decrease speed. Locomotive is not operational." << std::endl;
}
}
};
int main() {
// Example usage
SteamLocomotive locomotive("Steam Star", 1920, "Fire-Tube", 120.0);
locomotive.setIsOperational(true);
locomotive.setWaterLevelPercentage(80.0);
locomotive.setCoalLevelPercentage(90.0);
locomotive.displayStatus();
locomotive.increaseSpeed(50.0);
locomotive.displayStatus();
locomotive.decreaseSpeed(20.0);
locomotive.displayStatus();
return 0;
}