Question

Hello, im looking for help on adding overload operators to my code. It needs to consist...

Hello, im looking for help on adding overload operators to my code. It needs to consist of

  • + (addition) for the Fraction class
  • * (multiplication), no need not reduce
  • == (equality operator)
  • = (assignment operator)
  • << and >> so in main you can use << and >> as follows:  cin>>frac1; cout<< frac1; where frac1 is a an object of the Fraction class  

the original code is:

#include <iostream>

using namespace std;

class Fractions {
private:
   int numerator;
   int denominator;

public:
   void NumeratorDenominator(int n, int d);

   int getNumerator() const{
       return numerator;
   }

   int getDenominator()const {
       return denominator;
   }

   void print()const;
      
};

int main() {
   Fractions f1;
   Fractions f2;

   cout << " Testing 9/0" << endl;
   f1.NumeratorDenominator(9, 0);
   f1.print();
   cout << "Testing 9/2" << endl;
   f2.NumeratorDenominator(9, 2);
   f2.print();
   cout << " Testing -9/4" << endl;
   f1.NumeratorDenominator(-9, 4);
   f1.print();
   cout << " Testing 10/2" << endl;
   f1.NumeratorDenominator(10, 2);
   f1.print();
   cout << " Testing 3/4" << endl;
   f1.NumeratorDenominator(3, 4);
   f1.print();


   system("pause");
}


void Fractions::NumeratorDenominator(int n, int d) {

   if (d <= 0 || n < 0) {
       numerator = 0;
       denominator = 1;
   }
   else {
       numerator = n;
       denominator = d;
   }
}


void Fractions:: print()const {
   if (numerator<denominator)
       cout << numerator<< "/" << denominator << endl;
   else {
       cout << numerator / denominator << " " << numerator % denominator << "/" << denominator << endl;
   }
}

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

Note: Could you plz go through this code and let me know if u need any changes in this.Thank You
_________________

#include <iostream>

using namespace std;

class Fraction {
private:
int numerator;
int denominator;

public:
void NumeratorDenominator(int n, int d);

int getNumerator() const{
return numerator;
}

int getDenominator()const {
return denominator;
}

void setNumerator(int numer)
{
    this->numerator=numer;
}
int setDenominator(int denom)
{
this->denominator=denom;
}

void print()const;
Fraction operator+(const Fraction& f) const; // operator+()
Fraction operator*(const Fraction& f) const; // operator*()
bool operator==(const Fraction& f) const; // operator==()
Fraction& operator=(const Fraction & src);
/**
* Friend function declarations
* Output fraction number to an output stream, Example 3+4i
*/
friend std::ostream& operator<<(std::ostream& dout, const Fraction&);
/**
* read fraction number to an input stream
*/
friend std::istream& operator>>(std::istream& din, Fraction&);
};
void Fraction::NumeratorDenominator(int n, int d) {

if (d <= 0 || n < 0) {
numerator = 0;
denominator = 1;
}
else {
numerator = n;
denominator = d;
}


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

Fraction Fraction::operator+(const Fraction& f) const
{
Fraction frac;
int a = numerator;
int b = denominator;
int c = f.numerator;
int d = f.denominator;
int sumnumer = (a * d + b * c);
int denom = (b * d);
frac.setNumerator(sumnumer);
frac.setDenominator(denom);
return frac;
}

Fraction Fraction::operator*(const Fraction& f) const
{
Fraction frac;
int a = this->numerator;
int b = this->denominator;
int c = f.numerator;
int d = f.denominator;
int mulnumer = (a * c);
int muldenom = (b * d);
frac.setNumerator(mulnumer);
frac.setDenominator(muldenom);
return frac;
}
// Function implementation which display Fraction numbers by using the operator overloading '<<'
ostream& operator<<(ostream& dout, const Fraction& c)
{
dout << c.numerator << "/" << c.denominator;
return dout;
}
// Function implementation which read Fraction numbers by using the operator overloading '>>'
istream& operator>>(istream& din, Fraction& c)
{
char ch;
din >> c.numerator;
din >> ch;
din >> c.denominator;
return din;
}

bool Fraction::operator==(const Fraction& f) const
{
int a = this->numerator;
int b = this->denominator;
int c = f.numerator;
int d = f.denominator;
double n1 = a * d;
double d1 = b * d;
double n2 = c * b;
double d2 = d * b;

if ((n1 == n2) && (d1 == d2))
return true;
else
return false;
}

void Fraction:: print()const {
if (numerator<denominator)
cout << numerator<< "/" << denominator << endl;
else {
cout << numerator / denominator << " " << numerator % denominator << "/" << denominator << endl;
}
}

int main() {
Fraction f1;
Fraction f2;

cout << " Testing 9/0" << endl;
f1.NumeratorDenominator(9, 0);
f1.print();
cout << "Testing 9/2" << endl;
f2.NumeratorDenominator(9, 2);
f2.print();
cout << " Testing -9/4" << endl;
f1.NumeratorDenominator(-9, 4);
f1.print();
cout << " Testing 10/2" << endl;
f1.NumeratorDenominator(10, 2);
f1.print();
cout << " Testing 3/4" << endl;
f1.NumeratorDenominator(3, 4);
f1.print();
Fraction f3=f1+f2;
cout<<f1<<" + "<<f2<<" = "<<f3<<endl;
Fraction f4=f1*f2;
cout<<f1<<" * "<<f2<<" = "<<f4<<endl;
if(f3==f4)
{
    cout<<f3<<" is equal to "<<f4<<endl;
}
else
{
    cout<<f3<<" is not equal to "<<f4<<endl;
}
Fraction f5=f3;
cout<<"After Assignment :"<<f5<<endl;
Fraction f6;
cout<<"Enter Fraction:";
cin>>f6;
cout<<"You Entered Fraction :"<<f6<<endl;

system("pause");
}


_________________________

Output:


_______________Could you plz rate me well.Thank You

Add a comment
Know the answer?
Add Answer to:
Hello, im looking for help on adding overload operators to my code. It needs to consist...
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
  • 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...

  • 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)...

  • In this assignment you are required to complete a class for fractions. It stores the numerator...

    In this assignment you are required to complete a class for fractions. It stores the numerator and the denominator, with the lowest terms. It is able to communicate with iostreams by the operator << and >>. For more details, read the header file. //fraction.h #ifndef FRACTION_H #define FRACTION_H #include <iostream> class fraction { private: int _numerator, _denominator; int gcd(const int &, const int &) const; // If you don't need this method, just ignore it. void simp(); // To get...

  • 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...

  • 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...

  • 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++ I am using visual studio for it. Make a copy of the Rational class you...

    C++ I am using visual studio for it. Make a copy of the Rational class you created in the previous Lab. Modify the class. Replace all your mathematical, input, and output functions with overloaded operators. Overload the following 12 operators: + - * / < > = = ! = <= >= >> << In order to test your class in your main function, prompt the user for a numerator and a denominator for your first object. Repeat the prompt...

  • NEED ASAP PLEASE HELP Task: --------------------------------------------------------------------------------------...

    NEED ASAP PLEASE HELP Task: --------------------------------------------------------------------------------------------------------------------------------- Tasks to complete: ---------------------------------------------------------------------------------------------------------------------------------------- given code: ----------------------------------------------------------------------------------------------------------------------------------------------- main.cpp #include <iostream> #include <iomanip> #include "fractionType.h" using namespace std; int main() { fractionType num1(5, 6); fractionType num2; fractionType num3; cout << fixed; cout << showpoint; cout << setprecision(2); cout << "Num1 = " << num1 << endl; cout << "Num2 = " << num2 << endl; cout << "Enter the fraction in the form a / b: "; cin >> num2; cout << endl; cout <<...

  • C++ CODE /* This is program project 2 on page 695. * Before you begin the...

    C++ CODE /* This is program project 2 on page 695. * Before you begin the project, please read the project description * on page 695 first. * * Author: Your Name * Version: Dates */ #include <iostream> #include <cmath> #include <cassert> using namespace std; class Fraction { public: // constructor Fraction(int a, int b); // generate a fraction which is a/b Fraction(int a); // generate a fraction which is a/1 Fraction(); // generate a fraction which is 0/1. i.e...

  • 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...

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