Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This lab practices define class, more particular, identifying the member variables, declaring and defining constructors, and other member functions. Background reading: Please review textbook for

This lab practices define class, more particular, identifying the member variables, declaring and defining constructors, and other member functions. Background reading: Please review textbook for the basics of class, and the idea of Abstract Data Type. 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 three 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 int parameters. 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.

Rational a(1,2); //a is 1/2, first constructor is called to initialize a a.output (); // display a in standard output, as 1/2

Rational b(2); //b is 2/1, the second constructor is called to initialize b b.output (); // display b in standard output, as 2/1

Rational c; //the default constructor is called to initialize c to 0/1 c.output (); //You can see how the default constructor initializes the member variables...

c.input(); //read value for c from input. If the user enters 3/4, then // c's numerator is set to 3, and its denominator is set to 4 c.output(); //Display c's value

//Now testing the sum function c.sum (a,b); // c will be set to 1/2+2/1 = 5/2, see formular given above c.output (); // it should display 5/2 Hints Please follow the order suggested below when working on this lab, always maintaining a compliable 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 slides 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 parameter, and two parameters 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

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

Bioinformatics Databases And Systems

Authors: Stanley I. Letovsky

1st Edition

1475784058, 978-1475784053

More Books

Students also viewed these Databases questions

Question

Analog signals are transmitted over conducted media. True or false?

Answered: 1 week ago

Question

3. How would this philosophy fit in your organization?

Answered: 1 week ago

Question

3. What information do participants need?

Answered: 1 week ago