Instructions: Work on the following C++ program. This program will test your abi
ID: 3839058 • Letter: I
Question
Instructions: Work on the following C++ program. This program will test your ability to create classes. 1. Create a class named fraction which will represent a new data type corresponding to fractions.
Your class should create the following private members: - Integers n and d corresponding to the numerator and denominator of the fraction - A function gcd which returns the greatest common divisor of 2 inte- gers (to help you reduce the fraction). And the following public member functions: - 3 constructors which will create any fraction when declared with 2 integers, will create a/ 1 when declared with 1 integer, and will create 0 / 1 when declared with no integers. - num which returns the numerator of the fraction - denom which returns the denominator of the fraction - reduce which reduces the fraction to its lowest terms (using the gcd function above). This function changes the value of n and d appropriately. - convert which returns a double representing the fraction (i.e. 3 / 4 becomes 0 . 75) - show which displays the fraction (as a fraction) to the monitor (a fraction like a/ 1 should be displayed as a , and a fraction like 0 /b should be displayed as 0). Your main function should (in this order): (a) Test all three constructors using show (b) Test num and denom (c) Test reduce (d) Test convert (e) If you do the bonus (see below), test these. Bonus: Create 4 functions which can add, subtract, multiply, or divide fractions. The answer calculated should be in lowest terms (like 3 / 4 in- stead of 6 / 8).
Explanation / Answer
#include<iostream.h>
class Fraction {
private:
int numerator;
int denominator;
public:
Fraction(int a, int b){
numerator = a;
denominator = b;
}
Fraction(int a){
numerator = a;
denominator = 1;
}
Fraction(){
numerator = 0;
denominator = 1;
}
int num(){
return numerator;
}
int denom(){
return denominator;
}
int gcd(){
int c;
a = numerator;
b = denominator;
while ( a != 0 ) {
c = a;
a = b%a;
b = c;
}
return b;
}
void reduce(){
int c;
c = gcd();
numerator = numerator / c;
denominator = denominator / c;
}
double convert(){
return numerator/denominator;
}
void show(){
cout << numerator << "/" << denominator << endl;
}
};
void main() {
Fraction f1();
Fraction f2(3);
Fraction f3(4,16);
f1.show();
f2.show();
f3.show();
f3.num();
f3.denom();
f3.conver();
f3.reduce();
f3.show();
}