Hello, I am having trouble with this homework question for my c++ course.   Implement a Rational Number class with the following specifications. Data members a) numerator and denominator Functions a) Constructors: 1) default constructor 2) single parameter constructor to create numerator/1 3) dual parameter constructor to create numerator/denominator 4) Use constructor delegation across all constructors. b) Accessors and Mutators for both data members. c) Static recursive GCD function using Euclid's algorithm. d) Static LCM function for two numbers. e) Reduce function simplify a rational number. This function modifies its calling object. f) Your program should work with the supplied driver program.   Notes LCM (Least Common Multiple) This function returns the smallest multiple of a and b. Step 1: Multiply a and b to find a common multiple. Step 2: Divide the common multiple by the GCD of a and b. Step 3: Return the result of Step 2. Reduce: This function reduces a fraction to simplest terms (i.e. 9/12 to 3/4). Step 1: Find the GCD of the numerator and denominator. Step 2: Divide the numerator by GCD and store as the new numerator. Step 3: Divide the denominator by GCD and store as the new denominator.   Static Functions Recall that static functions are class functions and not associated with instances of the class (objects). In this class, the static functions GCD and LCM should accept inputs any input pair (a and b) and return an answer based upon that input pair. As such, these functions can be used by the programmer upon Rational Number objects or random values for a and b.   Use the following main function to test your program. (have to use this main function) int main() { cout << endl; // test constructors, accessors, mutators cout << "Default Constructor: "; RatNum r1; cout << r1.getNum() << "/" << r1.getDen() << endl; cout << "Single Parameter Constructor: "; RatNum r2(2); cout << r2.getNum() << "/" << r2.getDen() << endl; cout << "Dual Parameter Constructor: "; RatNum r3(1,3); cout << r3.getNum() << "/" << r3.getDen() << endl; cout << "Accessors / Mutators: "; r3.setNum(3); r3.setDen(12); cout << r3.getNum() << "/" << r3.getDen() << endl;   // test gcd cout << "\nGCD of the last fraction: " << RatNum::gcd(r3.getNum(),r3.getDen()) << endl; cout << "GCD of 40 and 24: " << RatNum::gcd(40,24) << endl;   // test lcm cout << "\nLCM of the last fraction: " << RatNum::lcm(r3.getNum(),r3.getDen()) << endl; cout << "LCM of 3 and 5: " << RatNum::lcm(3,5) << endl;   // test reduce cout << "\nReducing the last fraction: "; r3.reduce(); cout << r3.getNum() << "/" << r3.getDen() << endl; cout << endl; return 0; }   Output Example   Default Constructor: 0/1 Single Parameter Constructor: 2/1 Dual Parameter Constructor: 1/3 Accessors / Mutators: 3/12   GCD of the last fraction: 3 GCD of 40 and 24: 8   LCM of the last fraction: 12 LSM of 3 and 5: 15   Reducing the last fraction: 1/4

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question
100%

Hello, I am having trouble with this homework question for my c++ course.

 

Implement a Rational Number class with the following specifications.

Data members

a) numerator and denominator

Functions

a) Constructors:

1) default constructor

2) single parameter constructor to create numerator/1

3) dual parameter constructor to create numerator/denominator

4) Use constructor delegation across all constructors.

b) Accessors and Mutators for both data members.

c) Static recursive GCD function using Euclid's algorithm.

d) Static LCM function for two numbers.

e) Reduce function simplify a rational number. This function modifies its calling

object.

f) Your program should work with the supplied driver program.

 

Notes

LCM (Least Common Multiple)

This function returns the smallest multiple of a and b.

Step 1: Multiply a and b to find a common multiple.

Step 2: Divide the common multiple by the GCD of a and b.

Step 3: Return the result of Step 2.

Reduce:

This function reduces a fraction to simplest terms (i.e. 9/12 to 3/4).

Step 1: Find the GCD of the numerator and denominator.

Step 2: Divide the numerator by GCD and store as the new numerator.

Step 3: Divide the denominator by GCD and store as the new denominator.

 

Static Functions

Recall that static functions are class functions and not associated with instances of the class (objects). In this class, the static functions GCD and LCM should accept inputs any input pair (a and b) and return an answer based upon that input pair. As such, these functions can be used by the programmer upon Rational Number objects or random values for a and b.

 

Use the following main function to test your program. (have to use this main function)

int main() {

cout << endl;

// test constructors, accessors, mutators

cout << "Default Constructor: ";

RatNum r1;

cout << r1.getNum() << "/" << r1.getDen() << endl;

cout << "Single Parameter Constructor: ";

RatNum r2(2);

cout << r2.getNum() << "/" << r2.getDen() << endl;

cout << "Dual Parameter Constructor: ";

RatNum r3(1,3);

cout << r3.getNum() << "/" << r3.getDen() << endl;

cout << "Accessors / Mutators: ";

r3.setNum(3);

r3.setDen(12);

cout << r3.getNum() << "/" << r3.getDen() << endl;

 

// test gcd

cout << "\nGCD of the last fraction: "

<< RatNum::gcd(r3.getNum(),r3.getDen()) << endl;

cout << "GCD of 40 and 24: " << RatNum::gcd(40,24) << endl;

 

// test lcm

cout << "\nLCM of the last fraction: "

<< RatNum::lcm(r3.getNum(),r3.getDen()) << endl;

cout << "LCM of 3 and 5: " << RatNum::lcm(3,5) << endl;

 

// test reduce

cout << "\nReducing the last fraction: ";

r3.reduce();

cout << r3.getNum() << "/" << r3.getDen() << endl;

cout << endl;

return 0;

}

 

Output Example

 

Default Constructor: 0/1

Single Parameter Constructor: 2/1

Dual Parameter Constructor: 1/3

Accessors / Mutators: 3/12

 

GCD of the last fraction: 3

GCD of 40 and 24: 8

 

LCM of the last fraction: 12

LSM of 3 and 5: 15

 

Reducing the last fraction: 1/4

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY