My latest task found me wanting to create a library that encapsulated the functionality of multiple libraries that already existed into one library interface. This is referred to as a parent/child relationship, or a base class and a derived class. The parent is the base class and it’s features can be inherited by the child class. The child class is also called the derived class, as it’s functionality and features are derived from the base class. I’m not going to get into access modifiers, such as public, protected, private, etc. In my case, however, I was wanting to have one child class, a library interface to a common Arduino shield, which was derived from multiple parents, or base classes.
I fumbled a little figuring out how to access the initial instantiated objects of the parent classes from within the child class without dynamically allocating a new object and assigning a pointer during run time. The simple answer, which I found via an educated guess based on a compiler error, was to use the “::”, or double colon, operator.
Now, I am able write a wrapper that incorporates the functionality of existing libraries all through one library with one instantiation in my Arduino sketch. The shell libraries I created have comments throughout to explain and can be downloaded from my GitHub repository here. I hope these will provide a good starting point for building a child class library from multiple parent class libraries, in case your C++ skills are a tad rusty. I also hope to finalize and release the actual Arduino library that inspired this effort in the near future. Until then, stay tuned and thanks for reading.