4. Simple Inheritance

#include <iostream>
using namespace std;

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

class Sensor : public Device {
public:
    void printSensor() {
        cout << "Sensor Device\n";
    }
};

int main() {
    Sensor s;
    s.printType();    // from base
    s.printSensor();  // from derived
    return 0;
}

 

Solution Explanation

  • Sensor inherits from Device using public inheritance.
  • Because of this, printType() is directly accessible from a Sensor object.
  • The derived class adds its own function printSensor().
     

Layman’s Terms

Think of Sensor as a special kind of Device. It can do everything a Device does, plus extra sensor-specific things.

Significance for Embedded Developers

Inheritance is useful in firmware when designing HAL or driver layers.

 

For example:

  • Device could represent a generic communication device.
  • Sensor could represent a specific device (e.g., a temperature sensor) that extends the base interface.

     
Loading...

Input

Expected Output

Generic Device Sensor Device