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.