#include <iostream>
using namespace std;
class PowerControl {
public:
void enable() {
cout << "Power enabled" << endl;
}
};
class InterruptControl {
public:
void enable() {
cout << "Interrupt enabled" << endl;
}
};
// Multiple inheritance
class SpiDriver : public PowerControl, public InterruptControl {
};
int main() {
int mode;
cin >> mode;
SpiDriver spi;
if (mode == 0) {
spi.PowerControl::enable(); // Explicitly select PowerControl
} else if (mode == 1) {
spi.InterruptControl::enable(); // Explicitly select InterruptControl
}
return 0;
}
Explanation & Logic Summary:
When a class inherits from multiple base classes that contain functions with the same name, calling that function through the derived class is ambiguous.
C++ requires explicit qualification using the scope resolution operator (::) to specify which base class function should be invoked.
By qualifying the function call with the base class name, the ambiguity is resolved at compile time without altering the class hierarchy.
Firmware Relevance & Real-World Context:
In embedded firmware systems:
Understanding how to resolve multiple inheritance ambiguity:
This skill is critical for maintaining safe, readable, and correct embedded C++ firmware code.
Input
0
Expected Output
Power enabled