Project 3B: Designing and implementing a C++ class data type: Fraction Class A m
ID: 3920512 • Letter: P
Question
Project 3B: Designing and implementing a C++ class data type: Fraction Class
A multifile project
Write a fraction class whose objects will represent fractions. You should provide the following member functions:
Two constructors, a default constructor which assigns the value 0 to the fraction, and a constructor that takes two parameters. The first parameter will represent the initial numerator of the fraction, and the second parameter will represent the initial denominator of the fraction. (6 pts for specification and implementation)
Arithmetic operations that add, subtract, multiply, and divide fractions. These should be implemented as four value returning functions that return a fraction object. They should be named AddedTo,Subtract, MultipliedBy, and DividedBy. Note the returned fraction object must be in reduced form. (20 pts for specification and implementation)
A boolean operation named isGreaterThan that compares two fraction objects to determine whether one fraction object is greater than the other. (5 pts for specification and implementation)
An input operation named getFraction that prompts the user for two integer values to be used to define a new fraction object. One value is to be used for the numerator and the other for the denominator of the fraction object. on the screen in the form numerator/denominator. (5 pts for specification and implementation)
An output operation named showFraction that displays the value of a fraction object on the screen in the form numerator/denominator. (4 pts for specification and implementation)
NOTE: You may not implement the above member functions as inline functions but should include the function member declarations (or prototypes) in the class specification file (fraction.h) and then write the member function definitions in the implementation file (fraction.cpp).
Your class specification should name the class fraction (not Fraction) and should have exactly two data members, one to represent the numerator of the fraction being represented, and one to represent the denominator of the fraction being represented.
When adding or subtracting fractions, remember that you must first find the common denominator. The easy way to do this is to multiply the denominators together and use that product as the common denominator.
I am providing a client program for you below. You should copy and paste this into a file and use it as your client program (client.cpp) that will be a part of your mutlitfile project. The output that should be produced when the provided client program (client.cpp) is run with your completed class specification (fraction.h) and implementation files (fraction.cpp) is provided below, so that you can check your results. Since this project requires multiple files you will need to first create a new project in your compiler and then write the source code for your class specification file, the implementation file and then add the client code below to the project. For an example with explanations of such C++ class related multifile projects review the Software Engineering Tip: Seperating Class, Specification, Implementation, and Client Code in Chapter 7.11 in your Gaddis textbook (pgs. 446-452)
I strongly suggest that you design your fraction class incrementally. For example, you should first implement only the constructors and the output function, comment out portions of the client code that is not needed and then test what you have so far. Once this code has been thoroughly debugged, you should then add additional member functions, testing each one thoroughly as it is added. You might do this by creating your own client program to test the code at each stage; however, it would probably be better to use the provided client program below and comment out portions of code that relates to member functions that you have not yet implemented.
Here is the client program (save the contents below onto a new file client.cpp)
As you can see from the sample output given below, except for the arithmetic operations you are not required to change improper fractions into mixed numbers for printing. Just print it as an improper fraction. You are, however, required to reduce fractions that are the result of an arithmetic operation. See the reduced fraction result for the multiplication and division in the sample output. You are not required to deal with negative numbers, either in the numerator or the denominator.
Compiling/running the client program (client.cpp) above along with the fraction class specification (fraction.h) and implementation file (fraction.cpp) should produce the output shown below:
After successfully completing this mutifile C++ class project (ie., your program output should match the above), use the file submission area and upload just two source code files (1) the "fraction class" specification file (fraction.h), followed by source code for your "fraction class" implementation file (fraction.cpp). No need to submit the client code or the output produced when you ran your mutlitfile C++ class project using the client program above. You may comment out portions of the client code as you incrementally build and test your fraction class specification and implementation code but do not change the client program in any way for the final version. Changing the client program will result in a grade of 0 on the project.
*important reminder: for multi-file projects to work in your C++ compiler/IDE environment ....make sure you create a new project folder and place all related files in the same folder. Also check that you have the appropriate #include statements in the class implementation and client code files. Xcode users should place these files in the debug folder.
Please also make sure to include appropriate function documentation as needed (see handout on Function Documentation: Assertions and Dataflow comments) and separate your member functions definitions in the implementation file with at least 1 inches of whitespace. You must also include the constant key words for all member functions that are "observer type" of functions. DO NOT use global variables and inline member functions.
Your working project 3B folder in your C++ compiler should have the following files:
fraction.h (class specification file)
fraction.cpp (class implementation file)
client.cpp (client code using the fraction class)
Submit your source code for fraction.h and fraction.cpp only. DO NOT submit the client code already provided in the project specification. Before you submit the two required files, make sure that you have included your name in the comment area at the top of both the fraction.h and fraction.cpp files.
Explanation / Answer
fraction.h
#include <iostream>
#include <cassert>
using namespace std;
class fraction
{
private:
int num;
int den;
public:
fraction();
fraction(int a, int b);
void simplify();
fraction AddedTo(fraction f) const;
fraction Subtract(fraction f) const;
fraction MultipliedBy(fraction f) const;
fraction DividedBy(fraction f) const;
bool isEqualTo(fraction f) ;
bool isGreaterThan(fraction f) ;
void showFraction() const;
void getInput();
void getFraction();
};
fraction.cpp
#include "fraction.h"
fraction::fraction(){
num = 0;
den = 1;
}
fraction::fraction(int a, int b){
num = a;
assert(b != 0);
den = b;
simplify();
}
void fraction::simplify(){
int max;
if (num <= den)
max = num;
else
max = den;
for (int i = max; i>=2; i--){
if (num % i == 0 && den % i == 0){
den = den / i;
num = num / i;
}
}
}
fraction fraction::AddedTo(fraction f) const
{ fraction result;
result.num= (num * f.den)+(f.num * den);
result.den = den * f.den;
result.simplify();
return result;
}
fraction fraction::Subtract(fraction f) const
{
fraction result;
result.num= (num * f.den)-(f.num * den);
result.den=den * f.den;
result.simplify();
return result;
}
fraction fraction::MultipliedBy(fraction f) const
{
fraction result;
result.num= num * f.num;
result.den=den * f.den;
result.simplify();
return result;
}
fraction fraction::DividedBy(fraction f) const
{
fraction result;
result.num= num * f.den;
result.den=den * f.num;
result.simplify();
return result;
}
bool fraction::isEqualTo(fraction f)
{
return (num * f.den==f.num * den);
}
void fraction::showFraction() const
{
cout << num << "/" << den;
}
bool fraction::isGreaterThan(fraction f)
{
return (num * f.den > f.num * den);
}
void fraction::getFraction(){
cout << "Enter numerator:";
cin >> num;
cout << "Enter denominator:";
cin >> den;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.