Questions on OOP in MQL5 - page 67

 

Here's another question, here's theWiki articleAbstract Class

I'm interested in a C++ example

class CA { // Абстрактный класс
  public:
    CA ( void ) { std::cout << "This object of the class "; }

    virtual void Abstr ( void ) = 0; // Чистая (пустая) виртуальная функция.
    void         fun   ( void ) { std::cout << "Реализация не будет наследоваться!"; }

    ~CA () { std::cout << "." << std::endl; } //Вызывается в обр. порядке конструкторов
  };

class CB : public CA {
  public:
    CB ( void ) { std::cout << "CB;"; }

    void Abstr ( void ){ std::cout << " call function cb.Abstr();"; } //Подменяющая функция.
    void fun   ( void ){ std::cout << " call function cb.fun()"; }

    ~CB () {} // Неверно для абстр. кл. ~CB(){ ~CA(); } 
  };

class CC : public CA {
  public:
    CC ( void ) { std::cout << "CC;"; }

    void Abstr ( void) { std::cout << " call function cc.Abstr();"; } //Подменяющая функция.
    void fun   ( void ) { std::cout << " call function cc.fun()"; }

  ~CC () {} // Неверно для абстр. кл. ~CC(){ ~CA(); } 
  };

int main () {
  std::cout << "Program:" << std::endl;
  CB cb;
  cb.Abstr(); cb.fun(); cb.~CB();

  CC cc;
  cc.Abstr(); cc.fun(); cc.~CC();

  return 0;
  }

The result of the program:

Program:

This object of the class CB; call function cb.Abstr(); call function cb.fun().

This object of the class CC; call function cc.Abstr(); call function cc.fun().

.

.


interested in void fun ( void ) method :

- why is there no virtual specifier ?

- if we add virtual, what will it change later on ?


why and what for, here is the code:

class base
{
public:
   virtual void HighPriorityTask() {}
   virtual void Task() {}
};
//+------------------------------------------------------------------+
class A: public base
{
public:
   virtual void HighPriorityTask() { Print(__FUNCSIG__); }
};
//+------------------------------------------------------------------+
class B: public base
{
public:
   virtual void Task() { Print(__FUNCSIG__); }
};

//+------------------------------------------------------------------+
void OnStart()
{
   base *obj[4];;
   obj[0] = new A; obj[1] = new A;  
   obj[2] = new B; obj[3] = new B; 
   for(int i=ArraySize(obj)-1; i>=0; i--)
   {
      obj[i].HighPriorityTask();
      obj[i].Task();
   }
   
   for(int i=ArraySize(obj)-1; i>=0; i--)
      delete obj[i];

2020.05.28 14:41:20.294 tst (EURUSD,H1) void B::Task()

2020.05.28 14:41:20.298 tst (EURUSD,H1) void B::Task()

2020.05.28 14:41:20.298 tst (EURUSD,H1) void A::HighPriorityTask()

2020.05.28 14:41:20.298 tst (EURUSD,H1) void A::HighPriorityTask()

it works the way I want it to - I have a base class and inherit from it. But my descendants will need only one method and I want to call objects that have HighPriorityTask() method first and then Task() regardless of the sequence of declaration and initialization


all in one loop

is it possible to do this in a simple way?

 
Igor Makanu:

and of course all in one cycle.

is it possible by simple means?

And why the fuck would it need to be done in one cycle?

And why is it also natural?

Naturally there will be at least two cycles.

 
Koldun Zloy:

And why the fuck should it be done in the same cycle?

And why is it also natural?

Naturally there will be at least two cycles.

OK, two cycles means two cycles, so no miracle ((

 
Igor Makanu:

OK, 2 cycles means 2 cycles, so no miracle ((

You could try to get rid of loops altogether.
There are examples of how to print from 1 to 100 without loops and without recursion.
Maybe these examples will help, if it's even relevant ))

Печать от 1 до 100 на C ++, без цикла и рекурсии | Портал информатики для гиков
  • 2020.01.01
  • espressocode.top
Ниже приводится программа на C ++, которая печатает от 1 до 100 без цикла и без рекурсии. #include using namespace std;    template
 
Roman:
What is the point of this nonsense?
 
Igor Makanu:

Here's another question, here's theWiki articleAbstract Class

I'm interested in a C++ example

The result of the program:

Program:

This object of the class CB; call function cb.Abstr(); call function cb.fun().

This object of the class CC; call function cc.Abstr(); call function cc.fun().

.

.


interested in void fun ( void ) method :

- why is there no virtual specifier ?

- if we add virtual, what will it change later on ?


why and what for, here is the code:

2020.05.28 14:41:20.294 tst (EURUSD,H1) void B::Task()

2020.05.28 14:41:20.298 tst (EURUSD,H1) void B::Task()

2020.05.28 14:41:20.298 tst (EURUSD,H1) void A::HighPriorityTask()

2020.05.28 14:41:20.298 tst (EURUSD,H1) void A::HighPriorityTask()

it works the way I want it to - I have a base class and inherit from it. But my descendants will need only one method and I want to call objects that have HighPriorityTask() method first and then Task() regardless of the sequence of declaration and initialization


all in one loop

is it possible to do this in a simple way?

1. If you declare a base class pointer and create a child object, then when calling methods via this same pointer of the base class:

a) virtual methods are called by the child class (via the virtual function table specially created for this purpose)

b) non-virtual methods are called by base class. Even if they are overridden in the child class.

2. If you want to have a single loop, then

a) Sort the object array by type.

b) For specific tasks, keep a separate array of pointers to objects that solve these tasks. I.e. all the objects that must callHighPriorityTask

should be stored in a separate array, which should be called first.

h.e. How could you imagine that in general, without sorting, half of all objects will be executed first, and then the rest? :) Well, if only to start a job queue.

But this is a creative question, you can come up with a lot of sophisticated ways to solve a penny problem))) The main thing is not to encroach on the laws of physics and all will be well)))

 
Aleksey Mavrin:

2. If you want to use the same loop, then

a) Sort the array of objects by type.

b) For specific tasks, keep separate array of pointers to objects which perform these tasks. I.e. all the objects that must callHighPriorityTask

should be stored in a separate array, which should be called first.

h.e. How could you imagine that in general, without sorting, half of objects will be executed first, and then the rest? :) Well, if only to start a job queue.

But this is a creative question, you can come up with a lot of sophisticated ways to solve a penny task))) The main thing is not to encroach on the laws of physics, and everything will be fine)))

I don't want to, I believe the compiler will optimize the empty methods.

Aleksey Mavrin:

1. If you declare a pointer of a base class and create a child object, then when calling methods via this very pointer of the base class

a) virtual methods are called by the child class (via the virtual function table specially created for this purpose)

b) non-virtual methods are called by base class. Even if they are overridden in the child class.

yeah, i don't remember that i just fixed the json libraryhttps://www.mql5.com/en/code/11134(i found it on a githab somewhere a bit fresher than the KB).

and there were compiler warnings

deprecated behavior, hidden method calling will be disabled in a future MQL compiler version

fixed by specifying a method in a class

this.JSONValue:: getLong(getValue(index),out);

Thanks, it's a pretty clear picture

 
Igor Makanu:

definitely don't want to, I think the compiler will optimize empty methods

But calling an empty function is not likely to eat much anyway.

Igor Makanu:

OK, 2 loops means 2 loops, so there will be no miracle ((

Why should I shove into one loop something which is logically done in two loops? Because there is no speed, no clarity and simplicity gain and the size of hard drives is measured not in megabytes long ago.

 
Andrei Trukhanovich:

Why cram in one cycle what can be logically done in two? Because there is no gain in speed, clarity or simplicity, and the size of hard drives is not measured in megabytes.

I haven't even used them yet! ))))

just need to know the specifications to make something similar to normal method calls

Thank you. That explains it very well.

Andrei Trukhanovich:

Virtual? Not necessarily.

I wish I could show you an example, the question arose because of the great choice of polymorphism means, either virtual or without it, or inherit or close this method in derived classes

 
Roman:

You can try to get rid of loops altogether.
There are examples of how to print from 1 to 100 without loops and without recursion.
Maybe these examples will help, if it's even relevant ))

Why? Another pattern. The only thing that pattern adepts didn't recognize it, evidently it is not orthodox and canonical. Why not recursion? Also recursion, only not along, but across.