How to write an array of class?

 

I am new to OOP, and I have read some articles on it, but I do not know how to implement it.

an order class has ticket, open_time.

 

MyOrders have 10 tickets in array

I would like to call them by

MyOrders[0].ticket = 123;

 

how do I do it? 

 

 

 

int max_order = 10;

class Order
{
   private:
      int         ticket(); 
      datetime    open_time();
      int         type();
      double      lots;
      string      symbol;     
      double      open_price;
      double      take_profit;
      int         magic_number;
};

class Orders : public Order[max_order]
{
   public:
      void        Orders();
      void        Print_Ticket();
      
   private:
      int         counter;
}

void Orders::Orders()
{
   abc = 0;
}

void Orders::Print_Ticket()
{
   printf("Ticket Number: " + Order[counter].ticket;
}

 

 
Th easiest way is to use CArrayObj, you do not store with tickets but rather pointers to instances of the class Order you defined.
 

Could you show me an example? 

 

I know about pointers, they are storing addresses. But how do I reference to the next one?

 

class order {

private:

int ticket;

public:

void enter_ticket(); 

}  ;

 

They are referenced with the At() method.

Look at the documentation example: https://www.mql5.com/en/docs/standardlibrary/datastructures/carrayobj/carrayobjat

Documentation on MQL5: Standard Library / Classes of data / CArrayObj / At
Documentation on MQL5: Standard Library / Classes of data / CArrayObj / At
  • www.mql5.com
Standard Library / Classes of data / CArrayObj / At - Reference on algorithmic/automated trading language for MetaTrader 5
 

?

class order {

private:

int ticket;

public:

void enter_ticket(int tick){ticket=tick};

}  ;

...
order first;
first.enter_ticket(5468);
 

Modified your class version. I have not tested so there may be errors, but it's more or less like the ff:

int max_order = 10;


class Order : public CObject
{
   private:
      int         ticket;
      datetime    open_time;
      int         type;
      double      lots;
      string      symbol;     
      double      open_price;
      double      take_profit;
      int         magic_number;
      double      ATR;
      bool        losing_order;
      double      next_Win_Entry;
      double      next_Lose_Entry;
      double      next_LotSize;
      
   public:
      void        Order(void){};
      void        set_ticket (int value) {ticket=value;};
      int         read_ticket (void) {return ticket;};
};
      

CArrayObj MyOrders;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
      Order *order1 = new Order();
      order1.set_ticket(1);
      Order *order2 = new Order();
      order2.set_ticket(2);
      Order *order3 = new Order();
      order3.set_ticket(3);

      MyOrders.Add(order1); //add first order object
      MyOrders.Add(order2); //add second order object
      MyOrders.Add(order3); //add third order object

      Print(MyOrders.Total()); //returns total number of orders = 3
      
      //iterate over orders within MyOrders
      for (int i=0;i<MyOrders.Total();i++)
      {
         Order *item = MyOrders.At(i); //get order object at index i
         Print(item.read_ticket()); //print ticket of order object at index i
      }

      return(INIT_SUCCEEDED);
  }


CArrayObj already has a built-in array in its private class members. The array is of type CObject, so the objects that you would add to this CArrayObj should also inherit from CObject (hence

class Order : public CObject

). There is no need to declare an additional array.

For this function:

return_ticket_of_largest_open_price()

CArrayObj won't be able to provide that, but you can declare a new Orders object that inherits from CArrayObj, use that object instead of CArrayObj, and declare the member function there.