Interface Classes and inheritance on top of existing libraries

I have found great utility in having an interface class around a class or hierarchy of inherited classes. I am now trying to build a system where I have an underlying library, for example LiquidCrystal.h, and want to add a class on top of this which inherits LiquidCrystal. Easy. Done. But I am getting hung up trying to add an interface class so that I can create an array of the objects that are created within this "stack" of classes, so that I can step through all of the objects to do things with them all. I have used interface classes this way, for example, to step through a bunch of data fields and put the changing data on the LCD in the right place where the position, font size, font, last value, etc are members of one of the classes. For example (I'll just make this up quickly, so it might have some errors as I am not testing it):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// THIS IS JUST AN EXAMPLE TO SHOW HOW ONE MIGHT USE THE INTERFACE CLASS
class iInterface {

   virtual int getDisplayNumber()=0;
};

class Displays : public iInterface {

 private:
   int m_displayNumber;
   int m_xPosition;
   int m_y_position;

 public:
   //constructor
   Displays Displays (int disp, int x, int y) 
     :m_displayNumber{disp},
      m_XPosition{x},
      m_YPosition{y}
    {
    }

   int getDisplayNumber() {
     return (m_displayNumber);  
   }
}; // end of class Displays

// create Display objects

Displays FrontRoom { 32, 0, 1 };
Displays BackRoom  { 18, 3, 2 };
Displays Garage    { 11, 4, 6 };  // but image 50+ displays, for example

iInterface* const DisplayList[] = {&FrontRoom, &BackRoom, &Garage};

for (size_t i = 0; i < sizeof DisplayList/ sizeof DisplayList[0]; i++) {
    if(DisplayList[i]->getDisplayNumber() >= 42){     
       Serial.print("Life, the Universe, and Everything is playing on Display ");
       Serial.println(DisplayList[i]->getDisplayNumber());
    }
}

//OKAY, so it's a dumb example, but it provides a full example. 


Okay, so now here is the next level of complexity which I can't figure out (yet) how to make happen. I want to have the Displays class inherit the library class LiquidCrystal, and then I need to have that inherit the interface class, iInterface. Since LiquidCrystal is a library that I have included in my main.cpp, how might I accomplish this?

simplified I need:
1
2
3
4
5
class iInterface
...
class LiquidCrystal : public iInterface
...
class Displays: public LiquidCrystal


but short of copying and pasting the LiquidCrystal class into my code, which would be silly and would not track changes to that library, I am totally unclear how one might do this. Clearly, I am missing something basic, but have not stumbled onto it yet. Any help is greatly appreciated.




Last edited on
Are we to understand that you don't control the LiquidCrystal class? That you have a header file containing the class declaration, and an already compiled binary library containing its function implementations?

Clearly, I am missing something basic

If you're hoping there's some magic way in C++ to make the LiquidCrystal class inherit from something, without touching the class declaration, there isn't. You could probably write some tooling to do it for you every time you built to save you having to manually update the source code of that class when someone else changes it, but that's just automating what has to be done.
Last edited on
make a wrapper
1
2
class myLiquidCrystal: public LiquidCrystal, public iInterface;
class Display: public myLiquidCrystal;
Thank you ne555. That (monostable pulse of)* advise was just what I needed to move forward.

I appreciate your time and experience. I will be sure to pay it forward.

* https://www.ti.com/product/NE555 ;^)
Last edited on
Worked great! Here is a TinkerCAD demo of this combination of wrapper class, inheritance, and interface class. (just a demo that really just shows the concepts in action and does nothing useful, but it is the underlying proof-of-concept for a 16+ display system interfaced to MIDI for an electronic music system).

https://www.tinkercad.com/things/iz3CShVUVaM
Topic archived. No new replies allowed.