Fraction Class Project

In this project you are requested to design a fraction class in C++.  This class will include methods and overloaded operators that will make working with fractions easier. The following class interface file is to be used in your project.  Add the class and methods using the MSVC support I discussed in class.  Start working on everything but the overloaded operators.  These operators will be added in the next project.  You will also need a GCD function to use in reducing etc. Look it up.

#pragma once
#include <iostream>
using namespace std;
class Fraction
{
       int num;  // This will allow negative or positive fractions
       unsigned int denom;
public:
       Fraction();// Default constructor. Set num to 0 and denom to 1.
       Fraction(int);// construct int/1
       Fraction(int, unsigned int); // Paramaterized constructor
       ~Fraction();// Destructor.  Not really a big deal here since no dynamic allocation.
       void AddTo(Fraction f); // adds f to the object fraction. Does not reduce. Modifies object fraction 
       Fraction Add(Fraction f);// adds f to object fraction and returns result. Object fraction not modified
       void Reduce(); // Reduces the fraction object by dividing by the GCD.
        //Overloading output operator.  Note that you must put the ostream implementation here and not in the .cpp file.
       friend ostream &operator<<(ostream &output, Fraction &f)
       {
              return output << f.num << "/" << f.denom;
       }
};

Implement these methods and write a test main program that thoroughly exercises this class using only stdin and stdout as we did in the warmup project.  When you think the class works properly have your main program read in the following data and process each line. Each line will have an operation followed by the associated fractions.   For example the line Add 2 3 4 5  requests the addition of 2/3 and 4/5.  The line Reduce 12 18  request the reduction of 12/18.  For Add 2 3 4 5 output 2/3+4/5=22/15. Here is the data set you can test with.  I will use a different test set. Note that you read from stdin and write to stdout as we did in project#0.  I do not want to see any files opened, capice!? The format of the data should make it easy for you to read in.

Add 2 3 4 5
Add 4 5 7 20
Add 12 1 18 1
Add 123 256 12 120
Add 25 1 256 513
Reduce 256 312
Reduce 124 996
Reduce 23 29

TO TURN IN:  turn in a well documented complete project on a jump drive.  You are to only have this project on the drive.   Copy the executable to the root directory  of the jump drive, rename it to fract.exe.  Place the test data set in the root and test it via command line execution.   IE run    fract.exe < data.dat  at the root in DOS.     Also turn in a printout of your source as well as the output for the above data.  Do Not print the output window, write it to a file and print that.

If you don’t know how to do something just come to my office.  It’s that simple. Capice!?

Comments are closed.