ifndef RATIONALH if this compiler macro is not defined defi
#ifndef RATIONAL_H   // if this compiler macro is not defined
 #define RATIONAL_H   //   then define it so this file will not be processed again
#include \"stdafx.h\" // use only for Microsoft Visual Studio C++
 #include <iostream>
 using namespace std;
class Rational
 {
    // Friend functions are actually declared outside the scope of the
    // class but have the right to access public and private data and
    // member function members that belong to the class. The friend
    // function below gives the << operator for ostreams (including cout)
    // the ability to output a Rational object by accessing its member data.
    friend ostream &operator<< (ostream &out, Rational const &r);
public:
    Rational(int num = 0, int denom = 1); // also provides default constructor
   Rational add(Rational right);
    Rational operator+ (Rational right); // + addition operator
    Rational operator+= (Rational right); // += addition assignment operator
    Rational operator- (Rational right); // + addition operator
    Rational operator-= (Rational right); // += addition assignment operator
    void display();
    operator double() const; // convert Rational to double
private:
    int numerator;
    int denominator;
    // helper functions are private and not accessible by the main program
    int LCD(int v1, int v2);
    Rational setRational(int n, int d);
 };
#endif
#include \"stdafx.h\"
 #include <iostream>
 #include \"Rational.h\"
 using namespace std;
// By using the default parameter settings in Rational.h, this
 // constructor also provides the default constructor Rational()
 Rational::Rational(int num, int denom)
 {
    setRational(num, denom); // set numerator and denominator, reduce fraction, fix the sign
 }
// Helper function to fix a zero denominator and fix the sign if denominator is negative
 Rational Rational::setRational(int n, int d) // helper function
 {
    numerator = n;
    denominator = d;
   // if denominator == 0 then set it = 1
    if (denominator == 0)
        denominator = 1;
   if (denominator < 0) // if denominator is neg, multiply num and denom by -1
    {
        numerator = -numerator; // fix sign of numerator +/-
        denominator = -denominator; // denominator always +
    }
   int lcd = LCD(numerator, denominator);
    if (denominator != 0)
    {
        numerator /= lcd;
        denominator /= lcd;
    }
    return *this; // return the current object
 }
// find the lowest common divisor using a recursive function
 int Rational::LCD(int v1, int v2)
 {
    if (v2 == 0) return v1;
    else return LCD(v2, v1%v2);
 }
Rational Rational::add(Rational right)
 {
    int newNumerator;
    int newDenominator;
   newNumerator = numerator*right.denominator + right.numerator*denominator;
    newDenominator = denominator * right.denominator;
   // create a new Rational object and return it
    return setRational(newNumerator, newDenominator);
 }
// the operator+ method does the same thing as the add method
 Rational Rational::operator+ (Rational right)
 {
    int newNumerator;
    int newDenominator;
   newNumerator = numerator*right.denominator + right.numerator*denominator;
    newDenominator = denominator * right.denominator;
   // create a new Rational object and return it
    return setRational(newNumerator, newDenominator);
 }
Rational Rational::operator+= (Rational right)
 {
    numerator = numerator*right.denominator + right.numerator*denominator;
    denominator = denominator * right.denominator;
   // fix the sign, reduce the fraction and return the current object
    return setRational(numerator, denominator);
 }
// the operator- method does the same thing as the add method
 Rational Rational::operator- (Rational right)
 {
    int newNumerator;
    int newDenominator;
   newNumerator = numerator*right.denominator - right.numerator*denominator;
    newDenominator = denominator * right.denominator;
   // create a new Rational object and return it
    return setRational(newNumerator, newDenominator);
 }
Rational Rational::operator-= (Rational right)
 {
    numerator = numerator*right.denominator - right.numerator*denominator;
    denominator = denominator * right.denominator;
   // fix the sign, reduce the fraction and return the current object
    return setRational(numerator, denominator);
 }
Rational::operator double() const // convert Rational to double and return
 {
    return double(numerator) / double(denominator);
 }
// Display a Rational number using the display() member method
 void Rational::display()
 {
    cout << numerator << \'/\' << denominator;
 }
// Display a Rational number using << and a friend function.
 // Friend functions are not part of the class and their code must be
 // declared outside of the class with no :: Scope Resolution Operator.
 // All function arguments must have their type/class defined
 ostream &operator<< (ostream &out, Rational const &r)
 {
    out << r.numerator << \'/\' << r.denominator;
    return out;
 }
// Rational.cpp : Defines the entry point for the console application.
 // Create a Rational class defination
 // Rational(numerator, denominator)
 //
#include \"stdafx.h\"   // only for Microsoft Visual Studio C++
 #include \"Rational.h\" // double quotes = find file in project folder
 #include <iostream> // angle brackets = find file in compiler folder
 using namespace std;
// function prototypes
 void displayNumbers(double, Rational, Rational, Rational, Rational);
 void display(Rational);
int main(int argc, char* argv[])
 {
    // class object
    // | |
    // V V
    double num1 = 1.5; // sample definition of a double number
    Rational num2; // call the constructor with no arguments
    Rational num3(3, 4); // call the constructor setting num3 to 3/4
    Rational num4(2, 3); // call the constructor setting num4 to 2/3
    Rational num5; // call the constructor with no arguments
    display(num1);
    displayNumbers(num1, num2, num3, num4, num5);
cout << \"verify that addition works correctly\" << endl;
   // use the add member method (without overloading)
    num2 = num3.add(num4); // num3 + num4 = 3/4 + 2/3 = 9/12 + 8/12 = 17/12
    cout << \"num2 = num3.add(num4)\" << endl << \"num2,display();\" << endl;
    num2.display(); // using the display( ) member function
    cout << endl << endl;
   // use the operator+ method
    num2 = num3.operator+(num4); // num3 + num4 = 3/4 + 2/3 = 9/12 + 8/12 = 17/12
    cout << \"num3.operator+(num4)\" << endl;
    displayNumbers(num1, num2, num3, num4, num5);
   // use the + overloaded operator, use the friend operator << to display the result
    num2 = num3 + num4;     // num3 + num4 = 3/4 + 2/3 = 9/12 + 8/12 = 17/12
    cout << \"num2 = num3 + num4\" << endl;
    displayNumbers(num1, num2, num3, num4, num5);
   // use the += overloaded operator, use the friend operator << to display the result
    num5 = num3 += num4;     // num3 + num4 = 3/4 + 2/3 = 9/12 + 8/12 = 17/12
    cout << \"num5 = num3 += num4\" << endl;
    displayNumbers(num1, num2, num3, num4, num5);
   num3 = Rational(3, 4);
    cout << \"Reset num3 back to 3/4\" << endl;
    displayNumbers(num1, num2, num3, num4, num5);
   cout << endl << \"---------------------------------------\" << endl;
    cout << \"verify that subtraction works correctly\" << endl;
   // use the - overloaded operator, use the friend operator << to display the result
    num2 = num3 - num4;     // num3 + num4 = 3/4 - 2/3 = 9/12 - 8/12 = 1/12
    cout << \"num2 = num3 - num4\" << endl;
    displayNumbers(num1, num2, num3, num4, num5);
   // use the -= overloaded operator, use the friend operator << to display the result
    num5 = num3 -= num4;     // num3 - num4 = 3/4 - 2/3 = 9/12 - 8/12 = 1/12
    cout << \"num5 = num3 += num4\" << endl;
    displayNumbers(num1, num2, num3, num4, num5);
   num3 = Rational(3, 4);
    cout << \"Reset num3 back to 3/4\" << endl;
    displayNumbers(num1, num2, num3, num4, num5);
   // convert the Rational number to double
    cout << \"double(num2) = \" << double(num2) << endl; // 17/12 = 1.4166
    cout << endl;
    return 0;
 }
void display(Rational num1)
 {
    cout << \"num1 = \" << num1 << endl;
 }
 void displayNumbers(double num1, Rational num2, Rational num3,
    Rational num4, Rational num5)
 {
    cout << \"num1\\t num2\\t num3\\t num4\\t num5\ \";
    cout << num1 << \"\\t \" << num2 << \"\\t \" << num3 << \"\\t \"
        << num4 << \"\\t \" << num5 << endl << endl;
 }
Solution
#ifndef RATIONAL_H
 #define RATIONAL_H
 #include \"stdafx.h\"
 #include <iostream>
 using namespace std;
 class Rational
 {
 friend ostream &operator<< (ostream &out, Rational const &r);
 public:
 Rational(int num = 0, int denom = 1); // also provides default constructor
 Rational add(Rational right);
 Rational operator+ (Rational right); // + addition operator
 Rational operator+= (Rational right); // += addition assignment operator
 Rational operator- (Rational right); // + addition operator
 Rational operator-= (Rational right); // += addition assignment operator
 void display();
 operator double() const; // convert Rational to double
 private:
 int numerator;
 int denominator;
 // helper functions are private and not accessible by the main program
 int LCD(int v1, int v2);
 Rational setRational(int n, int d);
 };
 #endif
 
 
 #include \"stdafx.h\"
 #include <iostream>
 #include \"Rational.h\"
 using namespace std;
Rational::Rational(int num, int denom)
 {
 setRational(num, denom);
 }
Rational Rational::setRational(int n, int d)
 {
 numerator = n;
 denominator = d;
 // if denominator == 0 then set it = 1
 if (denominator == 0)
 denominator = 1;
 if (denominator < 0) // if denominator is neg, multiply num and denom by -1
 {
 numerator = -numerator; // fix sign of numerator +/-
 denominator = -denominator; // denominator always +
 }
 int lcd = LCD(numerator, denominator);
 if (denominator != 0)
 {
 numerator /= lcd;
 denominator /= lcd;
 }
 return *this; // return the current object
 }
 // find the lowest common divisor using a recursive function
 int Rational::LCD(int v1, int v2)
 {
 if (v2 == 0) return v1;
 else return LCD(v2, v1%v2);
 }
 Rational Rational::add(Rational right)
 {
 int newNumerator;
 int newDenominator;
 newNumerator = numerator*right.denominator + right.numerator*denominator;
 newDenominator = denominator * right.denominator;
 // create a new Rational object and return it
 return setRational(newNumerator, newDenominator);
 }
 // the operator+ method does the same thing as the add method
 Rational Rational::operator+ (Rational right)
 {
 int newNumerator;
 int newDenominator;
 newNumerator = numerator*right.denominator + right.numerator*denominator;
 newDenominator = denominator * right.denominator;
 // create a new Rational object and return it
 return setRational(newNumerator, newDenominator);
 }
 Rational Rational::operator+= (Rational right)
 {
 numerator = numerator*right.denominator + right.numerator*denominator;
 denominator = denominator * right.denominator;
 // fix the sign, reduce the fraction and return the current object
 return setRational(numerator, denominator);
 }
 // the operator- method does the same thing as the add method
 Rational Rational::operator- (Rational right)
 {
 int newNumerator;
 int newDenominator;
 newNumerator = numerator*right.denominator - right.numerator*denominator;
 newDenominator = denominator * right.denominator;
 // create a new Rational object and return it
 return setRational(newNumerator, newDenominator);
 }
 Rational Rational::operator-= (Rational right)
 {
 numerator = numerator*right.denominator - right.numerator*denominator;
 denominator = denominator * right.denominator;
 // fix the sign, reduce the fraction and return the current object
 return setRational(numerator, denominator);
 }
 Rational::operator double() const // convert Rational to double and return
 {
 return double(numerator) / double(denominator);
 }
 // Display a Rational number using the display() member method
 void Rational::display()
 {
 cout << numerator << \'/\' << denominator;
 }
 // Display a Rational number using << and a friend function.
 // Friend functions are not part of the class and their code must be
 // declared outside of the class with no :: Scope Resolution Operator.
 // All function arguments must have their type/class defined
 ostream &operator<< (ostream &out, Rational const &r)
 {
 out << r.numerator << \'/\' << r.denominator;
 return out;
 }
 
#include \"stdafx.h\" // only for Microsoft Visual Studio C++
 #include \"Rational.h\" // double quotes = find file in project folder
 #include <iostream> // angle brackets = find file in compiler folder
 using namespace std;
 // function prototypes
 void displayNumbers(double, Rational, Rational, Rational, Rational);
 void display(Rational);
 int main(int argc, char* argv[])
 {
 // class object
 // | |
 // V V
 double num1 = 1.5; // sample definition of a double number
 Rational num2; // call the constructor with no arguments
 Rational num3(3, 4); // call the constructor setting num3 to 3/4
 Rational num4(2, 3); // call the constructor setting num4 to 2/3
 Rational num5; // call the constructor with no arguments
 display(num1);
 displayNumbers(num1, num2, num3, num4, num5);
 cout << \"verify that addition works correctly\" << endl;
 // use the add member method (without overloading)
 num2 = num3.add(num4); // num3 + num4 = 3/4 + 2/3 = 9/12 + 8/12 = 17/12
 cout << \"num2 = num3.add(num4)\" << endl << \"num2,display();\" << endl;
 num2.display(); // using the display( ) member function
 cout << endl << endl;
 // use the operator+ method
 num2 = num3.operator+(num4); // num3 + num4 = 3/4 + 2/3 = 9/12 + 8/12 = 17/12
 cout << \"num3.operator+(num4)\" << endl;
 displayNumbers(num1, num2, num3, num4, num5);
 // use the + overloaded operator, use the friend operator << to display the result
 num2 = num3 + num4; // num3 + num4 = 3/4 + 2/3 = 9/12 + 8/12 = 17/12
 cout << \"num2 = num3 + num4\" << endl;
 displayNumbers(num1, num2, num3, num4, num5);
 // use the += overloaded operator, use the friend operator << to display the result
 num5 = num3 += num4; // num3 + num4 = 3/4 + 2/3 = 9/12 + 8/12 = 17/12
 cout << \"num5 = num3 += num4\" << endl;
 displayNumbers(num1, num2, num3, num4, num5);
 num3 = Rational(3, 4);
 cout << \"Reset num3 back to 3/4\" << endl;
 displayNumbers(num1, num2, num3, num4, num5);
 cout << endl << \"---------------------------------------\" << endl;
 cout << \"verify that subtraction works correctly\" << endl;
 // use the - overloaded operator, use the friend operator << to display the result
 num2 = num3 - num4; // num3 + num4 = 3/4 - 2/3 = 9/12 - 8/12 = 1/12
 cout << \"num2 = num3 - num4\" << endl;
 displayNumbers(num1, num2, num3, num4, num5);
 // use the -= overloaded operator, use the friend operator << to display the result
 num5 = num3 -= num4; // num3 - num4 = 3/4 - 2/3 = 9/12 - 8/12 = 1/12
 cout << \"num5 = num3 += num4\" << endl;
 displayNumbers(num1, num2, num3, num4, num5);
 num3 = Rational(3, 4);
 cout << \"Reset num3 back to 3/4\" << endl;
 displayNumbers(num1, num2, num3, num4, num5);
 // convert the Rational number to double
 cout << \"double(num2) = \" << double(num2) << endl; // 17/12 = 1.4166
 cout << endl;
 return 0;
 }
 void display(Rational num1)
 {
 cout << \"num1 = \" << num1 << endl;
 }
void displayNumbers(double num1, Rational num2, Rational num3,
 Rational num4, Rational num5)
 {
 cout << \"num1\\t num2\\t num3\\t num4\\t num5\ \";
 cout << num1 << \"\\t \" << num2 << \"\\t \" << num3 << \"\\t \"
 << num4 << \"\\t \" << num5 << endl << endl;
 }








