Class, or Interface

To add comments, please log in or register
GrumpyDuckMan
882
GrumpyDuckMan  

Hello Everyone,

From the best of my knowledge, both  of these are used within mql4.

Question: Why would you choose one over the other and why?

Anthony Garot
3259
Anthony Garot  
GrumpyDuckMan:

Hello Everyone,

From the best of my knowledge, both  of these are used within mql4.

Question: Why would you choose one over the other and why?

This tutorial for C++ gives a good example for where you use both:

https://www.tutorialspoint.com/cplusplus/cpp_interfaces.htm

Obviously, the syntax is slightly different from MQL4.

The gist of the example is that all shapes have an area, but the area is calculated differently. So:

1. You create an interface and specify a virtual function getArea().

2. You derive all shape classes from this interface.

3. You override the getArea() function in each class.

The interface basically guarantees that you (as a programmer) don't forget to implement the getArea() function in each derived class.

That guarantee, i.e. the requirement to implement the getArea() function in each and every derived shape class, is why you use an interface. It keeps the naming consistent and forces you to implement.

You could easily extend this interface by adding getPerimeter(), since all shapes have a perimeter.

Interfaces in C++ (Abstract Classes)
  • tutorialspoint.com
  • www.tutorialspoint.com
Interfaces in C++ (Abstract Classes) - Learn C++ in simple and easy steps starting from basic to advanced concepts with examples including C++ Overview, Environment Setup, Basic Syntax, Comments, Data Types, Variable Types, Scope, Constants/Literals, Modifier Types, Storage Classes, Operators, Loop Types, Decision Making, Functions, Numbers, Arrays, Strings, Pointers, References, Date and Time, Basic Input/Output, Data Structures, Classes and Objects, Object Oriented Language, Methods, Overriding, Inheritance, Polymorphism, Abstraction, Encapsulation, Interfaces, Files and Streams, STL, Iterators, Algorithms, Exception Handling, Dynamic Memory, Overloading, Templates, Namespaces and Signal Handling, Preprocessor, Multithreading, Web Programming.
GrumpyDuckMan
882
GrumpyDuckMan  

Hello again,

Thank you for your response. I have just started an online programming course. The course has shown me so far that a lot of things that I have be doing in any programming language are at times total nonsense. I have found that some of the codes I wrote years ago aren't "DRY". Some of them I have thought about re-writing or completely deleting them and starting over again. 

Amir Yacoby
1325
Amir Yacoby  
Not directly related, but interface in MQL5 is a limited version of what interface is supposed to be, which is a contract for specific behaviour implementation. The limit on MQL5 comes from the fact that a class can not extend another class and one or more interfaces at the same time, which makes it pretty useless in MQL5.

GrumpyDuckMan
882
GrumpyDuckMan  
Amir Yacoby:
Not directly related, but interface in MQL5 is a limited version of what interface is supposed to be, which is a contract for specific behaviour implementation. The limit on MQL5 comes from the fact that a class can not extend another class and one or more interfaces at the same time, which makes it pretty useless in MQL5.

Welcome to the discussion,

Maybe save a return value/s. Give the return back to the class/s. Then save return valves to file and retrieve them in order to assigned class.

Amir Yacoby
1325
Amir Yacoby  
GrumpyDuckMan:

Welcome to the discussion,

Maybe save a return value/s. Give the return back to the class/s. Then save return valves to file and retrieve them in order to assigned class.

What I mean is if you for instance have a behaviour which we might call crossing of price.
Something like
Interface ICrossable
{
  bool priceCrossed();
};
Now you have unrelated object classes, which want to implement this behaviour, but they already extending CObject, like
CObjChannel, CObjTrendLine etc..
So, we want those unrelated objects to have this behaviour to tell us that price crossed them, but they alrwady extending CObject so it is not possible to also extend ICrossable
GrumpyDuckMan
882
GrumpyDuckMan  

Hello friend,

Where did you think bool can equal a Trendline?

Secondly price can't really be true, or false.. can they!

Might be channel can be +- value & TrendLine can be another value....

Price can be up and down.

To add comments, please log in or register