Good evening, I have a question in my program of sum of two fractions using overload of classes, in which the program it is always printing the same value, even modifying the parameters of the two functions, thank you
- It’s not about class overload, there’s only one class in the program
- I imagine it referred to the reset --- Overload/overload --- of the operator
+
to sum instances of Fracao
.
- C and C++ are different animals. You should not use both tags
About your program
- The code is very confusing: should solve if it will use
int
or double
because mixing the two can destroy your result by unexpected conversions and truncation.
- Use more expressive and coherent names: Used
getNum()
but setnumber()
for example. Set something and use only that. Or use the upper case in the middle or hyphen and abbreviate numerador
and denominador
always the same way
- The sum returns a new variable, but do not use
new
- if you use an Overload to
+
and has a function mostra()
great chance that it would be simpler to also use an Overload to <<
right?
- getters in general are marked
const
.
- if you use getters you don’t need
friend
to add up. If you use Friend maybe you can do without getters
An example with your code
Note the simplest way to declare the constructor and the fact that the sum of Fracao
is not part of the class.
class Fracao
{
private:
double numerador;
double denominador;
public :
inline Fracao(double n = 0, double d = 0) :
numerador(n), denominador(d){};
double getNumer() const;
double getDenom() const;
double getValor() const;
void setNumer(double newNumer);
void setDenom(double newDenom);
public:
friend ostream& operator<<( ostream& o, const Fracao&);
}; // class Fracao()
exit from your program
F1 = [ 4 / 3 ]
F2 = [ 8 / 10 ]
F3 = [ 0 / 0 ]
Soma de F1 + F2 = [ 64 / 30 ]
F3 = F1 + F2 = [ 64 / 30 ]
Valor de F3: 2.13333
Your program changed to use this class:
int main(void)
{
setlocale(LC_ALL, "Portuguese");
Fracao F3;
Fracao F1 (4,3); // Numero 4/3
Fracao F2 (8,10); // Numero 8/10
cout << "F1 = " << F1;
cout << "F2 = " << F2;
cout << "F3 = " << F3;
cout << "\n\nSoma de F1 + F2 = " << (F1+F2);
F3 = F1 + F2;
cout << "F3 = F1 + F2 = " << F3 << "\n";
cout << "Valor de F3: " << F3.getValor() << endl;
return 0;
}
See that it is easier to read with Overload <<
The whole program
#include <iostream>
using namespace std;
class Fracao
{
private:
double numerador;
double denominador;
public :
inline Fracao(double n = 0, double d = 0) :
numerador(n), denominador(d){};
double getNumer() const;
double getDenom() const;
double getValor() const;
void setNumer(double newNumer);
void setDenom(double newDenom);
public:
friend ostream& operator<<( ostream& o, const Fracao&);
}; // class Fracao()
Fracao operator+(const Fracao& um, const Fracao& outro);
int main(void)
{
setlocale(LC_ALL, "Portuguese");
Fracao F3;
Fracao F1 (4,3); // Numero 4/3
Fracao F2 (8,10); // Numero 8/10
cout << "F1 = " << F1;
cout << "F2 = " << F2;
cout << "F3 = " << F3;
cout << "\n\nSoma de F1 + F2 = " << (F1+F2);
F3 = F1 + F2;
cout << "F3 = F1 + F2 = " << F3 << "\n";
cout << "Valor de F3: " << F3.getValor() << endl;
return 0;
}
Fracao operator+( const Fracao& um, const Fracao& outro)
{
double Num = um.getNumer() * outro.getDenom();
Num += outro.getNumer() * um.getDenom();
double Den = um.getDenom() * outro.getDenom();
return Fracao(Num,Den);
};
ostream& operator<< ( ostream& o, const Fracao& f)
{
o << "[ " <<
f.numerador << " / " <<
f.denominador << " ]\n";
return o;
};
//Modificadores
double Fracao::getNumer() const
{ return numerador; };
double Fracao::getDenom() const
{ return denominador; };
double Fracao::getValor() const
{ return double (numerador / denominador); };
void Fracao::setNumer(double novoNum)
{ numerador = novoNum; };
void Fracao::setDenom(double newDenom)
{ denominador = newDenom; };
vc may use Std::ratio https://en.cppreference.com/w/cpp/numeric/ratio/ratio
– Cleiton Santoia Silva