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.

Member Avatar for jencas

That's really all of the code?!?

you can put the sumAll function in baseClass. The printAll can be made virtual and also moved to base class. This way you can sum up the values in base(since both are int's) and then print the correct message out. i'm not sure why you're using so many static's and how you are initialising the values etc.

you can put the sumAll function in baseClass. The printAll can be made virtual and also moved to base class. This way you can sum up the values in base(since both are int's) and then print the correct message out. i'm not sure why you're using so many static's and how you are initialising the values etc.

If I make the printAll function virtual, but then how will the base class access that the derived classes' static sumall variable.

I'm using the statics because I want to keep the track of the total of all the a's for both mangoes and apples separately.

the example is not too easy to read and follow, i just gave a generic answer. If you need to keep a counter of sorts in each class then using a static varible is ok.

But this is very inelegant. I dont want to have same copies of a single function. Moreover, apple and mango are just examples, my real program contains 5 copies.

Member Avatar for jencas

That's really bad design in my opinion. Summing up should not be task of your class. Let your class return the number of fruits and calculate the sum using a STL algorithm. Put only code in the derived classes to be able to distinguish between the different sort of fruits.
You are on the right way if you do not have to change the code for summing up if adding another fruit derived class.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.