Computers and Technology

C++ DESCRIPTION
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 ratio-nal 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.
one constructor to make rational objects out of pairs of int values, i. e., a constructor with two int parameters. The constructor uses the two values to initialize the rational object's numerator and denominator respectively.
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:
an input() function that takes no parameters but reads from standard input the value for current object. The input should be in the form of 2/3 or 27/51.
an output() function that takes no parameters but displays the current object in the terminal. The output should also be in the form of 2/3, i. e., numerator/denominator.
Two getter functions that return the numerator and denominator respectively.
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 on rational that has the sum above.
cout << "\nC's numerator is: " ;
cout << "\nC's denominator is: ";
// TODO: Use two constructors to declare a whole number 3/1 and a 4/5
// TODO: Use output to print both rationals
return 0;
}

answer
Answers: 1

Other questions on the subject: Computers and Technology

image
Computers and Technology, 22.06.2019 06:00, kiaunarayne808
The width of a piece of rectangular land is 5m shorter rhan 1/3 of its length .find the width of the land if the length is 60m,150m.
Answers: 1
image
Computers and Technology, 22.06.2019 23:30, keviongardner
The next button in the review section shows the next available comment. next slide with no comment. previous comment. edited comment.
Answers: 1
image
Computers and Technology, 23.06.2019 18:00, bubbles173883
While inserting images, the picture command is usually used to insert photos from a digital camera, and the clip art command is usually used to a. edit the sizes and other characteristics of photos that have been inserted. b. take a screenshot of an image and copy it to the clipboard for pasting. c. search for drawings or other images from a library of prepared pictures. d. make illustrations using lines and shapes that are easy to manipulate.
Answers: 1
image
Computers and Technology, 24.06.2019 15:30, jhony70
What type of forensic evidence was recovered during the bomb set off at the new mexico facility on the video that was similar to the evidence obtained at the boston bombings and how did the evidence allow the researchers to connect other pieces of evidence to the same bomb?
Answers: 2
Do you know the correct answer?
C++ DESCRIPTION
This lab practices define class, more particular, identifying the member var...

Questions in other subjects:

Konu
Mathematics, 14.09.2022 02:10
Konu
Mathematics, 14.09.2022 02:30