#include <iostream>
using namespace std;
class Driver {
public:
virtual void startTransfer() = 0;
virtual ~Driver() {}
};
class SpiDriver : public Driver {
public:
void startTransfer() {
cout << "SPI transfer started" << endl;
}
};
class I2cDriver : public Driver {
public:
void startTransfer() {
cout << "I2C transfer started" << endl;
}
};
int main() {
int mode;
cin >> mode;
SpiDriver spi;
I2cDriver i2c;
Driver& driver = (mode == 0)
? static_cast<Driver&>(spi)
: static_cast<Driver&>(i2c);
driver.startTransfer();
return 0;
}
Explanation & Logic Summary
Firmware Relevance & Real-World Context
In real embedded firmware systems:
Using stack-allocated objects with polymorphic references provides:
This problem demonstrates a production-grade embedded C++ technique that is essential for firmware running on resource-constrained systems.
Input
0
Expected Output
SPI transfer started