Must be written in C++. include <iostream> and using namespace std; You have bee
ID: 3915632 • Letter: M
Question
Must be written in C++. include <iostream> and using namespace std;
You have been developing a Fraction class for Teacher’s Pet Software that contains several fields and functions. Add two constructors to the class. The first accepts two integer values representing the numerator and denominator. If a single integer is passed to the constructor, use it as the numerator, and use a default value of 1 for the denominator. If no values are passed to the constructor, use a default value of 0 for the numerator and 1 for the denominator. When any Fraction is constructed with a 0 argument for the denominator, force the denominator value to 1. The second constructor requires three arguments: a whole number portion for a Fraction, a numerator, and a denominator. This constructor executes when any Fraction object is instantiated using three integer arguments. As with the other constructor, when any Fraction is constructed with a 0 argument for the denominator, force the denominator value to 1. Whenever a Fraction object is constructed, automatically reduce the Fraction to the proper format. For example, a Fraction created using 0, 2, and 4 as arguments should be reduced to 1/2, and a Fraction created as 3 10/2 should be reduced to 8 0/1. Write a main() function that declares several Fraction objects, and confirm that the class works correctly. Save the file as Fraction.cpp.
Explanation / Answer
Hello, I have answered a similar question before, so I’m referring my own solution here, after making proper modifications needed. Comments are included, go through it, learn how things work and let me know if you have any doubts. Thanks
//Code
#include<iostream>
using namespace std;
class Fraction{
int numerator;
int denominator;
//private method to reduce the Fraction number into lowest form
void reduce(){
/**
* Finding the GCD
*/
int GCD = gcd(numerator, denominator);
/**
* removing the GCD from numer..and denom..
*/
numerator = numerator / GCD;
denominator = denominator / GCD;
}
/**
* A private method to calculate the greatest common divisor of two numbers
*/
int gcd(int x, int y) {
//removing sign
x= x>=0? x: -x;
y= y>=0? y: -y;
if (x % y == 0)
return y;
return gcd(y, x % y);
}
public:
//constructor to initialize the Fraction with a numerator and denominator
Fraction(int n, int d){
//initializing numerator and denominator
numerator=n;
denominator=d;
if(denominator==0){
denominator=1;//preventing dinominator being 0
}
//reducing to simplest form
reduce();
}
//constructor to initialize the Fraction with a given numerator (denominator as 1)
Fraction(int n){
//initializing numerator and denominator
numerator=n;
denominator=1;
}
//default constructor to initialize the Fraction with 0 numerator (denominator as 1)
Fraction(){
//initializing numerator=0 and denominator=1
numerator=0;
denominator=1;
}
//constructor to initialize the Fraction with 3 parts
Fraction(int w,int n, int d){
//initializing numerator and denominator
numerator=n;
denominator=d;
if(denominator==0){
denominator=1;//preventing dinominator being 0
}
//converting the 3 part fraction into a/b form
numerator=(denominator*w)+numerator;
//reducing to lowest terms
reduce();
}
//getter for numerator
int getNumerator() const{
return numerator;
}
//getter for dinominator
int getDenominator() const{
return denominator;
}
};
//overloaded << operator for displaying Fraction number in a/b form
ostream & operator << (ostream &out, const Fraction &r)
{
out<<r.getNumerator()<<"/"<<r.getDenominator();
}
int main(){
//Testing the Fraction class
Fraction f1(10,5); //should be reduced to 2/1
cout<<f1<<endl;
Fraction f2(11,7); //already in reduced state 11/7
cout<<f2<<endl;
Fraction f3; // 0/1
cout<<f3<<endl;
Fraction f4(3,10,2); // 3 10/2= 8/1
cout<<f4<<endl;
return 0;
}
/*OUTPUT*/
2/1
11/7
0/1
8/1
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.