Fraction Sum Program (Class Overload)

Asked

Viewed 185 times

-2

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!!

#include <iostream>
using namespace std;

class Fracao {
  private:
  double numerador;
  double denominador;

 public :
  inline Fracao(double = 0, double = 0);
  int getNum();
  int getDenom();
  double getValor();
  void setnumer(int newnum);
  void setdenom(int newden);
  friend Fracao operator + (Fracao &, Fracao&);
  inline void mostra();
};


int main () {

  setlocale(LC_ALL, "Portuguese");
  Fracao F3;
  Fracao F1 (4,3); // Numero 4/3
  Fracao F2 (8,10); // Numero 8/10
  F1.mostra(); F2.mostra(); 
  cout << "\n\nSoma de F1 + F2 = ";
  F3 = F1 + F2;
  cout << F3.getValor() << endl;
  return 0;
}


//Construtor
Fracao :: Fracao(double n, double d) {
  numerador = n;
  denominador = d;
}

//Modificadores
int Fracao::getNum(){
return numerador;
}

int Fracao::getDenom(){
  return denominador;
}

double Fracao::getValor(){
  return numerador / denominador;
}


void Fracao::setnumer(int novoNum){
numerador = novoNum;
}

void Fracao::setdenom(int newDen){
  denominador = newDen;
}

//Sobrecarga
Fracao operator + (Fracao &prim, Fracao &segun){
double novoDenom = prim.getDenom() * segun.getDenom();
double novoNum = prim.getNum() * segun.getDenom() + prim.getNum() * segun.getDenom();
Fracao *F3 = new Fracao(novoNum,novoDenom);
return *F3;
}


void Fracao :: mostra() {
  cout << "\nNumero Fracionário = " << this->numerador
       << " / " << this->denominador;
}

  • vc may use Std::ratio https://en.cppreference.com/w/cpp/numeric/ratio/ratio

2 answers

1

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; };

0

I decided to make the Frac class in terms of modern C++ ""

1 - No Setter and getter for "value types"

2 - Template for algebraic types

3 - UDL to create constant values (in place of constructor)

4 - Operator overload for "natural use"

https://godbolt.org/z/ezK55a

Att

Browser other questions tagged

You are not signed in. Login or sign up in order to post.