Learning OOP - Setting up child class contructor when base class constructor accepts paratmeters

 

Hey everyone, learning to uses classes to neaten up my coding and get with the times.

I am doing a basic moving average class set. I have created the base moving average class  - which seems to work ok. Now I am trying to make an exponential moving average extended class, but having a bit of trouble with the syntax...

 

class myMovingAverage {
   private:
      int maPeriod;
      int maMethod;
      int maAppliedPrice;
   
   //constructors
   public:
      myMovingAverage(int period, int shift);    
      ~myMovingAverage();
   
   //variables
   public:     
      string   maSymbol;
      int      maTimeframe;
      int      maShift;
   
   //methods
   public:     
      int               showPeriod(void);
      virtual double    getValue(int candleShift);
      void              updateShift(int shift);
      void              updateSymbol(string newSymbol);
      
   private:
       
  };
  
   //+------------------------------------------------------------------+
   //|                                                                  |
   //+------------------------------------------------------------------+
   myMovingAverage::myMovingAverage(int period, int shift) {
         this.maPeriod       = period;   
         this.maSymbol       = Symbol();
         this.maTimeframe    = Period();
         this.maMethod       = MODE_CLOSE;
         this.maAppliedPrice = MODE_SMA;
         this.maShift        = shift;
     }
   //+------------------------------------------------------------------+
   //|                                                                  |
   //+------------------------------------------------------------------+
   myMovingAverage::~myMovingAverage()  {
  }
//+------------------------------------------------------------------+

   int myMovingAverage::showPeriod() {     
      return(this.maPeriod);
   }
   
   void myMovingAverage::updateSymbol(string newSymbol) {
      this.maSymbol = newSymbol;
   }
   
   double myMovingAverage::getValue(int candleShift = 0) {
      if(!candleShift) { candleShift = this.maShift; }
      return(iMA(this.maSymbol, this.maTimeframe, this.maPeriod, 0, this.maAppliedPrice, this.maMethod, candleShift));
   }

 

Now when I try to set up the child class I get a compile error...

class myEMA : public myMovingAverage {
   private:
      int maMethod;
   
   public:
      myEMA(void);
      ~myEMA(void);      
};

   myEMA::myEMA() {
      this.maMethod = MODE_EMA;
   }

 

 I am guessing it is because the base class takes parameters in the constructor. I also tried...

class myEMA : public myMovingAverage {
   private:
      int maMethod;
   
   public:
      myEMA(int period, int shift);
      ~myEMA(void);      
};

   myEMA::myEMA(int period, int shift) {
      this.maMethod = MODE_EMA;
   }

 

I am still getting a wrong parameters count error for the constructor. Please help set me straight.

 

When there is no implicit constructor in the class you extend, you must explicitly call it:

myEMA(int period, int shift):myMovingAverage(period, shift) {
  this.maMethod = MODE_EMA;
}
 
  1. You must construct the base class in a initialization list. Structures and Classes - MQL4 Documentation
       myEMA::myEMA(int period, int shift)
          : myMovingAverage(period, shift){}
    
  2. this.maPeriod       = period;   
    You don't need the "this."
  3. this.maMethod = MODE_EMA;
    maMethod is a private variable, will not compile.
 

Thanks guys, the compiler went through with no errors.

Nice to see you're still kicking around WHRoeder, you were helping me with coding issues in like 2010.

Does it matter if I use the "this." operator in the code? It just helps me personally read the code better from an OOP point of view.

I guess the correct question would be, would there be any negative effect of using the this statement?

 
dazamate: Does it matter if I use the "this." operator in the code? It just helps me personally read the code better from an OOP point of view.
Makes no difference. Most name their member variables mVariable, myVariable, or _variable to differentiate from local variables; this.variable is too verbose for me.
 
I am used to web languages, it's good practice for me :)
Reason: