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
CCircle is a CShape).public and protected members of the base class. private members of the base class are inaccessible to derived classes.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 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 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 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
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; }
};
CCircle:** Inherits from CShape and adds a m_radius member. class CCircle : public CShape
{
private:
int m_radius;
public:
CCircle() { m_type = 1; }
};
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