Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

image text in transcribed

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

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

Introductory Relational Database Design For Business With Microsoft Access

Authors: Jonathan Eckstein, Bonnie R. Schultz

1st Edition

1119329418, 978-1119329411

More Books

Students also viewed these Databases questions

Question

What is a job analysis?

Answered: 1 week ago