Question

Problem 1: C/C++ will convert it to Write a Fraction class. An example of a fraction is h. Note that have at least the for this problem, it should still be displayed as 1/2. You should following two private member variables: numerator (top part), and denominator (bottom part). Overload the following and Also, implement the default operators constructor and a second constructor that takes two arguments for the numerator and the denominator Make sure the denominator is not 0. You do not have to worry about displaying the fraction to its lowest terms. For example, it is okay to display 4/8. However, when comparing 1/2 with 4/8, these two fractions are equal Hint: 1) multiply the denominator of the first fraction with the numerator of the second fraction, 2) multiply the numerator of t first fraction with the denominator of the second fraction, 3) if the two multiplications are equal, then the two fractions are equal Use the following main0 function for initial testing. int main() Fraction one, two //You need to check for denominator being 0, else you will lose points cout The input format should be: NNNN n, cout<< Enter the first fraction: In Cin one cout Enter the second fraction: In cin two The two fractions cout Testing (one two are are not) equalln cout Testing one two one two end return 0;

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Fraction.h

#include <iostream>
using namespace std;

class Fraction
{
private:
int numerator;
int denominator;
public:
Fraction();
Fraction(int, int);
Fraction operator+(const Fraction&);
Fraction operator-(const Fraction&);
Fraction operator*(const Fraction&);
Fraction operator/(const Fraction&);
bool operator==(const Fraction& rhs);
friend ostream &operator<<( ostream &output, const Fraction & );
friend istream &operator>>( istream &input, Fraction & );
};

Fraction.cpp

#include "Fraction.h"
#include <iostream>
#include <stdlib.h>
using namespace std;

Fraction::Fraction() { this->denominator = 1; this->numerator = 0;}

Fraction::Fraction(int num, int denom)
{
this->numerator = num;
this->denominator = denom;
}
bool Fraction::operator==(const Fraction& rhs)
{
return (this->numerator == rhs.numerator) && (this->denominator == rhs.denominator);
}
Fraction Fraction::operator+(const Fraction& c)
{
Fraction r = Fraction();
r.numerator = (this->numerator*c.denominator + this->denominator*c.numerator);
r.denominator = (this->denominator * c.denominator);
return r;
}

Fraction Fraction::operator-(const Fraction& c)
{
Fraction r = Fraction();
r.numerator = (this->numerator*c.denominator - this->denominator*c.numerator);
r.denominator = (this->denominator * c.denominator);
return r;
}

Fraction Fraction::operator*(const Fraction& c)
{
Fraction r = Fraction();
r.numerator = (this->numerator*c.numerator);
r.denominator = (this->denominator * c.denominator);
return r;
}

Fraction Fraction::operator/(const Fraction& c)
{
Fraction r = Fraction();
r.numerator = (this->numerator*c.denominator);
r.denominator = (this->denominator * c.numerator);
return r;
}


ostream &operator<<( ostream &output, const Fraction &F )
{
output << F.numerator <<"/"<<F.denominator ;
return output;
}

istream &operator>>( istream &input, Fraction &F ) {
input >> F.numerator >> F.denominator;
if (F.denominator == 0)
{
cout << "Denominator cannot be zero. Try again." << endl;
exit(1);
}
return input;
}

main.cpp

#include "Fraction.h"
#include <iostream>

using namespace std;

void displayResult(const string &, const Fraction &, const Fraction&, const Fraction&);

int main() {
Fraction one, two, result;
int choice;

cout << "The input format should be: NN NN\n";
cout << "Enter the first fraction: \n";
cin >> one;
cout << "Enter the second fraction: \n";
cin >> two;

cout << "Testing ==: The two fractions "\
<< (one == two ? "are" : "are not") << " equal\n";
cout << "Testing +: " << one << " + " << two << " = " << one + two << endl;
cout << "Testing -: " << one << " - " << two << " = " << one - two << endl;
cout << "Testing *: " << one << " * " << two << " = " << one * two << endl;
cout << "Testing /: " << one << " / " << two << " = " << one / two << endl;

return 0;
}

Please rate positively if this solved your question.

Add a comment
Know the answer?
Add Answer to:
Write a Fraction class. An example of a fraction is 1/2. Note that C/C++ will convert...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In C++ Fix any errors you had with HW5(Fraction class). Implement a function(s) to help with...

    In C++ Fix any errors you had with HW5(Fraction class). Implement a function(s) to help with Fraction addition. \**************Homework 5 code*****************************/ #include<iostream> using namespace std; class Fraction { private: int wholeNumber, numerator, denominator;    public: //get methods int getWholeNumber() { return wholeNumber; } int getNumerator() { return numerator; } int getDenominator() { return denominator; } Fraction()// default constructor { int w,n,d; cout<<"\nEnter whole number : "; cin>>w; cout<<"\nEnter numerator : "; cin>>n; cout<<"\nEnter denominator : "; cin>>d; while(d == 0)...

  • You have been developing a Fraction class for Teacher’s Pet Software that contains several fields and...

    You have been developing a Fraction class for Teacher’s Pet Software that contains several fields and functions. a. Add four arithmetic operators, +, -, *, and /. Remember that to add or subtract two Fractions, you first must convert them to Fractions with a common denominator. You multiply two Fractions by multiplying the numerators and multiplying the denominators. You divide two Fractions by inverting the second Fraction, then multiplying. After any arithmetic operation, be sure the Fraction is in proper...

  • C++ For this assignment you will be building on the Original Fraction class you began last...

    C++ For this assignment you will be building on the Original Fraction class you began last week. You'll be making four major changes to the class. [15 points] Delete your set() function. Add two constructors, a default constructor that assigns the value 0 to the Fraction, and a constructor that takes two parameters. The first parameter will represent the initial numerator of the Fraction, and the second parameter will represent the initial denominator of the Fraction. Since Fractions cannot have...

  • Please use C++ and add comments to make it easier to read. Do the following: 1)...

    Please use C++ and add comments to make it easier to read. Do the following: 1) Add a constructor with two parameters, one for the numerator, one for the denominator. If the parameter for the denominator is 0, set the denominator to 1 2) Add a function that overloads the < operator 3) Add a function that overloads the * operator 4) Modify the operator<< function so that if the numerator is equal to the denominator it just prints 1...

  • Hello, I'm looking to modify my fraction program to include constructors and destructors. Here are all...

    Hello, I'm looking to modify my fraction program to include constructors and destructors. Here are all the instructions: Create constructors default two argument three argument copy constructor Create a destructor. The destructor should set whole and numerator to zero and denominator to one. Add cout statements to the constructors and the destructor so you know when it's getting executed. Add system ("pause") to the destructor so you know when it executes. Only positive values allowed If denominator is 0 set...

  • C++ Create a Rational Number (fractions) class like the one in Exercise 9.6 of the textbook....

    C++ Create a Rational Number (fractions) class like the one in Exercise 9.6 of the textbook. Provide the following capabilities: Create a constructor that prevents a 0 denominator in a fraction, reduces or simplifies fractions (by dividing the numerator and the denominator by their greatest common divisor) that are not in reduced form, and avoids negative denominators. Overload the addition, subtraction, multiplication, and division operators for this class. Overload the relational and equality operators for this class. Provide a function...

  • Do the following: 1) Add a constructor with two parameters, one for the numerator, one for...

    Do the following: 1) Add a constructor with two parameters, one for the numerator, one for the denominator. If the parameter for the denominator is 0, set the denominator to 1 2) Add a function that overloads the < operator 3) Add a function that overloads the * operator 4) Modify the operator<< function so that if the numerator is equal to the denominator it just prints 1 5) Modify the operator>> function so that after it reads the denominator...

  • C++ Question Your class should support the following operations on Fraction objects: • Construction of a...

    C++ Question Your class should support the following operations on Fraction objects: • Construction of a Fraction from two, one, or zero integer arguments. If two arguments, they are assumed to be the numerator and denominator, just one is assumed to be a whole number, and zero arguments creates a zero Fraction. Use default parameters so that you only need a single function to implement all three of these constructors. You should check to make sure that the denominator is...

  • C++ and/or using Xcode... Define a class of rational numbers. A rational number is a number...

    C++ and/or using Xcode... Define a class of rational numbers. A rational number is a number that can be represented as the quotient of two integers. For example, 1/2, 3/4, 64/2, and so forth are all rational numbers. (By 1/2, etc., we mean the everyday meaning of the fraction, not the integer division this expression could produce in a C++ program). Represent rational numbers as two values of type int, one for the numerator and one for the denominator. Call...

  • C++ 1st) [Note: This assignment is adapted from programming project #7 in Savitch, Chapter 10, p.616.]...

    C++ 1st) [Note: This assignment is adapted from programming project #7 in Savitch, Chapter 10, p.616.] Write a rational number class. Recall that a rational number is a ratio-nal number, composed of two integers with division indicated. The division is not carried out, it is only indicated, as in 1/2, 2/3, 15/32. You should represent rational numbers using two int values, numerator and denominator. A principle of abstract data type construction is that constructors must be present to create objects...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT