Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

please help me solve this fast } ...... more member TUHCIIOHS ll 2 parameter constructor Rat(int i, intj){ n=i; d=j; } ll other Rat functions

please help me solve this fast

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed
} ...... more member TUHCIIOHS ll 2 parameter constructor Rat(int i, intj){ n=i; d=j; } ll other Rat functions as we saw in class The following demonstrates two ways that a member function could be called: 0 Explicitly on an object of the class using the "dot notation" . Implicitly as part of another member function then it called without the "clot" For example, consider the following member function: bool isZero(){ return n==0; ll returns true if n = = 0, false otherwise } 1. We can call it explicitly: Rat X(1,2), ; if ( x.isZero() ) II here isZero() is called explicitly on a Rat or we can call it implicitly as part of another member function or we can call it implicitly as part of another member function bool isNotZero(){ if ( isZero()) return true; else return false; } }; [lend Rat Here are the questions We would like to add a member function: bool operator==(Rat r) so that we can test whether or not two Rats are equal. 50 both of the following should return true, Rat X(1.3). y(1.3). 2(2.6) x==y; \"should return true x==z; // should return true You can assume that you have a member function void reduce() that will reduce a Rat to lowest terms. a. Write the operator ==() so that it works as described. (5 points) ANS: bool operator==(Rat r) b. In clear precise English, describe exactly how function reduce() could be implemented. Explain in detail why your method works. (5 points) c. Write member function reduce(). (10 points) ANS: void reduce(){ d. If we wanted to sort an array of Rats we could call the following function: void RatSort(Rat k, int size){ Rat t; for(int i=size-1; i>0; i--) for(int j=0; jk[j+ 1])0; i--) for(int j=0; j k[j+ 1]) t=k[j]; k[j]=k[j+1]; k[j+1]=t; }; which does a bubble sort on the array. Recall that since the compiler, out of the box, has no idea what Rats are, it doesn't know how to decide if one Rat is smaller than another. So, in order for this sort to work, we would have to have a member function which overloaded the > operator. It would be called operator>(..something goes here..). d1. Explain clearly exactly how this member function is implemented. (5 points)d1. Explain clearly exactly how this member function is implemented. (5 points) d2. Write this member function. Note: be careful of the return type. (10 points)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Financial management theory and practice

Authors: Eugene F. Brigham and Michael C. Ehrhardt

12th Edition

978-0030243998, 30243998, 324422695, 978-0324422696

Students also viewed these Programming questions