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