#include <iostream>
using namespace std;
class Sensor {
public:
virtual int readValue() const = 0;
virtual ~Sensor() = default;
};
class TemperatureSensor : public Sensor {
private:
int temperature;
public:
TemperatureSensor(int t) : temperature(t) {}
int readValue() const override { return temperature; } // fixed
};
int main() {
int t;
cin >> t;
TemperatureSensor ts(t);
Sensor* ptr = &ts;
cout << "Temperature=" << ptr->readValue();
return 0;
}
Solution Details
👉 In simple words: The base class (Sensor) sets a rule: “every sensor must say its value.” If the derived class (TemperatureSensor) doesn’t follow the rule, the compiler refuses to build the code.
Significance for Embedded Developers
Input
25
Expected Output
Temperature=25