Good morning, I'm still finding my way around Arduino and using classes.
I would like to have an array of oscillators/waves and a parent class that holds values that relate to all the child instances. I'm using the "static" type, it's acting as I expected and I just want to check if this is the right approach.
class parent_wave {
public:
static int sum;
static void sum_waves(int s){
sum += s;
}
};
int parent_wave::sum = 0;
class child_wave: public parent_wave {
public:
void set_value(int v){
_v = v;
sum_waves(_v);
};
private:
int _v;
};
child_wave wave[2];
void setup() {
wave[0].set_value(10);
wave[1].set_value(20);
}
void loop() {
}
Why a parent class? You could define your static values in the 'child_wave' class directly and they are related to all instances of your class 'child_wave'.
Or is there another reason for this 'parent_class'?
However do realise. If you had a container of your different child classes (an array of parent class) you wont be able to access all the child class specific variables.
I've done a bit more work on my oscillator class and it's all working as I hoped
I would like to have the wavemixer() function somehow incorporated into the oscillator class. What's a good way to do this? It simply adds the Y values of the classes together.
Although, what do I do if I want to make the oscillator array larger? For example sometimes a system might run with 5, 10 or 15 individual oscillators.
That's why I was originally thinking of the parent and child classes. With wavemixer being a function of the parent class summing up each of the individual child oscillators.
Besides set_rate, nothing else in the oscillator needs to be accessible. Just the sum output from the wavemixer.
static float wavemixer( Oscillator * oscillators, size_t count )
{
float wave = 0; // Never forget to initialize a local variable if you care about its initial value
for ( size_t i = 0; i < count; ++i )
{
Oscillator & oscillator = oscillators[i];
oscillator.make();
wave += oscillator._Y;
}
return wave;
}
...
Oscillator oscillators[2];
...
Oscillator::wavemixer( oscillators, 2 );
I think I've come to a solution using nested classes. This seems nice and neat concise. Besides sum() and set_rate() everything else is kept out of the way.