Problem Create a class named Fraction containing two integer data members named
ID: 3761468 • Letter: P
Question
Problem Create a class named Fraction containing two integer data members named num and denom used to store the numerator and denominator of a fraction having the form num/denom. Specification Include default and parametrized constructors. The default constructor should initialize num and denom to 1. Neither constructor should allow a 0 denominator value. Create a member function to display an object's data members. Overload operator functions for addition +: a/b + c/d = (a * d + b * c) / (b * d) subtraction -: a/b - c/d = (a * d - b * c) / (b * d) multiplication *: a/b * c/d = (a * c) / (b * d) division /: a/b / c/d = (a * d) / (b * c) stream insertion: > to input a fraction from the keyboard. Include the class in a C++ program that tests each member function. use header and .cpp files
Explanation / Answer
#include <iostream>
using namespace std;
void add(int top1, int bottom1, int top2, int bottom2, int& topResult, int& bottomResult)
{
if(bottom1 != 0 && bottom2 != 0)
{
int n1 = (b1 * b2) / b1;
int n2 = (b1 * b2) / b2;
bottomResult = b1 * b2;
int newNum1 = n1 * top1;
int newNum2 = n2 * top2;
topResult = newNum1 + newNum2;
}
else
cout<<" Invalid input, denominator cannot be 0."<<endl;
}
void subtract(int top1, int b1, int top2, int b2, int& topResult, int& bottomResult)
{
if(b1 != 0 && b2 != 0)
{
int n1 = (b1 * b2) / b1;
int n2 = (b1 * b2) / b2;
bottomResult = b1 * b2;
int newNum1 = n1 * top1;
int newNum2 = n2 * top2;
topResult = newNum1 - newNum2;
}
else
cout<<" Invalid input, denominator cannot be 0."<<endl;
}
void multiply(int top1, int b1, int top2, int b2, int& topResult, int& bottomResult)
{
if(b1 != 0 && b2 != 0)
{
bottomResult = b1 * b2;
topResult = top1 * top2;
}
else
cout<<" Invalid input, denominator cannot be 0."<<endl;
}
void divide(int top1, int b1, int top2, int b2, int& topResult, int& bottomResult)
{
if(b1 != 0 && b2 != 0)
{
int newTop = b2;
int newBottom = top2;
topResult = top1 * newTop;
bottomResult = b1 * newBottom;
}
else
cout<<" Invalid input. "<<endl;
}
int main()
{
int choice, num1, num2, denom1, denom2, numResult = 0, denomResult = 0;
cout<<"Adding fractions: 1"<<endl;
cout<<"Subtracting fractions: 2"<<endl;
cout<<"Multiplying fractions: 3"<<endl;
cout<<"Dividing fractions: 4"<<endl;
cout<<" Enter fractions: "<<endl;
cin>>num1>>denom1>>num2>>denom2;
cout<<"Enter operation (number): "<<endl;
cin>>choice;
cout<<endl;
switch(choice)
{
case 1:
add(num1, denom1, num2, denom2, numResult, denomResult);
break;
case 2:
subtract(num1, denom1, num2, denom2, numResult, denomResult);
break;
case 3:
multiply(num1, denom1, num2, denom2, numResult, denomResult);
break;
case 4:
divide(num1, denom1, num2, denom2, numResult, denomResult);
break;
default:
cout<<" Invalid input. "<<endl;
}
cout<<"Result = "<<numResult<<"/"<<denomResult<<endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.