Write a class Rational for performing arithmetic with fractions. Use integers to
ID: 3663923 • Letter: W
Question
Write a class Rational for performing arithmetic with fractions. Use integers to represent the (private) variables numerator and denominator. Provide a constructor function that enables an object of this class to be initialized, and which should contain default values in case no input is provided. The constructor should also guarantee that the fraction is stored in re duced form, so that for example) the inputs 2 and 4, for the fraction 4 will be stored with numerator 1 and denominator 2 Also provide public functions for the following operations Addition of two rational numbers, where the result should be stored in reduced form Subtraction of two rational numbers, where the result should be stored in reduced form Multiplication of two rational numbers, where the result should be stored in reduced form Division of two rational numbers, where the result should be stored in reduced form. Printing rational numbers Cusing cout in the form of a b where a is the numerator and b is the denominator Printing rational numbers in floating point formatExplanation / Answer
1. #include<iostream.h>
2. #include <iostream>
3. #include <iostream>
using namespace std;
int fraction_multiply (int x1, int y1, int x2, int y2);
int fraction_divide (int x1, int y1, int x2, int y2);
int main()
{
int x1, y1, x2, y2;
cout << "Enter a number for x1 and y1: ";
cin >> x1 >> y1;
cout << "Enter a number for x2 and y2: ";
cin >> x2 >> y2;
return 0;
}
void multiply()
{
num = (x1 * x2);
denom = (y1 * y2);
}
4. #include <iostream>
using namespace std;
int fraction_divide (int x1, int y1, int x2, int y2);
int main()
{
int x1, y1, x2, y2;
cout << "Enter a number for x1 and y1: ";
cin >> x1 >> y1;
cout << "Enter a number for x2 and y2: ";
cin >> x2 >> y2;
return 0;
}
void divide()
{
num = (x1 * y2);
denom = (y1 * x2);
}
5. printing output in fraction format
6. float point format should follow a small logic, is
#include <iostream> #include <iomanip> int main()
{ double my_double = 42.0; std::cout << std::fixed << std::setw(11) << std::setprecision(6) << my_double << std::endl; return 0; } Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.