Questions on OOP in MQL5 - page 34

 
Koldun Zloy:

If you're using classes for something you don't understand, it's not OOP.

And yes, I advise you, until you understand the need for these gimmicks, don't use them.

With such an approach, understanding will never come.
 
Alexey Viktorov:
With this approach, understanding will never come.

I don't think that with improper use, understanding can come.

 
Alexey Viktorov:

Igor, do you remember the task the Tsar gave to Fedot the Streltsy?

And what was the reply of the two young men?

How can you try to reproduce something, if you don't know the rules, or the final result to be obtained...

Well, that's about right, here's an imitation of the fact that news times and the amount of news differ from country to country:

enum ENUM_COUNTRY {RU,EN,FR };
//+------------------------------------------------------------------+
interface INews
{  void PrintNews(); };
//+------------------------------------------------------------------+
class NewsRU:public INews
{
private:
   datetime          ru_01,ru_02,ru_03;
public:
                     NewsRU(datetime t1,datetime t2,datetime t3):ru_01(t1),ru_02(t2),ru_03(t3)   { Print(__FUNCTION__);}
   void              PrintNews()                                                                 { Print("News №1 in ",ru_01); Print("News №2 in ",ru_02); Print("News №3 in ",ru_03); } };
//+------------------------------------------------------------------+
class NewsEN:public INews
{
private:
   datetime          en_01,en_02;
public:
                     NewsEN(datetime t1,datetime t2): en_01(t1),en_02(t2)                       { Print(__FUNCTION__);}
   void              PrintNews()                                                                { Print("News №1 in ",en_01); Print("News №2 in ",en_02);} };

//+------------------------------------------------------------------+
class NewsFake:public INews
{
public:
                     NewsFake() { Print(__FUNCTION__);}
   void              PrintNews() {} };
//+------------------------------------------------------------------+
class CCountryNews
{
private:
   INews             *arr[];
public:
                     CCountryNews(ENUM_COUNTRY &c[]);
                    ~CCountryNews()                                                            { for(int i=0; i<ArraySize(arr); i++) delete arr[i];             }
   void              PrinALLNews()                                                             { for(int i=0; i<ArraySize(arr); i++) arr[i].PrintNews();        }   };
//+------------------------------------------------------------------+
CCountryNews::CCountryNews(ENUM_COUNTRY &c[])
{  int sz = ArraySize(c);
   ArrayResize(arr,sz);
   for(int i=0; i<sz; i++)
   {  switch(c[i])
      {  case RU: arr[i] = new NewsRU((datetime)111, (datetime)222, (datetime)333 );   break;
         case EN: arr[i] = new NewsEN ((datetime)444, (datetime)555);                  break;
         default: arr[i] = new NewsFake();   
Print("Error, not this type ", EnumToString(c[i])); } } }
void OnStart()
{  ENUM_COUNTRY arrinput[] = {RU,EN,FR };
   CCountryNews CountryNews(arrinput);
   CountryNews.PrinALLNews();
}
//+------------------------------------------------------------------+


but it remains to be seen whether you need to wrap it up in a PLO.

;)

here's a log of how it was called and what it ended up with:

2019.09.08 16:00:35.031 tst (EURUSD,H1) NewsRU::NewsRU

2019.09.08 16:00:35.032 tst (EURUSD,H1) NewsEN::NewsEN

2019.09.08 16:00:35.032 tst (EURUSD,H1) NewsFake::NewsFake

2019.09.08 16:00:35.032 tst (EURUSD,H1) Error, not this type FR

2019.09.08 16:00:35.032 tst (EURUSD,H1) News #1 in 1970.01.01 00:01:51

2019.09.08 16:00:35.032 tst (EURUSD,H1) News #2 in 1970.01.01 00:03:42

2019.09.08 16:00:35.032 tst (EURUSD,H1) News #3 in 1970.01.01 00:05:33

2019.09.08 16:00:35.032 tst (EURUSD,H1) News #1 in 1970.01.01 00:07:24

2019.09.08 16:00:35.032 tst (EURUSD,H1) News #2 in 1970.01.01 00:09:15

 
Koldun Zloy:

I don't think that with improper use, understanding can come.

At the very least, an understanding will come that "there are no fish here".))
 
Alexey Viktorov:
At least there will be an understanding that "there are no fish here")).

We have heard this many times from some local characters.

And that's just from a lack of understanding.

And there are fish!

 
Igor Makanu:

well that's about it, here's a simulation that the news times and number of news items are different for different countries:


but it remains to be seen whether you need to wrap it up in an OOP.

;)

here's a log of how everything was called and what it ended up with:

No, Igor. This is not such an approach.

bool  CalendarValueHistory( 
   MqlCalendarValue&  values[],              // массив для получения описаний значений  
   datetime           datetime_from,         // левая граница диапазона времени 
   datetime           datetime_to=0          // правая граница диапазона времени 
   const string       country_code=NULL,     // кодовое имя страны по ISO 3166-1 alpha-2 
   const string       currency=NULL          // кодовое наименование валюты страны  
   );

The highlighted strings are three use cases. At the same time, the time range changes. Then the values[] array is processed in a certain way. The event id is used to get the description of this event. Its importance, time and other attributes.

 
Koldun Zloy:

We have heard this many times from some local characters.

And that's just from a lack of understanding.

And there are fish!

I've read about it. There are fish, but not where you don't understand. What kind of project should be in mql5 for there to be fish... I want to see at least one project in mql5, where the need for OOP would be seen.
 
Alexey Viktorov:
I've read about it. There are fish, but not where you don't understand. What kind of project should be in mql5 for there to be fish... I want to see at least one project in mql5, where the need for OOP would be seen.

Probably, there is no need for OOP. In principle, everything can be done in a structured style. But personally, my first urge started when I decided to use structures instead of sets of global variables, which had begun to grow dangerously large. I had already moved on from structures to classes because it seemed logical to integrate functions that processed data from those structures directly into them, which led to the creation of classes. This is not a matter of necessity, but simply arranging the data and working with them.

 
BlackTomcat:
It is not necessary to "kill" the object as soon as it is created and has done its job.
The created objects can be "killed" in the OnDeinit function after the MQL-program is finished,
While the program is running, all objects may be kept in memory and can be accessed.

If the object has performed its task, why keep it in memory?
Won't a memory leak occur?

 
Alexey Viktorov:

No, Igor. That's not the approach here.

The highlighted lines are the three use cases. In this case the time range changes. Then the values[] array is processed in a certain way. The event id is used to get the description of this event. Its importance, time and other attributes.

the approach is not important

If you want to understand OOP, my opinion ( I've already written ) - it's convenient, but OOP is just a paradigm, well, a way of writing, which combines several OOP concepts - Wiki...


So you can try the opposite : here's your task, you need to break it down into data and ways of processing the data...

1. Where will you store the data? - most likely a structure

2. How will you process the data? - Most likely a set of functions.

3. How will you initialise the data? - most likely an array of structures, and you will need to zero in this array and then fill it with data

4. How will you ensure the flexibility of previously written code - refactoring ?

Now if you use OOP:

1. in fields in a class, although maybe this field will be a structure, or maybe I will write a base class at all which will store the data and do inheritance OR this class will be a field in the class under discussion now

2.1. it will be a set of methods. If I write a base class, I will probably create methods in a base class that will do basic data processing, and if I inherit from a base class, these methods will be available in the class - it's inheritance!

2.2. If I want to change just one method after inheriting from the base class, I will not rewrite anything in the base class, it is not necessary! - i will just write a method ( a function!) with the same name as the method of the base class, and it will be inheritance!

3. it will be a constructor, and if I don't write a constructor, it will be called implicitly, so I keep in mind that every class I inherited from and / or class in my fields will always have a constructor called; and OOP gives me the ability to not write a constructor, to write a constructor without parameters, to write a dozen more constructors with parameters

4. using OOP you don't have to rewrite previously created code fragments, you can inherit, you can .... you can also make a mistake, the compiler will clean up after the programmer in most cases!


well, this is my rather amateurish view on OOP, in general, it is convenient and in order to make everything work efficiently, the main work when using OOP, not the programmer, but the compiler developers, so that the fields / methods are not used not to include in the compiled file, where the programmer made a mistake, well, to warn him)))

Reason: