What I want is to be able to override the functions of the sum class and replace them with the functions of the loop class. This has to do with Polymorphism.
In this code I explain better the question (see the comment):
#include<iostream>
using namespace std;
class suma{
public:
void leer(int &a, int &b, int &c);
void calculo(int &a, int &b, int &c);
void escribir(int &a, int &b, int &c);
};
class loop: public suma{
public:
void leer(int &a, int &b, int &c);
void calculo(int &a, int &b, int &c);
void escribir(int &a, int &b, int &c);
};
main(){
int a, b, c;
suma s;
loop l;
// Anulación <----
cout << "Se supone que con esto anulo." << endl;
s = l; // <---- Se supone que así anulo las funciones del objeto s
// y le asigno las funciones del objeto l
s.leer(a, b, c);
s.calculo(a, b, c);
s.escribir(a, b, c);
}
void suma::leer(int &a, int &b, int &c){
cout << "SUMA" << endl;
cout << "Ingresa numero a: ";
cin >> a;
cout << "Ingresa numero b: ";
cin >> b;
}
void suma::calculo(int &a, int &b, int &c){
c = a + b;
}
void suma::escribir(int &a, int &b, int &c){
cout << a << " + " << b << " = " << c << endl;
}
void loop::leer(int &a, int &b, int &c){
cout << "LOOP" << endl;
cout << "Ingresa numero a: ";
cin >> a;
cout << "Ingresa numero b: ";
cin >> b;
}
void loop::calculo(int &a, int &b, int &c){
c = a * b;
}
void loop::escribir(int &a, int &b, int &c){
cout << a << " * " << b << " = " << c << endl;
}
Apart from what @rnd commented, there is another C++ feature to take into account: hiding.
If you have a base class in which you declare several methods that may or may not be virtual:
And later, by inheriting from it you override some of those methods:
What effect does this have on the program? We see it with an example:
The declaration of
func
in the derived class will, by default, hide all overloads that may exist in its base class. There are mechanisms to import the functions that we need from the base class but by default they will be hidden.Now, what happens if, like in the example, we haven't declared the base function as
virtual
?As the function is not labeled as
virtual
, the compiler does not substitute one function for another, but depending on the context (in this case the type of pointer we are using) one function or another will be called.However, if we declare the function as virtual, things change:
Declaring the function of the parent class as
virtual
allows the compiler to drop the base function and replace it with the version provided by the derived class.As an additional note, the modifier
override
(available as of the C++11 standard) forces the compiler to check that the function in question is going to override a function of the parent class (which must be virtual). If these conditions are not met, the compiler will display an error. That the compiler checks these things by itself is something that avoids many errors when programming:All the best.
No doubt eferion 's and rnd 's answers are valid; but both use dynamic memory for something that would not be necessary; also does not comply with the syntax that you express in your question:
There is a way to get the syntax that you express in your question without the need to use inheritance (thus we write less) or pointers (thus we avoid using
->
, which is more comfortable) or dynamic memory (thus the program is slightly faster) 1 , it would be using pointers to functions; We will start by defining the types of the functions to use:And then we define an object that uses these functions:
Having the appropriate functions, we can configure our
operador
as we please:We see it [in an example] :
Since the signature of all operator functions is the same, a single type could have been used:
Overriding or overriding could also be used like so:
1 In general, any programming tool (such as inheritance and/or dynamic memory) should be used only when necessary, otherwise it could be considered overengineering . We can discuss whether inheritance and method overriding was the right tool in this case or not.
1- To be able to override a method, you must declare it as
virtual
in the parent class:2- Declaring objects as objects, virtual methods do not work. You will need to use a pointer or a reference. Example:
Don't forget to make
delete
the objects created withnew
.In assembler, of intel or of the processor that is, there is always an operation called "CALL".
That CALL is accompanied by a parameter, which can be a constant (a constant memory address written in "code stream" just after CALL), or "something variable" like. eg the value of a processor register.
In your example, since there are no virtual methods (and you do not want to work with virtual methods, as you say), the "s.read" line generates in assembler (and independent of the previous lines), a:
CALL "memory_cte_addr"
being the address you assign to the "sum.read" function.
However when virtual methods are used, something similar to this is generated:
CALL [EBX]
"go to the memory dir contained in the ebx register", which is a bit more inefficient, but very little else. But it allows dynamic "calls".
SUMMARY, without "virtual", it is impossible to make CALL beyond sum.read, since "read" is not "dynamic" (ie virtual).