20. Protected Inheritance

#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

  • Protected inheritance makes showType() hidden from outside, but still callable from Sensor.
  • The derived class exposes its own interface (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.

 

Loading...

Input

Expected Output

Generic Device Sensor Device