Class only with private parameters: how to use?

 

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.?????);
//?????????????????????????????????   
  }




//+------------------------------------------------------------------+
 
anyone has an idea? is it only that the example was meant to be used only within a class?
 
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. 
 
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.
 

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 :)

 
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!
 
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;
   }
 
oh Thanks! That's very helpful!
Reason: