In: Computer Science
Create a c++ class called Fraction in which the objects will represent fractions.
Remember:
do not reduce fractions,
do not use "const,"
do not provide any constructors,
do not use three separate files.
You will not receive credit for the assignment if you do any of these things. In your single file, the class declaration will come first, followed by the definitions of the class member functions, followed by the client program.
It is required that you provide these member functions:
A set() operation that takes two integer arguments, a numerator and a denominator, and sets the calling object accordingly.
Arithmetic operations that add, subtract, multiply, and divide Fractions. These should be implemented as value returning functions that return a Fraction object. They should be named addedTo, subtract, multipliedBy, and dividedBy. In these functions you will need to declare a local "Fraction" variable, assign to it the result of the mathematical operation, and then return it.
A boolean operation named isEqualTo that compares two Fraction objects for equality. Since you aren't reducing your Fractions, you'll need to do this by cross-multiplying. A little review: if numerator1 * denominator2 equals denominator1 * numerator2, then the Fractions are equal.
An output operation named print that displays the value of a Fraction object on the screen in the form numerator/denominator.
-Your class has to have exactly two data members, one in order to represent the numerator of the Fraction being represented, and the other to represent the denominator of the Fraction that is being represented.
Hint for how you will set up your arithmetic operation functions: You need 2 Fractions. One is the parameter, one is the calling object. The function multiplies the calling object times the parameter and returns the result. In some ways, it is similar to the comesBefore() function from the lesson. That function also needs two Fractions, and one is the calling object and one is the parameter.
When adding/ subtracting Fractions, keep in mind 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.
Use the program below. You have to copy and paste this and use it as your client program. The output that should be produced when the provided client program is run with your class is also given below so that you can check your results.
I highly recommend for you to design your class incrementally. Ex: you should first implement only the set function and the output function, and then test what you have so far. After this code has been thoroughly debugged, you should add additional member functions, testing each one thoroughly as it is added. You can do this by creating your own client program to test the code at each stage; but, it would probably be better to use the provided client program and comment out code that relates to member functions that you have not yet implemented.
As you can see from the sample output given below, you are not required to reduce Fractions or change improper Fractions into mixed numbers for printing. Just print it as an Improper Fraction. You are also not required to deal with negative numbers, either in the numerator or the denominator.
Here is the client program.(you may not change it or else you will not receive credit)
#include <iostream>
using namespace std;
int main()
{
Fraction f1;
Fraction f2;
Fraction result;
f1.set(9, 8);
f2.set(2, 3);
cout << "The product of ";
f1.print();
cout << " and ";
f2.print();
cout << " is ";
result = f1.multipliedBy(f2);
result.print();
cout << endl;
cout << "The quotient of ";
f1.print();
cout << " and ";
f2.print();
cout << " is ";
result = f1.dividedBy(f2);
result.print();
cout << endl;
cout << "The sum of ";
f1.print();
cout << " and ";
f2.print();
cout << " is ";
result = f1.addedTo(f2);
result.print();
cout << endl;
cout << "The difference of ";
f1.print();
cout << " and ";
f2.print();
cout << " is ";
result = f1.subtract(f2);
result.print();
cout << endl;
if (f1.isEqualTo(f2)){
cout << "The two Fractions are equal." << endl;
} else {
cout << "The two Fractions are not equal." << endl;
}
}
This should be the output:
the product of 9/8 and 2/3 is 18/24
the quotient of 9/8 and 2/3 is 27/16
the sum of 9/8 and 2/3 is 43/24
the difference of 9/8 and 2/3 is 11/24
the two Fractions are not equal.
Please find all the explanation to the code within the code itself as comments.
We need to create a Fraction class with the data members and member functions as specified in the problem statement.
We won't make any changes to the Client code and run our class code corresponding to it.
The output for the code has been added in the end and it matches the output specified in the problem statement.
Please note that we have used this pointer notation in order to distinguish the data members of the object on which the member functions are called and the data members of the object that are being passed as an argument with the member functions.
(
this pointer is an implicit pointer that points to the object in consideration. It is associated with all the non-static data members and member functions of the class and is provided by the compiler. We can use it to reference our object which is under consideration. For example, in our Fraction class, we can access the non-static data member numerator by using this pointer:
cout << this -> numerator << "\n";
like we did in the case of our print () function. As this is a pointer, we access the data associated with the corresponding object using the arrow( -> ) operator.
)
Here is the full code:
#include <iostream>
using namespace std;
// class definition
class Fraction {
// two data members
// one representing numerator
int numerator;
// other, representing denominator
int denominator;
public:
void set (int numerator, int denominator);
Fraction addedTo (Fraction f);
Fraction subtract (Fraction f);
Fraction multipliedBy (Fraction f);
Fraction dividedBy (Fraction f);
bool isEqualTo (Fraction f);
void print ();
};
// set () functions sets up the attributes of the Fraction object
void Fraction :: set (int numerator, int denominator) {
this -> numerator = numerator;
this -> denominator = denominator;
}
// this function adds Fraction argument f the with the calling Fraction object
Fraction Fraction :: addedTo (Fraction f) {
Fraction result;
// according to the problem statement, we can get the denominator of the sum
// by multiplying the denominators of the two fractions
result.denominator = (this -> denominator) * f.denominator;
// we can find the numerator by cross multiplying the numerators and denominators and adding them
result.numerator = ((this -> numerator) * f.denominator) + ((this -> denominator) * f.numerator);
// return the result Fraction
return result;
}
// this function subtracts the Fraction argument f from the calling Fraction object
Fraction Fraction :: subtract (Fraction f) {
Fraction result;
// according to the problem statement, we can get the denominator of the difference
// by multiplying the denominators of the two fractions
result.denominator = (this -> denominator) * f.denominator;
// we can find the numerator by cross multiplying the numerators and denominators and subtracting them
result.numerator = ((this -> numerator) * f.denominator) - ((this -> denominator) * f.numerator);
// return the result Fraction
return result;
}
// this function multiplies the Fraction argument f by the calling Fraction object
Fraction Fraction ::multipliedBy (Fraction f) {
Fraction result;
// according to the problem statement, we can get the product of the two fractions
// by multiplying the Fractions corresponding parameters with each other
result.numerator = (this -> numerator) * f.numerator;
result.denominator = (this -> denominator) * f.denominator;
// return the result Fraction
return result;
}
// this function divides the Fraction argument f by the calling Fraction object
Fraction Fraction :: dividedBy (Fraction f) {
Fraction result;
// we have found the this by using the fact that
// the dividing a Fraction by another is same as multiplying
// the reciprocal of the Fraction with the other
Fraction reciprocal;
reciprocal.numerator = f.denominator;
reciprocal.denominator = f.numerator;
// Now, that we have the reciprocal, we will now multiply the
// reciprocal object with the object on which the dividedBy () function is called
result.numerator = (this -> numerator) * reciprocal.numerator;
result.denominator = (this -> denominator) * reciprocal.denominator;
// return the result Fraction
return result;
}
// this function checks if the Fraction argument f is equal to the calling Fraction object
bool Fraction :: isEqualTo (Fraction f) {
// as provided in the problem statement
// we can know that if the two fractions are
// equal or not by using this if condition
if (((this -> numerator) * f.denominator) == ((this -> denominator) * f.numerator)) {
return true;
}
// otherwise
return false;
}
// this function will print the Fraction in the specified format
void Fraction :: print () {
// we will print the Fraction as specified in the problem statement
// numerator/denominator
cout << (this -> numerator) << "/" << (this -> denominator);
}
// client program provided in the
// problem statement
int main()
{
Fraction f1;
Fraction f2;
Fraction result;
f1.set(9, 8);
f2.set(2, 3);
cout << "The product of ";
f1.print();
cout << " and ";
f2.print();
cout << " is ";
result = f1.multipliedBy(f2);
result.print();
cout << endl;
cout << "The quotient of ";
f1.print();
cout << " and ";
f2.print();
cout << " is ";
result = f1.dividedBy(f2);
result.print();
cout << endl;
cout << "The sum of ";
f1.print();
cout << " and ";
f2.print();
cout << " is ";
result = f1.addedTo(f2);
result.print();
cout << endl;
cout << "The difference of ";
f1.print();
cout << " and ";
f2.print();
cout << " is ";
result = f1.subtract(f2);
result.print();
cout << endl;
if (f1.isEqualTo(f2)){
cout << "The two Fractions are equal." << endl;
} else {
cout << "The two Fractions are not equal." << endl;
}
}
Please refer to the screenshots of the code for understanding the indentation.
Let's look at the output of the code when run with the client code provided in the problem statement: