Question
code the inverse function

5. Here is the outline of a Rational number class, i.e., a class that contains fractional numbers represented by an integer n
0 0
Add a comment Improve this question Transcribed image text
Answer #1

/*C++ test program that test the inverse function of Rational class.The inverse function is tested in main function. */
//main.cpp
//include header files
#include<iostream>
using namespace std;
//Class Rational
class Rational
{
private:
   //declare private data variables
   int num;
   int denom;
public:
   /*default constructor*/
   Rational()
   {
       num=0;
       denom=1;
   }
   /*Parameter constructor with numerator and denominator*/
   Rational(int n, int d)
   {
       num=n;
       denom=d;
   }

   /*Reverse function that reverses
   the numerator into denominator and
   vice versa*/
   Rational inverse()
   {
       int temp=this->denom;
       this->denom =num;
       this->num=temp;
       return *this;
   }

   /*Overload the *= operator that returns the
   Rational of multiplication of two rational numbers*/
   Rational operator *= (Rational &ratioanl)
   {      
       this->num =this->num* ratioanl.num;
       this->denom = this->denom*ratioanl.denom;
       return *this;
   }
   //Helper method
   void display()
   {
       cout<<this->num<<"/"<<this->denom<<endl;
   }
};
//start of main funciton
int main()
{
   //create two Rational object,r
   Rational r(1,2);  
   cout<<"Prining Rational,r "<<endl;
   //call display
   r.display();
   //call inverse method
   Rational rev=r.inverse();
   cout<<"Inverse Rational"<<endl;
   //call display
   rev.display();
   //pause program output on console display
   system("pause");
   return 0;
}
------------------------------------------------------------------------------------------------------------------

Sample Output:

Prining Rational,r 1/2 Inverse Rational 2/1 Press any key to continue ...

Add a comment
Know the answer?
Add Answer to:
code the inverse function 5. Here is the outline of a Rational number class, i.e., a...
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
  • Rational Number *In Java* A rational number is one that can be expressed as the ratio...

    Rational Number *In Java* A rational number is one that can be expressed as the ratio of two integers, i.e., a number that can be expressed using a fraction whose numerator and denominator are integers. Examples of rational numbers are 1/2, 3/4 and 2/1. Rational numbers are thus no more than the fractions you've been familiar with since grade school. Rational numbers can be negated, inverted, added, subtracted, multiplied, and divided in the usual manner: The inverse, or reciprocal of...

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

  • Define a class for rational numbers. A rational number is a number that can be represented...

    Define a class for 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 would produce in a C++ program). Represent rational numbers as two values of type int, one for the numerator and one for the denominator. Call the class rational Num...

  • Rational will be our parent class that I included to this post, Implement a sub-class MixedRational....

    Rational will be our parent class that I included to this post, Implement a sub-class MixedRational. This class should Implement not limited to: 1) a Constructor with a mathematically proper whole, numerator and denominator values as parameters. 2) You will override the: toString, add, subtract, multiply, and divide methods. You may need to implement some additional methods, enabling utilization of methods from rational. I have included a MixedRational class with the method headers that I used to meet these expectations....

  • Define a class for rational numbers. A rational number is a "ratio-nal" number, composed of two...

    Define a class for rational numbers. A rational number is a "ratio-nal" number, composed of two integers with division indicated. Requirement: - two member variables: (int) numerator and (int) denominator. - two constructors: one that takes in both numerator and denominator to construct a rational number, and one that takes in only numerator and initialize the denominator as 1. - accessor/modifier - member functions: add(), sub(), mul(), div(), and less(). Usage: to add rational num b and rational num a,...

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

  • Make a new class called Rational to represent rational numbers in C++ language. Use a long...

    Make a new class called Rational to represent rational numbers in C++ language. Use a long to store each part (numerator and denominator). Be sure your main function fully test the class. 1. Create a helper function called gcd to calculate the greatest common divisor for the two given parameters. See Euclid’s algorithm (use google.com). Use this function to always store rationals in “lowest terms”. 2. Create a default, one argument and two argument constructor. For the two argument constructor,...

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

  • Construct a class named Fractions containing two integer data members named num and denom, used to...

    Construct a class named Fractions containing two integer data members named num and denom, used to store the numerator and denominator of a fraction having the form num/denom. Your class should include a default constructor that initializes num and denom to 1 if there's no user initialization, and it must prohibit a 0 denominator value. In addition, create member functions for displaying an object's data values and overloaded operator functions for adding, subtracting, multiplying, and dividing two Fraction objects, as...

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

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