Having a little trouble with this problem. Keep getting a compiler error which says
there is no match for my operators when I run the test program
Create a class called Rational for performing arithmetic with fractions.
Write a driver program to test your class.
Use integer variables to represent the private data of the class - the
numerator and the denominator. Provide a constructor function that enables
an object of this class to be initialized when it is declared. The
constructor should contain default values in case no initializers are
provided and should store the fraction in reduced form (i.e., the fraction
2
4
would be stored in the object as 1 in the numerator and 2 in the
denominator). Provide public member functions for each of the following:
a. Addition of two Rational numbers. The result should be stored in
reduced form.
b. Subtraction of two Rational numbers. The result should be stored in
reduced form.
c. Multiplication of two Rational numbers. The result should be stored in
reduced form.
d. Division of two Rational numbers. The result should be stored in
reduced form.
e. Printing Rational numbers in the form a/b where a is the numerator and
b is the denominator.
f. Printing Rational numbers in the floating point format.
So far, here is my code:
Header
#ifndef Rational_H
#define Rational_H
#include <iostream>
using namespace std;
class Rational
{
public:
Rational(int = 0,int =0);//default constructor
//set functions
void addition(Rational r);
void subtraction(Rational r);
void multiplication(Rational r);
void division(Rational r);
void printRational();
void printRationalAsDouble();
void Reduction();
private:
int numerator;
int denominator;
};//end class Rational
#endif
Source Files
#include <iostream>
#include <iomanip>
#include "Rational.h"//include definition of class Rational from Rational.h
using namespace std;
//Rational constructor initializes each data member to zero
Rational::Rational(int num, int den)
{
numerator = num;
denominator = den;
}// end Rational constructor
void Rational::addition(Rational r)
{
numerator = numerator + r.numerator;
denominator = denominator + r.denominator;
Reduction();
}
void Rational::subtraction(Rational r)
{
numerator = numerator - r.numerator;
denominator = denominator - r.denominator;
Reduction();
}
void Rational::multiplication(Rational r)
{
numerator = numerator * r.numerator;
denominator = denominator * r.denominator;
Reduction();
}
void Rational::division(Rational r)
{
numerator = numerator * r.denominator;
denominator = denominator * r.numerator;
Reduction();
}
void Rational::printRational()
{
cout << numerator << "/" << denominator;
}
void Rational::printRationalAsDouble()
{
cout << numerator / static_cast<float>( denominator );
}
void Rational::Reduction()
{
int largest;
largest = numerator > denominator ? numerator : denominator;
int gcd = 0; // greatest common divisor
for ( int loop = 2; loop <= largest; loop++ )
if ( numerator % loop == 0 && denominator % loop == 0 )
gcd = loop;
if (gcd != 0)
{
numerator /= gcd;
denominator /= gcd;
} // end if
}
Tester provided by instructer.
#include <iostream>
using std::cout;
using std::endl;
#include "Rational.h" // include definition of class Rational
int main()
{
Rational c( 2, 6 ), d( 7, 8 ), x; // creates three rational objects
c.printRational(); // prints rational object c
cout << " + ";
d.printRational(); // prints rational object d
x = c.addition( d ); // adds object c and d; sets the value to x
cout << " = ";
x.printRational(); // prints rational object x
cout << '\n';
x.printRational(); // prints rational object x
cout << " = ";
x.printRationalAsDouble(); // prints rational object x as double
cout << "\n\n";
c.printRational(); // prints rational object c
cout << " - ";
d.printRational(); // prints rational object d
x = c.subtraction( d ); // subtracts object c and d
cout << " = ";
x.printRational(); // prints rational object x
cout << '\n';
x.printRational(); // prints rational object x
cout << " = ";
x.printRationalAsDouble(); // prints rational object x as double
cout << "\n\n";
c.printRational(); // prints rational object c
cout << " x ";
d.printRational(); // prints rational object d
x = c.multiplication( d ); // multiplies object c and d
cout << " = ";
x.printRational(); // prints rational object x
cout << '\n';
x.printRational(); // prints rational object x
cout << " = ";
x.printRationalAsDouble(); // prints rational object x as double
cout << "\n\n";
c.printRational(); // prints rational object c
cout << " / ";
d.printRational(); // prints rational object d
x = c.division( d ); // divides object c and d
cout << " = ";
x.printRational(); // prints rational object x
cout << '\n';
x.printRational(); // prints rational object x
cout << " = ";
x.printRationalAsDouble(); // prints rational object x as double
cout << endl;
system("pause");
return 0;
} // end main
I feel like I am on the right track as my header and function definitions compile. Not sure why the tester is having trouble. Any help would be appreciated.