Question: Given the interface for rational class below: class rational { public: rational(int n = 0, int d = 1); // Constructor with three default parameters

Given the interface for rational class below:

class rational

{

public:

rational(int n = 0, int d = 1);

// Constructor with three default parameters. With them, the program does not need to supply the default constructor!!!

// Postcondition: if nothing is passed to the constructor, declared object is initialized to the default values

// otherwise, the declared object is initialized to values passed to n and d

rational add(const rational &r2) const;

// Version 1 of the add function

// Postcondition: sum of the calling object (*this) and r2 is returned

void add(const rational &r1, const rational &r2) const;

// Version 2 of the add function

// Postcondition: the sum of r1 and r2 is stored in the calling object *this

rational subtract(const rational &r2) const;

// Version 1 of the subtract function

// Postcondition: difference of the calling object (*this) - r2 is returned

void subtract(const rational &r1, const rational &r2) const;

// Version 2 of the add function

// Postcondition: the difference of r1 - r2 is stored in the calling object *this

rational multiply(const rational &r2) const;

// Postcondition: the product of the calling object (*this) * r2 is returned

rational divide(const rational &r2) const;

// Postcondition: the quotient of the calling object (*this) / r2 is returned

int compare(const rational &r2) const;

// Postcondition: returns 1, 0, or -1 if (*this), the calling object, is greater than, equal to, or less than r2, respectively.

private:

int num; // numerator

int denom; // denominator

};

You are asked to:

a) Implement all member functions

b) Write a main() function) to test all member functions.

Please do the program outside of the scope for example rational::

Given the interface for rational class below: class rational { public: rational(int

C: Windowslsy stem32\cmd.exe 1/4 2 is initialized by the 2nd constructor: r2 1/3 rl is initialized by the 2nd constructor: rl Testing the comapare() member function, found 1/4 is less than 1/3 Testing the four arithmetic member functions: ri + r2 1/4 + 1/3 7/12 Press any key to continue

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!