#include <iostream>
class UARTDriver {
private:
// Member is now private to prevent arbitrary assignments
int baud_rate;
public:
// Initialize with the default safe baud rate of 9600
UARTDriver() : baud_rate(9600) {}
/**
* Updates the baud rate only if it matches hardware-supported values.
* Supported: 9600, 19200, 115200.
*/
void setBaudRate(int requested_rate) {
if (requested_rate == 9600 ||
requested_rate == 19200 ||
requested_rate == 115200) {
baud_rate = requested_rate;
}
// If invalid, we ignore the request and keep the previous state.
}
/**
* Provides read-only access to the current active baud rate.
*/
int getBaudRate() const {
return baud_rate;
}
};
int main() {
int N;
// Standard check to ensure we read the number of commands correctly
if (!(std::cin >> N)) return 0;
UARTDriver driver;
for (int i = 0; i < N; ++i) {
int req;
if (!(std::cin >> req)) break;
// Use the validating setter instead of direct member access
driver.setBaudRate(req);
// Retrieve the current state via the getter
std::cout << "Active: " << driver.getBaudRate() << std::endl;
}
return 0;
}
Input
4 115200 500 19200 -1
Expected Output
Active: 115200 Active: 115200 Active: 19200 Active: 19200