Question

Consider this declaration of the class Fraction: class Fraction { private: int numerator, denominator; public: Fraction...

Consider this declaration of the class Fraction:
class Fraction {
private:
int numerator, denominator;
public:
Fraction operator+(const Fraction & frac2);
Fraction & setNumerator(int numer);
Fraction & setDenominator(int denom);
};
a. Why is there only one parameter, frac2, used with operator+, even though addition
has two operands?
b. Write the operator:
Fraction Fraction::operator+ (Fraction frac2){
}
c. Write setNumerator() and setDenominator() as cascading functions:
Fraction & Fraction::setNumerator(int numer) {
}
Fraction & Fraction::setDenominator(int denom) {
}
d. Assign the fractions frac1 to 3/5, and frac2 to 7/8, using cascading functions:
Fraction frac1; frac1______________________________;
Fraction * frac2 = new Fraction; frac2______________________________;
e. Write an operator Fraction::operator!() which performs inversion.
For example, if x is 3/5 then !x is 5/3.

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

a. Why is there only one parameter, frac2, used with operator+, even though addition
has two operands?

This is because the operation + is applied on the object which calls it using this pointer.
b. Write the operator:
Fraction Fraction::operator+ (Fraction frac2){
}

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

c. Write setNumerator() and setDenominator() as cascading functions:
Fraction & Fraction::setNumerator(int numer) {
}
Fraction & Fraction::setDenominator(int denom) {
}

Fraction & Fraction::setNumerator(int numer)
{
       numerator = numer;
   return *this;
}
Fraction & Fraction::setDenominator(int denom)
{
   denominator = denom;
   return *this;
}

d. Assign the fractions frac1 to 3/5, and frac2 to 7/8, using cascading functions:
Fraction frac1; frac1______________________________;
Fraction * frac2 = new Fraction; frac2______________________________;

Fraction frac1;
   frac1.setNumerator(3);
   frac1.setDenominator(5);

   Fraction * frac2 = new Fraction;
   frac2->setNumerator(7);
   frac2->setDenominator(8);

e. Write an operator Fraction::operator!() which performs inversion.
For example, if x is 3/5 then !x is 5/3.

Fraction Fraction::operator!()
{
   int temp = this->numerator;
   this->numerator = this->denominator;
   this->denominator = temp;
   return *this;
}

Full Code:

#include <iostream>
using namespace std;

class Fraction {
private:
int numerator, denominator;
public:
Fraction operator+(const Fraction & frac2);
Fraction& setNumerator(int numer);
Fraction& setDenominator(int denom);
Fraction operator!();
void display();
};

Fraction Fraction::operator+(const Fraction & frac2)
{

this->numerator=this->numerator*frac2.denominator+frac2.numerator*this->denominator;
this->denominator=this->denominator*frac2.denominator;
return *this;
}
Fraction & Fraction::setNumerator(int numer)
{
  
   numerator = numer;
   return *this;
}
Fraction & Fraction::setDenominator(int denom)
{
   denominator = denom;
   return *this;
}
Fraction Fraction::operator!()
{
   int temp = this->numerator;
   this->numerator = this->denominator;
   this->denominator = temp;
   return *this;
}
void Fraction::display()
{
   cout<<"\n"<<numerator<<"/"<<denominator;
}

int main() {
  
   Fraction frac1;
   frac1.setNumerator(3);
   frac1.setDenominator(5);
  
   Fraction frac;
   frac.setNumerator(4);
   frac.setDenominator(7);
  
  
   Fraction * frac2 = new Fraction;
   frac2->setNumerator(7);
   frac2->setDenominator(8);
  
   frac1+frac;
   frac1.display();
  
   !frac1;
  
   frac1.display();
   return 0;
}

Output:

41/35
35/41

Do ask if any doubt. Please upvote.

Add a comment
Know the answer?
Add Answer to:
Consider this declaration of the class Fraction: class Fraction { private: int numerator, denominator; public: Fraction...
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
  • Refer to this header file: // Fraction class // This class represents a fraction a /...

    Refer to this header file: // Fraction class // This class represents a fraction a / b class Fraction { public: // Constructors Fraction(); // sets numerator to 1 and denominator to 1 Fraction(int num, int denom); // Setters void setNumerator(int num); void setDenominator(int denom); // getters int getNumerator()const {return num;} int getDenominator()const {return denom;} double getDecimal(){return static_cast<double> num / denom;} private: int num, denom; }; 1.Write the code for the non-member overloaded << operator that will display all of...

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

  • 13.21 Lab: Rational class This question has been asked here before, but every answer I have...

    13.21 Lab: Rational class This question has been asked here before, but every answer I have tested did not work, and I don't understand why, so I'm not able to understand how to do it correctly. I need to build the Rational.cpp file that will work with the main.cpp and Rational.h files as they are written. Rational Numbers It may come as a bit of a surprise when the C++ floating-point types (float, double), fail to capture a particular value...

  • I need help with the following Java code Consider a class Fraction of fractions. Each fraction...

    I need help with the following Java code Consider a class Fraction of fractions. Each fraction is signed and has a numerator and a denominator that are integers. Your class should be able to add, subtract, multiply, and divide two fractions. These methods should have a fraction as a parameter and should return the result of the operation as a fraction. The class should also be able to find the reciprocal of a fraction, compare two fractions, decide whether two...

  • Header file for the Rational class: #ifndef RATIONAL_H #define RATIONAL_H class Rational { public: Rational( int...

    Header file for the Rational class: #ifndef RATIONAL_H #define RATIONAL_H class Rational { public: Rational( int = 0, int = 1 ); // default constructor Rational addition( const Rational & ) const; // function addition Rational subtraction( const Rational & ) const; // function subtraction Rational multiplication( const Rational & ) const; // function multi. Rational division( const Rational & ) const; // function division void printRational () const; // print rational format void printRationalAsDouble() const; // print rational as...

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

  • If you have already answered this question, please do not repost your old solutions, you will be thumbs downed. I'm...

    If you have already answered this question, please do not repost your old solutions, you will be thumbs downed. I'm looking for NEW solutions only! Frac.h: // a Fraction object holds one Fraction number, one fraction #ifndef FRAC_H #define FRAC_H #include <iostream> using namespace std; //Creaing a Fraction class class Fraction { public: Fraction(int = 0, int = 1); // Function Declarations which performs operations on Fraction class Fraction add(const Fraction &); Fraction subtract(const Fraction& a); Fraction multiply(const Fraction& a);...

  • java code Write a class called FractionObject that represents a fraction with an integer numerator and...

    java code Write a class called FractionObject that represents a fraction with an integer numerator and denominator as fields. A Fraction Object object should have the following methods: A constructor with int numerator, int denominator as parameters - Constructs a new fraction object to represent the ratio (numerator/denominator). The denominator cannot be 0, so output a message if O is passed and set the numerator and denominator to 1. A constructor with no parameters - Constructs a new FractionObject to...

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