#include <iostream>
using namespace std;
class Device {
public:
void showType() {
cout << "Generic Device\n";
}
};
class Sensor : protected Device {
public:
void showSensor() {
showType();
cout << "Sensor Device\n";
}
};
int main() {
Sensor s;
s.showSensor();
return 0;
}
Solution Explanation
showType()
hidden from outside, but still callable from Sensor.showSensor()
), which internally uses the base method.Layman’s Terms
Think of showType() as a tool inside the workshop — outsiders can’t touch it, but workers (derived classes) can still use it.
Significance for Embedded Developers
Protected inheritance is useful in designing driver hierarchies where base internals are available to child classes but hidden from the user API. Example: a generic communication device base class with protected helpers for UART/SPI drivers.
Input
Expected Output
Generic Device Sensor Device