c++copy-constructorstatic-membersfractionsrational-number

How to write Rational Fractions using classes in c++


I have to implement Rational class in order to get Rational fractions. The header.h file is provided by my instructor so I have to follow up. I also have to write copy constructor in Rational::Rational(const Rational& cRational) function so the object can be copied. I have written my code but the addition of fractions in the output is wrong. Can anybody help me figure this one out? What is wrong with my coding specially in Rational::addition(const Rational &a) or how could i fix it ?

output :

 Begin Rational Class Tests

Testing default constructor: 1/1

Testing std constructor: 1/2

Testing copy constructor: 1/2

Testing addition: 4/4 + 1/2 = 4/4    // it should be 1/2 + 1/2 = 4/4

main function:

int main()
{
    cout << "Begin Rational Class Tests\n\n";

    cout<<"Testing default constructor: ";
    Rational n1;
    n1.printRational();
    cout << endl << endl;

    cout<<"Testing std constructor: ";
    Rational n2(1,2);
    n2.printRational();
    cout << endl << endl;

    cout<<"Testing copy constructor: ";
    Rational n3(n2);
    n3.printRational();
    cout << endl << endl;

    cout<<"Testing addition: ";
    n1 = n2.addition(n3);
    n2.printRational();
    cout <<" + ";
    n3.printRational();
    cout <<" = ";
    n1.printRational();
    cout << endl << endl;
}

Header File:

class Rational {
  public:
    Rational();  // default constructor
    Rational(int, int); //std (initialisation) constructor
    Rational(const Rational&); //copy constructor
    Rational addition(const Rational &);
    void printRational();
  private:
    int numerator;
    int denominator;
};

my program:

//default constructor
Rational::Rational() 
{
   numerator = 1;
   denominator = 1;
}
//initialize constructor
Rational::Rational(int n, int d)
{
     numerator = n;
     if (d==0) 
     {
        cout << "ERROR: ATTEMPTING TO DIVIDE BY ZERO" << endl;
        exit(0); // will terminate the program if division by 0 is attempted
     }
     else
        denominator = d;
}
//copy constructor
Rational::Rational(const Rational& cRational)
{
    numerator = cRational.numerator;
    denominator = cRational.denominator;
}
//addition
Rational Rational::addition(const Rational &a)
{
     numerator = numerator * a.denominator + a.numerator * denominator;
     denominator = denominator * a.denominator;
     return Rational(numerator,denominator);
}

void Rational::printRational()
{
    cout << numerator << "/" << denominator ;
}

Solution

  • Your addition function is modifying the member variables of *this, which causes strange things to happen.
    A more appropriate prototype would be

    Rational addition(const Rational &) const;
    

    as that would let the compiler tell you that you were doing something weird.

    You can either use local variables instead of assigning to the members, or you can do without intermediate variables completely:

    Rational Rational::addition(const Rational &a)
    {
         return Rational(numerator * a.denominator + a.numerator * denominator, 
                         denominator * a.denominator);
    }