Question

NEED ASAP PLEASE HELP

Task:

Overloading and Templates Rational fractions are of the form a/b, in which a and b are integers and b#0 In this exercise, by
Fractions are compared as follows: a/b op c/d if ad op bc, in which op is any of the relational operations. For example, alb<

cin >x; should store 2/3 in x . The statement: cout <<x y << endl; should output the value of x y in fraction form. The state

---------------------------------------------------------------------------------------------------------------------------------

Tasks to complete:

Tasks Addition works Subtraction works operator works

----------------------------------------------------------------------------------------------------------------------------------------

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 << "New value of num2 = " << num2 << endl;

num3 = num1 + num2;

cout << "Num3 = " << num3 << endl;
cout << num1 << " + " << num2 << " = " << num1 + num2 << endl;
cout << num1 << " * " << num2 << " = " << num1 * num2 << endl;

num3 = num1 - num2;

cout << "Num3 = " << num3 << endl;
cout << num1 << " - " << num2 << " = " << num1 - num2 << endl;
cout << "(" << num1 << ") / (" << num2 << ") = " << num1 / num2 << endl;

return 0;
}

--------------------------------------------------------------------------------------------------------------------------

fractionType.cpp

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

using namespace std;

// overload operator <<
ostream& operator << (ostream& os, const fractionType& fraction)
{
// write overloaded code here
}

// overload operator >>
istream& operator>> (istream& is, fractionType& fraction)
{
// write overloaded code here
}

// overload operator ==
bool fractionType::operator==(fractionType rightFr) const
{
// write overloaded code here
}

// overload operator !=
bool fractionType::operator!=(fractionType rightFr) const
{
// write overloaded code here
}

// overload operator <=
bool fractionType::operator<=(fractionType rightFr) const
{
// write overloaded code here
}

// overload operator <
bool fractionType::operator<(fractionType rightFr) const
{
// write overloaded code here
}

// overload operator >=
bool fractionType::operator>=(fractionType rightFr) const
{
// write overloaded code here
}

// overload operator >
bool fractionType::operator>(fractionType rightFr) const
{
// write overloaded code here
}

// constructor
fractionType::fractionType(int num, int deno)
{
numerator = num;

if (deno == 0)
{
cout << "denominator cannot be zero" << endl;
denominator = 1;
}
else
denominator = deno;
}

void fractionType::setFraction(int num, int deno)
{
numerator = num;

if (deno == 0)
{
cout << "denominator cannot be zero" << endl;
denominator = 1;
}
else
denominator = deno;
}

// overload the operator +
fractionType fractionType::operator+(fractionType rightFr)
{
// write overloaded code here
}

// overload the operator *
fractionType fractionType::operator*(fractionType rightFr)
{
// write overloaded code here
}

// overload operator -
fractionType fractionType::operator-(fractionType rightFr)
{
// write overloaded code here
}

// overload operator /
fractionType fractionType::operator/(fractionType rightFr)
{
// write overloaded code here
}

--------------------------------------------------------------------------------------------------------

fractionType.h

//Specification file fractionType.h

#ifndef H_fraction
#define H_fraction
  
#include <iostream>

using namespace std;

class fractionType
{
   // overload stream insertion and extraction operators
friend ostream& operator<< (ostream&, const fractionType&);
friend istream& operator>> (istream&, fractionType&);

public:
void setFraction(int num, int den);

fractionType(int num = 0, int den = 1);
//Constructor

fractionType operator+(fractionType rightFr);
//overload +
fractionType operator*(fractionType rightFr);
//overload *
fractionType operator-(fractionType rightFr);
//overload -
fractionType operator/(fractionType rightFr);
//overload /
  
//overload relational operators
bool operator==(fractionType rightFr) const;
bool operator!=(fractionType rightFr) const;
bool operator<=(fractionType rightFr) const;
bool operator<(fractionType rightFr) const;
bool operator>=(fractionType rightFr) const;
bool operator>(fractionType rightFr) const;   

private:
int numerator; //variable to store the numerator
int denominator; //variable to store the denominator
};

#endif

------------------------------------------------------------------------------------

Overloading and Templates Rational fractions are of the form a/b, in which a and b are integers and b#0 In this exercise, by "fractions" we mean rational fractions. Suppose a/b and c/dare fractions. Arithmetic operations on fractions are defined by the following rules a/b c/d (ad bc)/bd a/b - c/d -(ad bc)/bd a/b c/d -ac/bd ас (a/b) / (c/d) -ad/bc, in which c/d # 0
Fractions are compared as follows: a/b op c/d if ad op bc, in which op is any of the relational operations. For example, alb
0 0
Add a comment Improve this question Transcribed image text
Answer #1

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 << "New value of num2 = " << num2 << endl;

num3 = num1 + num2;

cout << "Num3 = " << num3 << endl;
cout << num1 << " + " << num2 << " = " << num1 + num2 << endl;
cout << num1 << " * " << num2 << " = " << num1 * num2 << endl;

num3 = num1 - num2;

cout << "Num3 = " << num3 << endl;
cout << num1 << " - " << num2 << " = " << num1 - num2 << endl;
cout << "(" << num1 << ") / (" << num2 << ") = " << num1 / num2 << endl;

return 0;
}

--------------------------------------------------------------------------------------------------------------------------

fractionType.cpp

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

using namespace std;

// overload operator <<
ostream& operator << (ostream& os, const fractionType& fraction)
{
os << fraction.numerator << " / " << fraction.denominator;
return os;
}
// overload operator >>
istream& operator>> (istream& is, fractionType& fraction)
{
is >> fraction.numerator >> fraction.denominator;
return is;
}
// overload operator ==
bool fractionType::operator==(fractionType rightFr) const
{
return ( this->numerator == rightFr.numerator && this->denominator == rightFr.denominator);
}
// overload operator !=
bool fractionType::operator!=(fractionType rightFr) const
{
return ( this->numerator != rightFr.numerator || this->denominator == rightFr.denominator);
}
// overload operator <=
bool fractionType::operator<=(fractionType rightFr) const
{
return ( this->numerator <= rightFr.numerator && this->denominator == rightFr.denominator);
}
// overload operator <
bool fractionType::operator<(fractionType rightFr) const
{
return ( this->numerator < rightFr.numerator && this->denominator == rightFr.denominator);
}
// overload operator >=
bool fractionType::operator>=(fractionType rightFr) const
{
return ( this->numerator >= rightFr.numerator && this->denominator == rightFr.denominator);
}
// overload operator >
bool fractionType::operator>(fractionType rightFr) const
{
return ( this->numerator > rightFr.numerator && this->denominator == rightFr.denominator);
}
// constructor
fractionType::fractionType(int num, int deno)
{
numerator = num;
if (deno == 0)
{
cout << "denominator cannot be zero" << endl;
denominator = 1;
}
else
denominator = deno;
}
void fractionType::setFraction(int num, int deno)
{
numerator = num;
if (deno == 0)
{
cout << "denominator cannot be zero" << endl;
denominator = 1;
}
else
denominator = deno;
}
// overload the operator +
fractionType fractionType::operator+(fractionType rightFr)
{
fractionType temp;
temp.numerator=numerator*rightFr.denominator+rightFr.numerator*denominator;
temp.denominator=denominator*rightFr.denominator;
  
return temp;
}
// overload the operator *
fractionType fractionType::operator*(fractionType rightFr)
{
   fractionType temp;
temp.numerator=numerator*rightFr.numerator;
temp.denominator=denominator*rightFr.denominator;

return temp;
}
// overload operator -
fractionType fractionType::operator-(fractionType rightFr)
{
fractionType temp;
temp.numerator=numerator*rightFr.denominator-rightFr.numerator*denominator;
temp.denominator=denominator*rightFr.denominator;
  
return temp;
}
// overload operator /
fractionType fractionType::operator/(fractionType rightFr)
{
fractionType temp;
temp.numerator=numerator*rightFr.denominator;
temp.denominator=denominator*rightFr.numerator;

return temp;
}


--------------------------------------------------------------------------------------------------------

fractionType.h

//Specification file fractionType.h

#ifndef H_fraction
#define H_fraction
  
#include <iostream>

using namespace std;

class fractionType
{
   // overload stream insertion and extraction operators
friend ostream& operator<< (ostream&, const fractionType&);
friend istream& operator>> (istream&, fractionType&);

public:
void setFraction(int num, int den);

fractionType(int num = 0, int den = 1);
//Constructor

fractionType operator+(fractionType rightFr);
//overload +
fractionType operator*(fractionType rightFr);
//overload *
fractionType operator-(fractionType rightFr);
//overload -
fractionType operator/(fractionType rightFr);
//overload /
  
//overload relational operators
bool operator==(fractionType rightFr) const;
bool operator!=(fractionType rightFr) const;
bool operator<=(fractionType rightFr) const;
bool operator<(fractionType rightFr) const;
bool operator>=(fractionType rightFr) const;
bool operator>(fractionType rightFr) const;   

private:
int numerator; //variable to store the numerator
int denominator; //variable to store the denominator
};

#endif

Output:

Num1 = 5 / 6
Num2 = 0 / 1
Enter the fraction in the form a / b: 1 4
New value of num2 = 1 / 4
Num3 = 26 / 24
5 / 6 + 1 / 4 = 26 / 24
5 / 6 * 1 / 4 = 5 / 24
Num3 = 14 / 24
5 / 6 - 1 / 4 = 14 / 24
(5 / 6) / (1 / 4) = 20 / 6

Do ask if any doubt. Please upvote.

Add a comment
Know the answer?
Add Answer to:
NEED ASAP PLEASE HELP Task: --------------------------------------------------------------------------------------...
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
  • I couldn't find where to comment. But it has to be written in C++!!!!! Task-1: Rational...

    I couldn't find where to comment. But it has to be written in C++!!!!! Task-1: Rational fractions are of the form a / b, in which a and b are integers and ! ≠ 0. In this exercise, by ‘‘fractions’’ we mean rational fractions. Suppose a / b and c / d are fractions. Arithmetic operations and relational operations on fractions are defined by the following rules: Arithmetic Operations: a/b + c/d = (ad + bc)/bd a/b – c/d =...

  • Task: Tasks to complete: ------------------------------------------------------------------------------------------------------------------------------------------ given code: --------------------...

    Task: Tasks to complete: ------------------------------------------------------------------------------------------------------------------------------------------ given code: ------------------------------------------------------------------------------------------------------------------------------------------ main.cpp #include #include "rectangleType.h" using namespace std; // part e int main() { rectangleType rectangle1(10, 5); rectangleType rectangle2(8, 7); rectangleType rectangle3; rectangleType rectangle4; cout << "rectangle1: " << rectangle1 << endl; cout << "rectangle2: " << rectangle2 << endl; rectangle3 = rectangle1 + rectangle2;    cout << "rectangle3: " << rectangle3 << endl; rectangle4 = rectangle1 * rectangle2;    cout << "rectangle4: " << rectangle4 << endl; if (rectangle1 > rectangle2) cout << "Area...

  • So the assignment is to write a program that have the user entered 3 numbers and...

    So the assignment is to write a program that have the user entered 3 numbers and than it will sort it by ascending order. Here are the code I wrote, but it won't compiled and I kept on getting an error message "Using uninitiazed memory" for all the variables. Can someone please help take a look and see whats going on? #include <iostream> using namespace std; int main() { //variables int num1, num2, num3; int lowest, middle, highest; //user inputs...

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

  • In the following code, it gets hung up at    cout << "Number1 * Number2 =...

    In the following code, it gets hung up at    cout << "Number1 * Number2 = " << number1 * number2 << endl; giving an error of "no math for operator<<" what am i doing wrong? Thank you #include <iostream> #include <cctype> #include <cstdlib> using namespace std; class Rational //class for rational numbers (1/2, 5/9, ect..) {    public:        Rational(int numerator, int denominator);        Rational(int numberator);        Rational(); //default        friend istream& operator >>(istream& ins,...

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

  • Please zoom in so the pictures become high resolution. I need three files, FlashDrive.cpp, FlashDrive.h and...

    Please zoom in so the pictures become high resolution. I need three files, FlashDrive.cpp, FlashDrive.h and user_main.cpp. The programming language is C++. I will provide the Sample Test Driver Code as well as the codes given so far in text below. Sample Testing Driver Code: cs52::FlashDrive empty; cs52::FlashDrive drive1(10, 0, false); cs52::FlashDrive drive2(20, 0, false); drive1.plugIn(); drive1.formatDrive(); drive1.writeData(5); drive1.pullOut(); drive2.plugIn(); drive2.formatDrive(); drive2.writeData(2); drive2.pullOut(); cs52::FlashDrive combined = drive1 + drive2; // std::cout << "this drive's filled to " << combined.getUsed( )...

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

  • Please help me solve the following. Thank you in advance :) Answer the following five questions...

    Please help me solve the following. Thank you in advance :) Answer the following five questions related to code output (Q2 i through m) What is the output of the following C+ coaer int numl; int num2; int *p = &num1 ; p = &num2 ; *p 25; num1 num2 + 6; p = &num! ; num2 = 73; *p = 47; cout << *p << " "<< numl <<""<< num2 << endl; What is the output of the following...

  • SCREENSHOTS ONLY PLEASE!!! DON'T POST ACTUAL CODE PLEASE LEAVE A SCREENSHOT ONLY! ACTUAL TEXT IS NOT NEEDED!!! myst...

    SCREENSHOTS ONLY PLEASE!!! DON'T POST ACTUAL CODE PLEASE LEAVE A SCREENSHOT ONLY! ACTUAL TEXT IS NOT NEEDED!!! mystring.h: //File: mystring1.h // ================ // Interface file for user-defined String class. #ifndef _MYSTRING_H #define _MYSTRING_H #include<iostream> #include <cstring> // for strlen(), etc. using namespace std; #define MAX_STR_LENGTH 200 class String { public: String(); String(const char s[]); // a conversion constructor void append(const String &str); // Relational operators bool operator ==(const String &str) const; bool operator !=(const String &str) const; bool operator >(const...

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