1

I have a problem with the code below. It is not performing any function now... neither add nor subtract nor divison nor multiplication.

Any help will be appreciated... I need explanation... so that I can understand it and get over with it :D

#include <iostream>
using namespace std;

// Class Definitions

class RationalNumber
{
public:
     RationalNumber(int, int, int, int);                    
     RationalNumber operator+(RationalNumber);
     RationalNumber operator-(RationalNumber);
     RationalNumber operator*(RationalNumber);
     RationalNumber operator/(RationalNumber);
     RationalNumber operator<(RationalNumber);
     RationalNumber operator>(RationalNumber);
     RationalNumber operator<=(RationalNumber);
     RationalNumber operator>=(RationalNumber);
     RationalNumber operator==(RationalNumber);
     RationalNumber operator!=(RationalNumber);
private:
     int numerator;
     int denominator;
     int numerator2;
     int denominator2;

};  // end RationalNumber class


// RationalNumber class member-function definitions
RationalNumber::RationalNumber(int num, int denom, int num2, int denom2)
{
     numerator = num;
     denominator = denom;
     numerator2 = num2;
     denominator2 = denom2;

//for first fraction
     if (denominator == 0 || denominator < 0)
          cout << "ERROR:Denominator can not be zero or less than zero." << "\n";

     else
          //Reduces the fraction to lowest terms.
     {
          int i = numerator > denominator ? numerator : denominator;

          while(i > 1)
          {
               if(numerator % i == 0 && denominator % i == 0)  
               {
               numerator /= i;
               denominator /= i;
               }
               --i;
          }

     }

     cout << "Simplified fraction one is: " << numerator << " / "
        << denominator << "\n";

//For second fraction
     if (denominator2 == 0 || denominator2 < 0)
          cout << "ERROR:Denominator can not be zero or less than zero" << "\n";
     else
          //Reduces the fraction to lowest terms.
     {
          int j = numerator2 > denominator2 ? numerator2 : denominator2;

          while(j > 1)
          {
               if(numerator2 % j == 0 && denominator2 % j == 0)  
               {
               numerator2 /= j;
               denominator2 /= j;
               }
               --j;
          }

     }
     cout << "Simplified fraction two is: " << numerator2 << " / "
        << denominator2 << "\n";

}

// addition operator
RationalNumber RationalNumber::operator+(RationalNumber a)
{
      RationalNumber temp=RationalNumber(1,2,3,4);
      temp.numerator = numerator + a.numerator;
      temp.denominator = denominator + a.denominator;
      temp.numerator2 = numerator2 + a.numerator2;
      temp.denominator2 = denominator2 + a.denominator2;
      return temp;
}

// subtraction operator
RationalNumber RationalNumber::operator-(RationalNumber a)
{
     RationalNumber temp=RationalNumber(1,2,3,4);
      temp.numerator = numerator - a.numerator;
      temp.denominator = denominator - a.denominator;
      temp.numerator2 = numerator2 - a.numerator2;
      temp.denominator2 = denominator2 - a.denominator2;
      return temp;
}

// multiplication operator
RationalNumber RationalNumber::operator*(RationalNumber a)
{
      RationalNumber temp=RationalNumber(1,2,3,4);
      temp.numerator = numerator * a.numerator;
      temp.denominator = denominator * a.denominator;
      temp.numerator2 = numerator2 * a.numerator2;
      temp.denominator2 = denominator2 * a.denominator2;
      return temp;
}

// division operator
RationalNumber RationalNumber::operator/(RationalNumber a)
{
      RationalNumber temp=RationalNumber(1,2,3,4);
      temp.numerator = numerator / a.numerator;
      temp.denominator = denominator / a.denominator;
      temp.numerator2 = numerator2 / a.numerator2;
      temp.denominator2 = denominator2 / a.denominator2;
      return temp;
}

int main()
{

     int top;
     int bot;
     int top2;
     int bot2;

     cout << "Please enter the Numerator for fraction one: \n";
     cin >> top;
     cout << "Please enter the Denominator for fraction one: \n";
     cin >> bot;
     cout << "Please enter the Numerator for fraction two: \n";
     cin >> top2;
     cout << "Please enter the Denominator for fraction two: \n";
     cin >> bot2;

     RationalNumber A(top, bot, top2, bot2);
     return 0;

}
einpoklum
  • 118,144
  • 57
  • 340
  • 684
tiger
  • 19
  • 1
  • 1
  • 6
  • 3
    Aside from the fact that `class()` makes no sense, I fail to see why your class tracks two rational numbers, there are much better ways of finding GCDs, and your addition and subtraction do not make sense arithmetically. – ephemient Nov 27 '10 at 04:00
  • Note that your division operator does some probably unwanted rounding on the numerators/denominators. Probably it should be implemented in a different way. – sth Nov 27 '10 at 04:07
  • can u fix it... i tried my best – tiger Nov 27 '10 at 04:28
  • As a side question: Why does `RationalNumber` need two numerator/denominator pairs? – Marcelo Cantos Nov 27 '10 at 21:59
  • because i thought of taking it as two differents rational numbers... – tiger Nov 27 '10 at 23:10
  • and it displays two different rational numbers now i want to perform the add function and other functions – tiger Nov 27 '10 at 23:11

5 Answers5

4

By declaring RationalNumber(int, int, int, int), you automatically remove the default constructor, i.e. it is no longer possible to create an instance of your class by invoking a constructor without parameters.

In other words,

RationalNumber number;

is no longer possible. You'd need to say

RationalNumber number(1,2,3,4);.

If you want to be able to create an instance using a default constructor, then you need to define one in addition to the other constructor:

RationalNumber()
{
    // Initialization
}
EboMike
  • 76,846
  • 14
  • 164
  • 167
1

Redefine your operators thus:

RationalNumber RationalNumber::operator+(RationalNumber a)
{
      return RationalNumber(
          numerator + a.numerator,
          denominator + a.denominator,
          numerator2 + a.numerator2,
          denominator2 + a.denominator2);
}

This avoids creating the parameterless temporary that is causing you grief. It also helps the compiler make use of the return-value optimisation (RVO).

Marcelo Cantos
  • 181,030
  • 38
  • 327
  • 365
  • Is `a/b + c/d` equal to `(a+c)/(b+d)`? – Potatoswatter Nov 27 '10 at 05:36
  • Perhaps I should have looked at the OP's overall logic and corrected it, but I didn't. I'm not even sure why a `RationalNumer` needs four components, so going beyond the stated problem to fix the overall logic would open a can of worms. – Marcelo Cantos Nov 27 '10 at 21:57
1

I'll take a quick shot at it…

#include <utility>

struct ratio {
    int numerator;
    int denominator;

    explicit ratio( int n = 0, int d = 1 )
        : numerator( n ), denominator( d ) { }

    ratio operator - () const {
        ratio x( * this );
        x.numerator = - x.numerator;
        return x;
    }

    ratio operator + () const
        { return *this; }
};

void reduce( ratio &x ) {
    int larger = x.numerator, smaller = x.denominator;
    if ( larger < smaller ) std::swap( larger, smaller );
    while ( int rem = larger % smaller ) {
        larger = smaller;
        smaller = rem;
    }
    x.numerator /= smaller;
    x.denominator /= smaller;
}


ratio &operator += ( ratio &l, ratio const &r ) {
    l.numerator = l.numerator * r.denominator
                + r.numerator * l.denominator;
    l.denominator *= r.denominator;
    reduce( l );
    return l;
}

ratio &operator -= ( ratio &l, ratio const &r )
    { return l += - r; }

ratio &operator *= ( ratio &l, ratio const &r ) {
    l.numerator *= r.numerator;
    l.denominator *= r.denominator;
    reduce( l );
    return l;
}

ratio &operator /= ( ratio &l, ratio const &r ) {
    l.numerator *= r.denominator;
    l.denominator *= r.numerator;
    reduce( l );
    return l;
}

Canonical definitions for operators +, -, *, /.

Potatoswatter
  • 134,909
  • 25
  • 265
  • 421
  • so you want me to change the functions for addition, sub, multiply and divide – tiger Nov 27 '10 at 06:20
  • i changed the functions but the problem i m having the addition part is not printing out u get my point... – tiger Nov 27 '10 at 06:24
  • neither the subtract nor the divide nor the multiply – tiger Nov 27 '10 at 06:24
  • @tiger: This is as close to writing the whole thing as I'll do. – Potatoswatter Nov 27 '10 at 06:35
  • man.. i tried fixing it and it didnt fix... u have my code.. its not that i didnt putted my effort in it i used ur struct function and everything u told me.. but it just didnt work out.. – tiger Nov 27 '10 at 06:37
  • @tiger: Can't help you without understanding more. A rational number has one numerator and one denominator, yet your class has two of each. The operations you implement are incorrect. Why? And what difficulty do you have using mine? – Potatoswatter Nov 27 '10 at 07:42
  • ok.. u know i used ur operators but the problem is functions are not printing out... how will i use these operators and make them print on the screeen.... – tiger Nov 27 '10 at 15:43
0

Your class does not have a constructor that takes zero arguments. Therefore, the following code:

class object=class();

Is attempting to use a constructor that doesn't exist. You must either write a constructor that takes zero arguments (explicitly or by providing all the default values for another constructor's arguments) or you must provide values upon construction:

class object(1, 2, 3, 4);
class object2 = class(1, 2, 3, 4);
Jonathan Grynspan
  • 43,286
  • 8
  • 74
  • 104
  • now its just printing out the fraction part its not performing any addition division nothing help – tiger Nov 27 '10 at 04:05
  • @tiger: Piece of advice: place cout statements at the beginning of every function you want to debug, like cout<<"reached here"< – Nav Nov 27 '10 at 05:28
0

now the problem is ... its not performing any addition nor any other function... the program is running perfect but somehow i get the additon and other functions to work

thanks

#include <iostream>
using namespace std;

// Class Definitions

class RationalNumber
{
public:
     RationalNumber(int, int, int, int);                    
     RationalNumber operator+(RationalNumber);
     RationalNumber operator-(RationalNumber);
     RationalNumber operator*(RationalNumber);
     RationalNumber operator/(RationalNumber);
     RationalNumber operator<(RationalNumber);
     RationalNumber operator>(RationalNumber);
     RationalNumber operator<=(RationalNumber);
     RationalNumber operator>=(RationalNumber);
     RationalNumber operator==(RationalNumber);
     RationalNumber operator!=(RationalNumber);
private:
     int numerator;
     int denominator;
     int numerator2;
     int denominator2;

};  // end RationalNumber class


// RationalNumber class member-function definitions
RationalNumber::RationalNumber(int num, int denom, int num2, int denom2)
{
     numerator = num;
     denominator = denom;
     numerator2 = num2;
     denominator2 = denom2;

//for first fraction
     if (denominator == 0 || denominator < 0)
          cout << "ERROR:Denominator can not be zero or less than zero." << "\n";

     else
          //Reduces the fraction to lowest terms.
     {
          int i = numerator > denominator ? numerator : denominator;

          while(i > 1)
          {
               if(numerator % i == 0 && denominator % i == 0)  
               {
               numerator /= i;
               denominator /= i;
               }
               --i;
          }

     }

     cout << "Simplified fraction one is: " << numerator << " / "
        << denominator << "\n";

//For second fraction
     if (denominator2 == 0 || denominator2 < 0)
          cout << "ERROR:Denominator can not be zero or less than zero" << "\n";
     else
          //Reduces the fraction to lowest terms.
     {
          int j = numerator2 > denominator2 ? numerator2 : denominator2;

          while(j > 1)
          {
               if(numerator2 % j == 0 && denominator2 % j == 0)  
               {
               numerator2 /= j;
               denominator2 /= j;
               }
               --j;
          }

     }
     cout << "Simplified fraction two is: " << numerator2 << " / "
        << denominator2 << "\n";

}

// addition operator
RationalNumber RationalNumber::operator+(RationalNumber a)
{
      RationalNumber temp=RationalNumber(1,2,3,4);
      temp.numerator = numerator + a.numerator;
      temp.denominator = denominator + a.denominator;
      temp.numerator2 = numerator2 + a.numerator2;
      temp.denominator2 = denominator2 + a.denominator2;
      return temp;
}

// subtraction operator
RationalNumber RationalNumber::operator-(RationalNumber a)
{
      RationalNumber temp=RationalNumber(1,2,3,4);
      temp.numerator = numerator - a.numerator;
      temp.denominator = denominator - a.denominator;
      temp.numerator2 = numerator2 - a.numerator2;
      temp.denominator2 = denominator2 - a.denominator2;
      return temp;
}

// multiplication operator
RationalNumber RationalNumber::operator*(RationalNumber a)
{
      RationalNumber temp=RationalNumber(1,2,3,4);
      temp.numerator = numerator * a.numerator;
      temp.denominator = denominator * a.denominator;
      temp.numerator2 = numerator2 * a.numerator2;
      temp.denominator2 = denominator2 * a.denominator2;
      return temp;
}

// division operator
RationalNumber RationalNumber::operator/(RationalNumber a)
{
      RationalNumber temp=RationalNumber(1,2,3,4);
      temp.numerator = numerator / a.numerator;
      temp.denominator = denominator / a.denominator;
      temp.numerator2 = numerator2 / a.numerator2;
      temp.denominator2 = denominator2 / a.denominator2;
      return temp;
}

int main()
{

     int top;
     int bot;
     int top2;
     int bot2;

     cout << "Please enter the Numerator for fraction one: \n";
     cin >> top;
     cout << "Please enter the Denominator for fraction one: \n";
     cin >> bot;
     cout << "Please enter the Numerator for fraction two: \n";
     cin >> top2;
     cout << "Please enter the Denominator for fraction two: \n";
     cin >> bot2;

     RationalNumber A(top, bot, top2, bot2);
     return 0;

}
Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
tiger
  • 19
  • 1
  • 1
  • 6