Using method in derived class that overload base class

Hello,
I'm facing an issue that i cannot solve.
I have a base class to drive some sensors and i derivate this class for specific sensors operations.

Consider the following simplified code :

/*sensorBase.h*/
class sensorBase
{
protected:
  void begin(int);
  void myOtherMethod(char);
}
//end

/*sensorBase.cpp*/
sensorBase::begin(int i)
{
  Serial.println("i run in base class");
}


/* sensor2.h */
#include "sensorBase.h"
class sensor2 : public
{
protected:
   void begin(int);
   void myOtherMethod(char, int);
}
//end

/*sensor.cpp*/
#include "sensor2.h"
sensor2::begin(int i)
{
  Serial.println("i run in derivated class");
}
sensor2::myOtherMethod(char c, int i)
{
  serial.println("do more stuff than in base class");
}
//end

/*project.ino*/
#include "sensor2.h"
sensor2 s;
loop()
{
  s.begin(1);
}
//end

Serial monitor says "i run in base class"

So my derivated class is never called.
I tryed to use virtual/override options, but i think i misuse them and it does not work too.
Can someone provide some help, direction, documentation to read or clarify this ?

Thanks.
Gui2

It would have been a whole lot easier to help if your simplified code had actually compiled.

project.ino

#include "sensor2.h"
sensorBase s;
sensor2 s2;

void setup() {
   Serial.begin(115200);
   s.begin(1);
   s2.begin(1);
}

void loop() {
}

sensor2.h

#include "sensorBase.h"
class sensor2 {
   public:
      void begin(int) {
         Serial.println("i run in derivated class");
      };
   protected:
      void myOtherMethod(char, int) {
         Serial.println("do more stuff than in base class");
      };
};

sensorBase.h

class sensorBase {
   public:
      virtual void begin(int) {
         Serial.println("i run in base class");
      };

   protected:
      virtual void myOtherMethod(char);
};

Output

Monitor port settings:
baudrate=115200
Connected to /dev/ttyUSB0! Press CTRL-C to exit.
i run in base class
i run in derivated class
1 Like

You don't define ANY derived class... Not at all sure what you expect to happen, but you clearly do not understand how to define a derived class, and it's not clear to me what you are trying to accomplish, much less what code you used to try.

1 Like

couldn't have said it better.

@gchardin

here is an example which shows

  • class inheritance
  • override a virtual function from the base class
  • make a base function visible with USING even if it is overloaded in the derived class

Hello guys,
thank you so much for you remarks and advices.

I changed a little the code provided by noiasca to show you which part is failing to compile.
Hope it will give you better understanding.

The main issue/error is :
error: conflicting return type specified for 'virtual float Sensor2::myOtherMethod(int)'
float myOtherMethod(int);

Because the overriding method in derived class use another return type than the base class.
Searching by myself let me beleive that i have to use pointer for function return to "abstract" this return. But i do not like it (maybe because i do not fully confident with pointer/syntax). What about that ?
Is there a "simple" or better way to address this issue ?

Bye.
Gui2

Ok if it does not work. There always a reason.
But what are the options here.

To give more background and understand the goals :

Sensors share a lot of common (ie commands sent by uart, calibration).
Sensors return results as an int and some kinds require some more calculation (ie to get values in %, and adjust precision with decimals, some can send result in a specific unit) and should return int or float or char*.
My design was to embark all the common stuff in the base class and add specific sensors calculation in the child class. It seems good for me, but my bad knowledge of C++ lead me to some issues. like the one reported.
The actual code have in the base class the standard calculation method and the derived class is running the base method, apply calculations and return the result in another type. Because next sensor generation will behave deferently i choose this (bad?) desing.

As a result, discussions and explanations it leads me to this questions :

  • is my design correct or must be re-evaluated ?
  • should i adapt the existing code ?
  • should i rewrite the existing code because it will not be acheived like i wrote it ?

Maybe some experienced developer can give me some ideas/answers.

Thanks for reading and helping

Check out how it's done in the Adafruit Unified Sensor Driver.

1 Like

I changed you damages and made it compile again.

when you have a different return type / a different signature - you don't need a late binding and therefore you don't need to override / virtual.

Regarding your usecase:
when you have different sensors (with different return types) I see no need for a "return result" member function in the base class at all. If you have a "common" value calculation, name it what it is ... some kind of calculation.

Perhaps you have to read about template functions

1 Like

Some of the posts seem to confuse overloading (two methods with different signatures and the same name, statically resolved) and overriding (a subclass method that hides a superclass method of the same name and same signature dynamically at runtime).

The C++ word for superclass is base class and for subclass is derived class, the normal terms used everywhere else!

did any of my posts confuse you or why did you answer me?
If I were wrong please point me to my mistake.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.