This lab practices define class, more particular, identifying the member variables, declaring and defining constructors, and other member functions. Requirement Write a rational number class. Recall a rational number is a rational number, composed of two integers with division indicated. The division is not carried out, it is only indicated, as in 1/2, 2/3, 15/32. You should represent rational numbers using two int values, numerator and denominator. A principle of abstract data type construction is that constructors must be present to create objects with any legal values. You should provide the following two constructors: one constructor to make rational objects without any argument. The constructor sets the rational object's numerator to 0, and denominator to 1.

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

This lab practices define class, more particular, identifying the member variables, declaring and defining constructors, and other member functions.

Requirement

Write a rational number class. Recall a rational number is a rational number, composed of two integers with division indicated. The division is not carried out, it is only indicated, as in 1/2, 2/3, 15/32. You should represent rational numbers using two int values, numerator and denominator.

A principle of abstract data type construction is that constructors must be present to create objects with any legal values. You should provide the following two constructors:

  1. one constructor to make rational objects without any argument. The constructor sets the rational object's numerator to 0, and denominator to 1.
  2. one constructor to make rational objects out of pairs of int values, i.e., a constructor with two intparameters. The constructor uses the two values to initialize the rational object's numerator and denominator respectively.
  3. one constructor to make rational objects out of a single int values, as in 2/1, 17/1, i.e., a constructor with only one int parameter. The constructor sets the rational’ s numerator to the given parameter and sets the denominator to 1.

You should also provide the following member functions:

  1. an input function that reads from standard input the value for current object. The input should be in the form of 2/3 or 27/51.
  2. an output function that displays the current object in the terminal. The output should also be in the form of 2/3, i.e., numerator/denominator.
  3. Two getter functions that return the numerator and denominator respectively.
  4. a Sum function that takes two rational objects as parameters. It sets the current rational object to be the sum of the two given rational numbers.

Note the following formula for adding two rational numbers:

a/b + c/d = ( a*d + b*c)/(b*d)

 

The following code segment demonstrate the usage of the above mentioned member functions, and constructors. Your main function should be some code like this.

int main()
{
 // ToDo: declare three rational objects using the default constructor
    char answer;

// Main loop to read in rationals and compute the sum
    do {
cout << "\nEnter op1 (in the format of p/q):";
// ToDo: use your input member function to read the first rational        cout << "\nEnter op2 (in the format of p/q):";
// ToDo: use your input member function to read the second rational

// ToDo: use the third rational to call Sum with first and second as parameters

 cout << "\nThe sum of op1 and op2 is:";
// ToDo: ouptput the third rational


cout << endl;
cout << "\nTry again (Y/N)?";
cin >> answer;
} while (answer == 'y' || answer == 'Y');


// ToDo: test getters
cout << "\nC's numerator is: " ;
cout << "\nC's denominator is: ";

return 0;
}

Hints

Please follow the order suggested below when working on this lab, always maintaining a compilable version of the code. Take an incremental approach. Write and test one function at a time!

  1. Define the class rational first, i.e., list the member variables, and declare the member functions in the class, including the constructors. Please refer to notes and textbook for the special syntax for declaring constructors.
  2. Implement the three constructors, test it in main, by declaring rational objects with no parameter, one paramter, and two paramters respectively.
  3. implement output function, and test it in main. This will also help you verify/test the three constructors.
  4. Implement input function, and test it in main.
  5. Implement Sum function, and test in main.
  6. Implement isEqual function, and test in main.

 

// Add appropriate headers
 
/*  KEEP THIS COMMENT
* class Rational
*/
class rational
{
// ToDo: Constructor that takes int numerator and int denominator
 
// ToDo: Constructor that takes int numerator
 
// ToDo: Default Constructor
 
// ToDo: Member function to read a rational in the form: n/d
 
// ToDo: Member function to write a rational as n/d
 
// ToDo: declare an accessor function to get the numerator
 
// ToDo: declare an accessor function to get the denominator
 
// ToDo: delcare a function called Sum that takes two rational objects
// sets the current object to the sum of the given objects using the
// formula: a/b + c/d = ( a*d + b*c)/(b*d)
 
 
};
 
 
int main()
{
// ToDo: declare three rational objects using the default constructor
char answer;
 
// Main loop to read in rationals and compute the sum
do {
cout << "\nEnter op1 (in the format of p/q):";
// ToDo: use your input member function to read the first rational
 
cout << "\nEnter op2 (in the format of p/q):";
// ToDo: use your input member function to read the second rational
 
 
// ToDo: use the third rational to call Sum with first and second as parameters
 
 
cout << "\nThe sum of op1 and op2 is:";
// ToDo: ouptput the third rational
 
cout << endl;
 
cout << "\nTry again (Y/N)?";
cin >> answer;
 
} while (answer == 'y' || answer == 'Y');
 
// ToDo: test getters 
cout << "\nC's numerator is: " ;
cout << "\nC's denominator is: ";
 
return 0;
}
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 5 steps with 3 images

Blurred answer
Similar questions
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