Protected Inheritance

#include <iostream>
using namespace std;

class Device {
public:
    void showType() {
        cout << "Generic Device\n";
    }
};

// Define class Sensor with protected inheritance from Device
class Sensor : protected Device {
public:
    // implement showSensor() that calls showType() and then prints "Sensor Device"
    void showSensor() {
        // showType() is accessible here because it was public in Device 
        // and becomes protected in Sensor due to protected inheritance.
        showType(); 
        cout << "Sensor Device\n";
    }
};

int main() {
    Sensor s;
    s.showSensor();  // should print both lines
    return 0;
}
Upvote
Downvote
Loading...

Input

Expected Output

Generic Device Sensor Device