Polymorfism in MQL5 seems does not work.

To add comments, please log in or register
DeltaElectronics
77
DeltaElectronics  

Hello to everybody,


I tried to execute this simple example about polymorphism (https://www.mql5.com/en/docs/basis/oop/polymorphism), copying and pasting the code in my MetaEditor; but when I tried to execute it does not work because it alwasy call the method GetARea() of CShape base class instead of that of the derived ones...

I modified the code, in order the base area method alerts the string "pippo" and, as you can see from the screenshot attached, it is that it Always happens, instead of calling the GetArea methods of the corresponding derived classes.

Enclosed is the code I excuted

Documentation on MQL5: Language Basics / Object-Oriented Programming / Polymorphism
Documentation on MQL5: Language Basics / Object-Oriented Programming / Polymorphism
  • www.mql5.com
Polymorphism is an opportunity for different classes of objects, related through inheritance, to respond in various ways when calling the same function element. It helps to create a universal mechanism describing the behavior of not only the base class, but also descendant classes. Let's continue to develop a base class CShape, and define a...
Files:
poli.mq5 3 kb
screnshot.png 93 kb
Uffa.mqh 7 kb
nicholi shen
1898
nicholi shen  
DeltaElectronics:

Hello to everybody,


I tried to execute this simple example about polymorphism (https://www.mql5.com/en/docs/basis/oop/polymorphism), copying and pasting the code in my MetaEditor; but when I tried to execute it does not work because it alwasy call the method GetARea() of CShape base class instead of that of the derived ones...

I modified the code, in order the base area method alerts the string "pippo" and, as you can see from the screenshot attached, it is that it Always happens, instead of calling the GetArea methods of the corresponding derived classes.

Enclosed is the code I excuted

class CShape 
  { 
protected:  
   int            m_type;                    // tipo de figura 
   int            m_xpos;                    // X - coordenada del punto de enlace 
   int            m_ypos;                    // Y - coordenada del punto de enlace 
public: 
   void           CShape(){m_type=0;};       // constructor, el tipo es igual a cero 
   int            GetType(){return(m_type);};// devuelve el tipo de figura 
virtual 
   //double         GetArea(){Alert("pippo");return (0); }    // devuelve la superficie de figura 
   virtual double         GetArea(){Alert("pippo");return (0); }
  };
To add comments, please log in or register