MQL5 Language Basics: CLASS TYPES
// DEFINATION OF A CLASS
// A class is more like an expanded concept of the data structure, but instead
// of holding data only only, it holds data and functions.
// A class can contain variables, & funtions ==> members of the class
// DEFINATION OF OBJECT
// An object is just an instance of a class.
// In other words, to create an object you need a class.
/* struct struct_name{
double structure_defination;
};
struct_name STRUCTURE_OBJECT;
*/
//1.1 DECLARING A CLASS
//the basic format of a class/ prototype
/*
class class_name{
access_keyword_1: // specifies the access right to the members of our class
// can be: ===> private, protected or public
member1;
access_keyword_2:
member2;
------
};
*/
/*
class CAR{
private: // data members
int doors;
int sits;
int tyres;
double weight;
public: // funtion members
bool start();
void changegear();
void stop();
bool horn();
};
// CREATE AN OBJECT
CAR Toyota;
CAR V8;
*/
// LET US CONSIDER IN DETAILS, THE FORMAT OF A CLASS IN MQL5
// prototype
/*
class class_name{
private:
members1;
members2;
members3;
public:
class_name() // constructor
~class_name() // destructor
members4();
members5();
protected:
members6();
members7();
};
*/
// THE CONSTRUCTOR
// Is a special function that is called automatically when a new OBJECT of the
// type of the class is created
// The name of the constructor MUST match the name of the class
// It does not take any parameters and does not have any return type
// A class in MQL5 can ONLY have just one constructor
// THE DESTRUCTOR
// It is called automatically when a class object is destroyed
// 1.2 INHERITANCE
// BASE CLASS
/*
class class_name{
private:
members1;
members2;
members3;
public:
class_name() // constructor
~class_name() // destructor
members4();
members5();
protected:
members6();
members7();
};
*/
// DERIVED CLASS
//class new_class : access_keyword /*public, protected, private*/base_class {
// private:
// members8;
// public:
// new_class();
// ~new_class();
// members9;
//};
/*
class CAR{
protected: // data members
int doors;
int sits;
double weight;
public: // funtion members
bool start();
virtual void changegear();
void stop();
bool horn();
private:
int tyres;
};
class SALOON : public CAR{
private:
int maxspeed;
public:
void runAtHighSpeed();
virtual void changegear() {gear1 = reverse; gear2 = low, gear3 = high;}
};
class WAGON : public CAR{
private:
bool hasLiftBack;
public:
virtual void changegear() {gear1 = low, gear2 = high, gear3 = reverse;}
};
*/
// WHY THE VIRTUAL FUNCTION
// Its simply because we want any class that inherits the function from the base
// class to be able to implement it on its own way.
// 1.3 DEFINING CLASS METHODS(MEMBER FUNCTIONS)
class CAR{
protected: // data members
int doors;
int sits;
double weight;
public: // funtion members
void CAR(); // constructor
bool start();
virtual void changegear();
void stop();
bool horn();
private:
int tyres;
};
// :: scope operator
bool CAR::start(){
// star car procedure
return false;
void CAR::CAR(){
// initialize member variables here
};
//+------------------------------------------------------------------+
//| OOP_Class.mq5 |
//| Copyright 2023, MetaQuotes Ltd. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2023, MetaQuotes Ltd."
#property link "https://www.mql5.com"
#property version "1.00"
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
//---
//---
return(INIT_SUCCEEDED);
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
//---
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
//---
//+------------------------------------------------------------------+