I am having some issues with my project. This is homework and I am not looking for answers, just some help. I am to create a RationalNumber class that takes two rational numbers (fractions), reduces them, and I am then supposed to use overloaded operators to add, subtract, multiply, and divide them (along with relational and equality operators as well but i need to handle the arithmetic first). I thought that i had my head wrapped around the notion of operator overloading, but I am still having issues. The program compiles but it certainly does not display the correct values. Any advice would be greatly appreciated! I am very new to C++ so don't be too hard on me. :-)
Header File:
#ifndef RATIONALNUMBER_H
#define RATIONALNUMBER_H
#include <iostream>
using namespace std;
class RationalNumber {
public:
RationalNumber(int, int); //constructor to initialize setFraction functions
RationalNumber(); //default constructor
void setFraction(int, int); //set fraction ones values and reduce it
void setFractionTwo(int, int); //set fraction ones values and reduce it
void print(); //function to print the rational numbers
RationalNumber operator+(RationalNumber &); //overload the + operator
RationalNumber operator-(RationalNumber &) ; //overload the - operator
RationalNumber operator*(RationalNumber&) ; //overload the * operator
RationalNumber operator/(RationalNumber&) ; //overload the / operator
RationalNumber operator>(RationalNumber &);
RationalNumber operator<(RationalNumber &);
RationalNumber operator>=(RationalNumber &);
RationalNumber operator<=(RationalNumber &);
RationalNumber operator==(RationalNumber &);
RationalNumber operator!=(RationalNumber &);
private:
int numerator; //variable for first numerator
int denominator; //variable for first denominator
int numerator2; //variable for second numerator
int denominator2; //variable for second denominator
};
#endif
Source Code
#include <iostream>
#include "RationalNumber.h"
using namespace std;
RationalNumber::RationalNumber(int n, int d) { //constructor initializes set fraction
setFraction(n, d); //pass values top setFraction
setFractionTwo(n, d);
}
RationalNumber::RationalNumber() {}
void RationalNumber::setFraction(int n, int d) { //function to reduce the first fraction
int a = n;
int b = d;
int r;
while (d != 0)
{
r = n % d;
n = d;
d = r;
}
numerator = a/n;
denominator = b/n;
}
void RationalNumber::setFractionTwo(int n, int d) { //function to reduce the second fraction
int a = n;
int b = d;
int r;
while (d != 0)
{
r = n % d;
n = d;
d = r;
}
numerator2 = a/n;
denominator2 = b/n;
}
RationalNumber RationalNumber::operator+(RationalNumber &c) {
RationalNumber result;
result.numerator = ((numerator * denominator2) + (denominator * numerator2));
result.denominator = (denominator * denominator2);
return result;
}
RationalNumber RationalNumber::operator-( RationalNumber &c) {
RationalNumber result;
result.numerator = ((numerator * denominator2) - (denominator * numerator2));
result.denominator = (denominator * denominator2);
return result;
}
RationalNumber RationalNumber::operator*( RationalNumber &c) {
RationalNumber result;
result.numerator = ((numerator * numerator2) - (denominator * denominator2));
result.denominator = (denominator * denominator2);
return result;
}
RationalNumber RationalNumber::operator/( RationalNumber &c) {
RationalNumber result;
result.numerator = (numerator * denominator2);
result.denominator = (denominator * denominator2);
return result;
}
void RationalNumber::print() {
cout << '(' << numerator << "/" << denominator << ')';
}
Driver
#include <iostream>
#include "RationalNumber.h"
using namespace std;
int main() {
int x, y;
RationalNumber number;
RationalNumber number2;
RationalNumber a;
cout << "Enter your first Numerator and Denominator: ";
cin >> x >> y;
number.setFraction(x, y);
cout << "Enter your second Numerator and Denominator: ";
cin >> x >> y;
number2.setFraction(x, y);
cout << "\n\n";
cout << "First fraction reduced is: ";
number.print();
cout << "\n\n";
cout << "Second fraction reduced is: ";
number2.print();
cout << "\n\n";
a = number + number2;
cout << "The two fractions added together: ";
a.print();
cout << "\n\n";
cout << "The two fractions subtracted: ";
a = number - number2;
a.print();
cout << "\n\n";
cout << "The two fractions multiplied: ";
a = number * number2;
a.print();
cout << "\n\n";
cout << "The two fractions divided: ";
a = number / number2;
a.print();
cout << "\n\n";
system("pause");
return 0;
};
I just don't think that I am on the right track.