How do I create constructor in my class

 

Hi

I am very to MQL4 and I am a learner at the moment. I am creating a simple class as shown below. I would to add constructor in my class, so I can use to build object.

I have this simple script, but getting error;

#property strict

void OnStart(){

   CurrencyCount currencyCount1 = CurrencyCount("USD",3);

}

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

class CurrencyCount {

   public:

      string currency;

      int count;


  // Class constructor 

   public:

   CurrencyCount(string incurrency, int incount){

      this.currency = incurrency;

      this.count = incount;

   };

}; 

I am getting error when I try to compile. 

Please can someone help.

Thank you.

 
ishf fady:

Hi

I am very to MQL4 and I am a learner at the moment. I am creating a simple class as shown below. I would to add constructor in my class, so I can use to build object.

I have this simple script, but getting error;

#property strict

void OnStart(){

   CurrencyCount currencyCount1 = CurrencyCount("USD",3);

}

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

class CurrencyCount {

   public:

      string currency;

      int count;


  // Class constructor 

   public:

   CurrencyCount(string incurrency, int incount){

      this.currency = incurrency;

      this.count = incount;

   };

}; 

I am getting error when I try to compile. 

Please can someone help.

Thank you.

try below:

#property strict
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
class CurrencyCount
  {
public:
   string            currency;
   int               count;
public:
                     CurrencyCount(string incurrency, int incount)
     {
      this.currency = incurrency;
      this.count = incount;
     };
  };
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnStart()
  {
   CurrencyCount CurrencyCount1("USD",3);
   Print(CurrencyCount1.currency," ",CurrencyCount1.count);
//output:
//USD 3
  }
//+------------------------------------------------------------------+
 

Thank you very much  Tsungche Kuo

Much appreciated

Tsungche Kuo
Tsungche Kuo
  • www.mql5.com
Trader's profile
Reason: