Inheritance in MQL4

Overview

Inheritance is a core OOP feature in MQL4 that promotes code reuse by allowing a new class (derived class) to be based on an existing class (base class). The derived class inherits members from the base class and can modify or supplement them. This avoids redundant code development and re-testing.

Key Concepts

  • **Code Reuse:** Reduces development time and potential errors by leveraging existing class structures.
  • **Hierarchy:** Establishes a classification system where derived classes are specific types of base classes (e.g., a CCircle is a CShape).
  • **Member Inheritance:** Derived classes inherit public and protected members of the base class. private members of the base class are inaccessible to derived classes.
  • **Constructor/Destructor Behavior:** Base class constructors are called before derived class constructors. Derived class destructors are called before base class destructors.
  • **Single Inheritance:** MQL4 does not support multiple inheritance.
  • Syntax

    The syntax for defining a derived class is:

    class DerivedClassName : access BaseClassName
      {
        // Derived class members declaration
      };
    

  • access: Can be public, protected, or private.
  • Access Specifiers and Inheritance Types

    The access specifier used in the derived class declaration determines how base class members are inherited:

  • **Public Inheritance (public BaseClassName):**
  • * public members of the base class remain public in the derived class. * protected members of the base class remain protected in the derived class. * The relationship "object of derived class is object of base class" holds true.
  • **Protected Inheritance (protected BaseClassName):**
  • * public members of the base class become protected in the derived class. * protected members of the base class remain protected in the derived class. * The relationship "object of derived class is object of base class" does not hold true externally.
  • **Private Inheritance (private BaseClassName):**
  • * public members of the base class become private in the derived class. * protected members of the base class become private in the derived class. * The relationship "object of derived class is object of base class" does not hold true externally.

    **Important Note:** Regardless of the inheritance type, only public and protected members of the base class are accessible *from within* the derived class itself. private members are never accessible.

    Example: CShape Hierarchy

  • **Base Class CShape:** Contains common properties like shape type (m_type) and base point coordinates (m_xpos, m_ypos).
  •     class CShape
          {
          protected:
            int m_type;
            int m_xpos;
            int m_ypos;
          public:
            CShape() { m_type = 0; m_xpos = 0; m_ypos = 0; }
            void SetXPos(int x) { m_xpos = x; }
            void SetYPos(int y) { m_ypos = y; }
          };
        
  • **Derived Class CCircle:** Inherits from CShape and adds a m_radius member.
  •     class CCircle : public CShape
          {
          private:
            int m_radius;
          public:
            CCircle() { m_type = 1; }
          };
        
  • **Derived Class CSquare:** Inherits from CShape and adds a m_square_side member.
  •     class CSquare : public CShape
          {
          private:
            int m_square_side;
          public:
            CSquare() { m_type = 2; }
          };
        

    Access Example (CBaseClass and CDerived)

    This example demonstrates access restrictions:

    class CBaseClass
      {
      private:
        int m_member;
      protected:
        int Member() { return(m_member); }
      public:
        CBaseClass() { m_member = 5; }
      private:
        void Member(int value) { m_member = value; };
      };

    class CDerived : public CBaseClass { public: void Func() { // m_member = 0; // Error: private member not available // Member(0); // Error: private method not available // Print(m_member); // Error: private member not available Print(Member()); // OK: protected method is available } };

    Protected Inheritance Example (CBaseMathClass and CProtectedChildClass)

    Illustrates how protected inheritance restricts external access to base class members.

    class CBaseMathClass
      {
      private:
        double m_Pi;
      public:
        void SetPI(double v) { m_Pi = v; }
        double GetPI() { return m_Pi; }
        CBaseMathClass() { SetPI(3.14); }
      };

    class CProtectedChildClass : protected CBaseMathClass { private: double m_radius; public: void SetRadius(double r) { m_radius = r; } double GetCircleLength() { return GetPI() * m_radius; } // GetPI() is accessible here };

    // In OnStart(): // CProtectedChildClass pt; // pt.SetRadius(10); // PrintFormat("Length=%G", pt.GetCircleLength()); // OK // pt.SetPI(3); // Error: SetPI() is protected in CProtectedChildClass

    See Also

  • [Structures and Classes](/basis/types/classes)
  • [Encapsulation and Extensibility of Types](/basis/oop/incapsulation)
  • [Polymorphism](/basis/oop/polymorphism)