#include <iostream>
using namespace std;
class Driver {
public:
virtual void process() {
cout << "Generic driver processing" << endl;
}
};
class SpiDriver : public Driver {
public:
void process() override {
cout << "SPI driver processing" << endl;
}
};
class I2cDriver : public Driver {
public:
void process() override {
cout << "I2C driver processing" << endl;
}
};
int main() {
int mode;
cin >> mode;
SpiDriver spi;
I2cDriver i2c;
Driver* driver = nullptr;
if (mode == 0) {
driver = &spi;
} else {
driver = &i2c;
}
driver->process();
return 0;
}
Explanation & Logic Summary:
The base class defines a virtual function process().
const)override, the compiler allowed itoverride keyword was presentoverride to both derived implementationsFirmware Relevance & Real-World Context:
In real embedded firmware:
override prevents field failures that are extremely difficult to debugCore rule reinforced:
Every virtual function override must use
override, and the compiler must be allowed to validate it.
This discipline is essential for building safe, reliable, and maintainable firmware systems.
Input
0
Expected Output
SPI driver processing