So i’m trying to better understand virtual methods.
I have a base class Sensor and several subclasses SensorTemp *SensorPh* SensorOrp
class Sensor
{
public:
virtual void updateValue();
}
the subclasses then override updateValue with their own method definitions (this happens in each of the subclasses)
.h file
#include "Sensor.h"
class SensorTemp : public Sensor
{
private:
public:
};
.Cpp file
#include "Sensor.h"
#include "SensorTemp.h"
void Sensor::updateValue(){
int reading = analogRead(pinId);
float voltage = reading * 5.0;
voltage /= 1024.0;
currentVal = voltage * 100 ;
Serial.print("temp: "); Serial.println(currentVal);
}
My issue is this works for the first subclass declaration, although the following will have a compiler error: updateValue() …. first defined here ld.exe : : Disabling relaxation: it will not work with multiple definitions
So I’m stuck on what type of approach I should take if I want to define the method differently from each subclass?
Any insight would be highly appreciated!
You subclass implementation should look like: