2

I have two subclasess inheriting from the same abstract superclass. There is a common operation to all subclasses which depends on several attributes. Let me explain with an example:

Say this is superclass A (A is abstract):

class superClass
{
  int valueA;
  int valueB;
  float* array;

   public superClass(){
      array[valueA + valueB]
   }

   virtual foo(){
   }

}

And these are my subclasses:

class firstSubClass: superClass
{

   public firstSubClass():superClass(), valueA(100),valueB(2){
   }

   foo(){
   }

}

class secondSubClass: superClass
{

   public secondSubClass():superClass(), valueA(50),valueB(3){
   }

   foo(){
   }

}

Will the array be properly initialized? Which translates to, is the subclass constructor called before the superClass one, or is it the other way around?

Is there a way to make the initialization behavior common to both subclasses by putting it into the superClass?

Thanks in advance.

Yun Huang
  • 4,256
  • 7
  • 27
  • 36
M Rajoy
  • 4,028
  • 14
  • 54
  • 111

2 Answers2

4

The superClass constructor is called first.

The superClass constructor should be responsible for initializing the superClass's data members and firstSubClass should initialize its own data members and call the superClass's constructor;

More info on the constructor's calling order here and you might also want to read about constructor initializer lists here

So, I would define superClass , firstSubClass and secondSubClass as :

class superClass
{
  int valueA;
  int valueB;
  float* array;

  public:

   superClass( int a , int b ): valueA(a),valueB(b) {

      array = new float[valueA + valueB];
   }
   virtual foo(){
   }

}



class firstSubClass: public superClass
{

   public:

   firstSubClass():superClass(100,2){ //calls superclass's constructor
   }

   foo(){
   }

}

class secondSubClass: public superClass
{

   public :

   secondSubClass():superClass(50 , 3){
   }

   foo(){
   }

}
Aman Aggarwal
  • 3,905
  • 4
  • 26
  • 38
1

If I understood well, you want superclass to create (not just initialize) an array of floats when you're constructing any of subclasses. When creating an instance of inherited class, base class constructor is invoked first and you need to pass valueA and valueB values to it - you need to add them as constructor arguments:

superClass::superClass(int valueA, int valueB) : 
   valueA(valueA), valueB(valueB), array(0)
{
   // now create an array of requested length - allocate memory for it
   array = new float[valueA + valueB];
}

// don't forget to deallocate memory for it (possibly in destructor)
superClass::~superClass()
{
   delete[] array;
   array = 0;
}

Then, when you create instances of inherited class, you can provide valueA and valueB as parameters of superclass constructor:

firstSubClass::firstSubClass() : superClass(100, 2)
{
   //...
}

It is not good practice to use hardcoded values (magic numbers) so it would be better to add valueA and valueB as subclass constructor arguments:

firstSubClass::firstSubClass(int valueA, int valueB) : superClass(valueA, valueB)
{
   //...
}

Finally, you should avoid arrays. This is C++, use std::vector<float>, it is much safer and easier to use!

Bojan Komazec
  • 9,216
  • 2
  • 41
  • 51
  • 1
    @kelmer - vectors are simply wrappers around arrays, they make things easier for you but are unlikely to be less efficient. – Peter Wood Feb 21 '12 at 12:09
  • 1
    @kelmer Performance difference is non-existing compared to high level of possible bugs with error prone and hard to maintain arrays. – Bojan Komazec Feb 21 '12 at 12:10
  • Thanks guys, I will follow your advice then :) – M Rajoy Feb 21 '12 at 12:12
  • @kelmer and have a look at this question - it will give you some hints about this question: http://stackoverflow.com/questions/381621/using-arrays-or-stdvectors-in-c-whats-the-performance-gap – Bojan Komazec Feb 21 '12 at 12:13