#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
printType()
is directly accessible from a Sensor object.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:
Input
Expected Output
Generic Device Sensor Device