Download MetaTrader 5
To add comments, please log in or register
Do you have Mac OS? Find out how to install MetaTrader on it!
Claudio Arrau
104
Claudio Arrau 2013.07.17 22:07 

I am kind of stuck! :(

I am using the example for OOP in one of the articles here.

It is clear how to create a class, but how can I use it?

In the MyDateClass example, several DateTime variables are declared (as int).

But how can I use them later in the code?

I managed to use classes where variables or functions were defined in the public section, something like my_class.XXX,

no problem. But in example below, how do I use it? Or is it maybe that the example is supposed to be used only inside of a class?
THANKS!


//+------------------------------------------------------------------+

class MyDateClass
  {
private:
   int               m_year;          // Year
   int               m_month;         // Month
   int               m_day;           // Day of the month
   int               m_hour;          // Hour in a day
   int               m_minute;        // Minutes
   int               m_second;        // Seconds
public:
   //--- Default constructor
                     MyDateClass(void);
   //--- Parametric constructor
//                     MyDateClass(int h,int m,int s);
  };
  
  
MyDateClass::MyDateClass(void)
  {
//---
   MqlDateTime mdt;
   datetime t=TimeCurrent(mdt);
   m_year=mdt.year;
   m_month=mdt.mon;
   m_day=mdt.day;
   m_hour=mdt.hour;
   m_minute=mdt.min;
   m_second=mdt.sec;
   Print(__FUNCTION__);
  }
  
//+------------------------------------------------------------------+

void OnStart()
  {
MyDateClass my_dc;

//?????????????????????????????????
Print(my_dc(???);
Print(my_dc.?????);
//?????????????????????????????????   
  }




//+------------------------------------------------------------------+
Claudio Arrau
104
Claudio Arrau 2013.07.18 14:26  
anyone has an idea? is it only that the example was meant to be used only within a class?
Luis Sampaio
146
Luis Sampaio 2013.07.18 14:36  
claudio_arrau2:
anyone has an idea? is it only that the example was meant to be used only within a class?
You have to create public get methods to have access to the private variables... try a quick search "object-oriented" and you can see many examples. 
Jordi Bassaganas
3525
Jordi Bassaganas 2013.07.18 14:41  
claudio_arrau2:
anyone has an idea? is it only that the example was meant to be used only within a class?
That's it!, you have to create the so-called getter and setter methods, which must be public, to access those private variables from outside the class. You can also decalre those variables public, though this does not adhere the "encapsulation" principle of OOP.
Claudio Arrau
104
Claudio Arrau 2013.07.18 14:54  

Well, I sort of understood that even; what was weird for me, that in the documentation there are many examples how to generate classes, structures etc.,

but then almost nothing explaining how to use them in the main program....

But well, 20 years of procedural programming is quite hard in my brain :)

Jordi Bassaganas
3525
Jordi Bassaganas 2013.07.18 15:06  
claudio_arrau2:

Well, I sort of understood that even; what was weird for me, that in the documentation there are many examples how to generate classes, structures etc.,

but then almost nothing explaining how to use them in the main program....

But well, 20 years of procedural programming is quite hard in my brain :)

There are some articles explaining how you can instantiate objects of your cusom types, for instance you can have a look at The Basic of Object-Oriented Programming. I mean that there's more info in MQL5 Programming Articles. Regards!
Jordi Bassaganas
3525
Jordi Bassaganas 2013.07.18 15:15  
claudio_arrau2:

Well, I sort of understood that even; what was weird for me, that in the documentation there are many examples how to generate classes, structures etc.,

but then almost nothing explaining how to use them in the main program....

But well, 20 years of procedural programming is quite hard in my brain :)

I attach here an example of an OOP wrapper so that you can work with indicators in a conceptual level, thinking in OOP terms. Please observe how you access private properties through the public getter and setter methods:

class CBearsPower
   {
protected:   
   int m_handler;
   double m_buffer[];
               
public:
   //--- Constructor and destructor methods
                           CBearsPower(void);
                           ~CBearsPower(void);
   //--- Getter methods
   int                     GetHandler(void);
   void                    GetBuffer(double &buffer[], int ammount);
   //--- Setter methods
   bool                    SetHandler(string symbol, ENUM_TIMEFRAMES period, int ma_period);
   bool                    SetBuffer(int ammount);
   //--- CBearsPower specific logic
   };
   
CBearsPower::CBearsPower(void)
   { 
      ArraySetAsSeries(m_buffer, true);   
   }
               
CBearsPower::~CBearsPower(void)
   {
      IndicatorRelease(m_handler);
      ArrayFree(m_buffer);
   }
        
int CBearsPower::GetHandler(void)
   {
      return m_handler;
   }

void CBearsPower::GetBuffer(double &buffer[], int ammount)
   {
      ArrayCopy(buffer, m_buffer, 0, 0, ammount);
   }
      
bool CBearsPower::SetHandler(string symbol, ENUM_TIMEFRAMES period, int ma_period)
   {   
      if((m_handler = iBearsPower(symbol, period, ma_period)) == INVALID_HANDLE)
      {
         printf("Error creating Bears Power indicator");
         return false;
      }
      return true;
   }
   
bool CBearsPower::SetBuffer(int ammount)
   {   
      if(CopyBuffer(m_handler, 0, 0, ammount, m_buffer) < 0) 
      { 
         Alert("Error copying Bears Power buffers, error: " , GetLastError());
         return false;
      }      
      return true;
   }

Then you can use the class above in another context, for instance:

class CTechIndicators
   {
protected:
   CBearsPower*           m_bearsPower;
   CBullsPower*           m_bullsPower;
                  
public:
   //--- Constructor and destructor methods
                           CTechIndicators(void);
                           ~CTechIndicators(void);
   //--- Getter methods
   CBearsPower*            GetBearsPower(void);
   CBullsPower*            GetBullsPower(void);
   };
   
CTechIndicators::CTechIndicators(void)
   {
      m_bearsPower = new CBearsPower;
      m_bullsPower = new CBullsPower;      
   }
               
CTechIndicators::~CTechIndicators(void)
   {
      delete(m_bearsPower);
      delete(m_bullsPower);
   }
        
CBearsPower* CTechIndicators::GetBearsPower(void)
   {
      return m_bearsPower;
   }
   
CBullsPower* CTechIndicators::GetBullsPower(void)
   {
      return m_bullsPower;
   }
Claudio Arrau
104
Claudio Arrau 2013.07.18 16:48  
oh Thanks! That's very helpful!
/
To add comments, please log in or register