22. Multiple Inheritance Sensor and Logger

#include <iostream>
using namespace std;

class Sensor {
public:
    void readValue() {
        cout << "Reading sensor value\n";
    }
};

class Logger {
public:
    void logData() {
        cout << "Logging data\n";
    }
};

class SmartSensor : public Sensor, public Logger {
public:
    void process() {
        readValue();
        logData();
    }
};

int main() {
    SmartSensor ss;
    ss.process();
    return 0;
}

 

Solution Explanation

  • SmartSensor inherits from both Sensor and Logger.
  • It can use methods from both base classes.
  • process() simply calls each method in order.
     

Layman’s Terms

It’s like a gadget that’s both a sensor (can measure) and a logger (can record). The smart sensor combines both jobs.

 

Significance for Embedded Developers

Multiple inheritance comes up when designing reusable components.

For example:

  • A device driver that inherits a communication interface (like I2CDevice) and a diagnostics interface (like Loggable).
  • A sensor that not only provides data but can also directly log or communicate.

     
Loading...

Input

Expected Output

Reading sensor value Logging data