I am overloading the operator + externally. It is obvious that if I put:
Fraccion operator+ (const Fraccion& r1, const Fraccion& r2)
{
Fraccion res;
res.numerador = r1.obtenerNum() + r2.obtenerNum();
res.denominador = r1.obtenerDen() + r2.obtenerDen();
return res;
}
res.numerador
and res.denominador
are not accessible as they are private members and could only be called through a public function that returns them, but if I use this:
Fraccion operator+ (const Fraccion& r1, const Fraccion& r2)
{
Fraccion res;
res.obtenerNum() = r1.obtenerNum() + r2.obtenerNum();
res.obtenerDen() = r1.obtenerNum() + r2.obtenerDen();
return res;
}
The compiler tells me that it is not assignable.
It is clear that if this same thing I do overloading as a member function yes that would be worth (the first overloaded function that I put you with members res.numerador
)
But then how should I put those variables so that they can access a value with that external overloaded function?
Thank you!
I attach the Fraction class (invented by me):
class Fraccion
{
public:
Fraccion(float n=0, float d=0) : numerador(n), denominador(d) {}
//Operador = OBLIGATORIA como función interna a la clase
Fraccion& operator=(const Fraccion& frac);
Fraccion& operator-(const Fraccion& frac2);
void mostrarFraccion() const;
void rellenarFraccion();
float obtenerNum() const { return numerador; }
float obtenerDen() const { return denominador; }
private:
float numerador;
float denominador;
};