Hello all,
I have a base class called reuse_pattern and two derived classes apple_reuse_pattern, mango_reuse_pattern.
Actually, the derived classes are only there to keep track of some static variables and they are all completely same except for the word mango and apple. I find that this is a very inelegant way of doing it because every thing is same but my principle requirement in this case is separate sum_a for both mango_reuse_pattern and apple_reuse_pattern for which I have created the two derived classes Can someone please suggest a better and more elegant way of designing the whole program so that so much code repetition does not occur.
class reuse_pattern
{
int a;
int fun(int);
}
class mango_reuse_pattern::public reuse_pattern
{
static int sum_a;
void sumAll();
static void Printall();
}
int mango_reuse_pattern::a;
void mango_reuse_pattern::sumAll()
{
sum_a+=a;
}
class apple_reuse_pattern::public reuse_pattern
{
static int sum_a;
void sumAll();
static void Printall();
}
int apple_reuse_pattern::a;
void apple_reuse_pattern::sumAll()
{
sum_a+=a;
}
int main()
{
mango_reuse_pattern myMRP[10];
apple_reuse_pattern myARP[10];
/*some code that gives values to both*/
for(int i=0;i<10;i++){ myMRP[i].sumAll();}
for(int i=0;i<10;i++){ myARP[i].sumAll();}
mango_reuse_pattern::printAll();
apple_reuse_pattern::printAll();
}
I have no problem with main, but I find that the same code is being repeated in the mango_reuse_pattern and apple_reuse_pattern which is very inelegant. Please suggest a better code.
Thanks.