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

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

  • Create a fraction class. This will have two attributes, a numerator and a denominator, both int....

    Create a fraction class. This will have two attributes, a numerator and a denominator, both int. This class will have constructors accessors and mutators (for the attributes) a toString method which will allow us to print the fraction in the form 3/4 an Add method so we can add two fractions a subtract method (subtracts one fraction from the other) a multiply method (multiply two fractions) a divide method (divides one fraction by the other) DO NOT (for now) worry...

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

  • C++ Questions. Question 1 What would the function prototype in the class declaration for the following...

    C++ Questions. Question 1 What would the function prototype in the class declaration for the following member function look like? void Fractions::showData() { if((num < 0 && denom < 0) || denom < 0) { num = -num; denom = -denom; } cout << num << "/" << denom << endl; } Question 2 What would the function header of the following member function prototype in class Fractions be? Fractions operator+(const Fractions &f2); Question 3 What would the function prototype...

  • Consider a class Fraction of fractions. Each fraction is signed and has a numerator and a...

    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 fractions are equal, and convert a fraction to...

  • This in in C# There are two classes, class Fraction and class FractionDemo The user needs...

    This in in C# There are two classes, class Fraction and class FractionDemo The user needs to be able to input a value for the numerator and the denominator Create a Fraction class with private fields that hold a positive int numerator and a positive int denominator. In addition, create Properties for each field with the set mutator such that the numerator is greater than or equal to 0 and the denominator is greater than 0 (illegal values should be...

  • C++ I need help adding to this program. Here's my assignment: (1) Add the following functions...

    C++ I need help adding to this program. Here's my assignment: (1) Add the following functions to your working mixed number class: Overload the ==, <, and > comparison operators Overload the + and * arithmetic operators Overload the insertion (<<) operator: It should display whole part, space, numerator, forward slash, denominator: 5 4/13 Overload the extraction (>>) operator: It should read integer, integer, character, integer. That is, it can assume that the numerator, forward slash, and denominator are input...

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

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