class in a class

Hi all.

With some help here i've created a library for a small serial oled display. I create an instance with code like this;

uOLED uoled(8,256000, &Serial1);  
// reset=pin8, baudrate=256k and serial port to use =Serial1

After that the functions in the library (draw line etc.) use SerialX.write statements where X is the serial port mentioned.

In addition to that i've made another library for drawing bar-graphs. An instance is made with a line like

bargraph graph1(_lot_of_arguments_here);

This graph-library has to use the functions of the uoled instance.

[u]The question;[/u] How do i 'glue' those 2 libraries together so that the graph-library is a 'child' of the uOLED-library.

example of desired functionality;

uOLED uoled1(8,256000, &Serial1);    // reset=pin8, baudrate=256k and serial port to use =Serial1
uOLED uoled2(8,256000, &Serial2);    // reset=pin8, baudrate=256k and serial port to use =Serial2
 // 2 displays on different serial ports

create an instance of bargraph in instance uoled1
create another instance of bargraph in instance uoled1

create an instance of bargraph in instance uoled2
create another instance of bargraph in instance uoled2

graph-lib code http://arduino.pastebin.com/f659b045

uOLED-lib code http://arduino.pastebin.com/f2d926db2

I have googled with keywords c++, inheritance but i did not understand enough to solve this on my own.

Thanks in advance,

Jeroen

Since the bargraph class is not meant to be standalone, I'd include it's definition in the uoled class' header file.

In the uoled class, declare an instance of the bargraph class.

Change the constructor for bargraph to take no arguments, and add a drawGraph method that take all the arguments that the constructor took.

Add a method to the uoled class that calls the bargrapgh::drawGraph method.

Combine the bargraph and uoled class implementation into one file. Don't advertise the bargraph capabilities as a stand-alone class.

Thanks for your comment PaulS.

Since the bargraph class is not meant to be standalone, I’d include it’s definition in the uoled class’ header file.

That’s my intention, yes. That the user would only have to “#include uoled.h” in the sketch

In the uoled class, declare an instance of the bargraph class.

And already you lost me.

class uOLED 
{
  public:
    uOLED (int pin, long baud, HardwareSerial *serialToUse);
    bargraph(_removed_arguments_);[glow]<- you tried to tell me this?[/glow]

// all the other methods/functions/variables
}

or

class uOLED 
{
  public:
    uOLED (int pin, long baud, HardwareSerial *serialToUse);

// all the other methods/functions/variables
}
class barGraph
{
  public 
    bargraph(_removed_arguments_);[glow]<- or this?[/glow]

// all the other methods/functions/variables
}

Change the constructor for bargraph to take no arguments, and add a drawGraph method that take all the arguments that the constructor took.

I could manage this. Some typing/copypasting.

Add a method to the uoled class that calls the bargrapgh::drawGraph method.

So when this method get called from a sketch a new bargraph instance would be created? The method in the uoled class would have to take an argument to reference the instance?

Combine the bargraph and uoled class implementation into one file.

I would rather avoid this as i find it convenient to have multiple files. Basically splitting ‘basic’ functions and ‘extended’ functions build on the ‘basic’ ones

Don’t advertise the bargraph capabilities as a stand-alone class.

In the sense of ‘here is a general graph library’ to use on every display? That was definitely not my intention.

After a day trying i’m still getting nowhere unfortunately. PaulS or anyone else willing to clarify?

regards,

Jeroen

In the uoled class, add:

bargraph myGrapher; // Define an object of the bargraph class

This defines an instance of the class, resulting in a call to the bargraph constructor, which should take no arguments.

The uoled class would get a new method:

drawGraph(//some arguments go here);

The uoled::drawGraph() method would simply call the bargraph object's methods:

drawGraph()
{
    myBarGraph.DoWhateverNeedsToBeDone();
}

If you have just one header file, with both classes defined in it, you'll need to combine the implementations, too. There is a limitation in the IDE that only compiles and links in source code files whose header files are included in the sketch.