Question: Can someone help me write a rational.h, rational.cpp, and main.cpp file for this problem; I have to write it in c++ using only and the

Can someone help me write a rational.h, rational.cpp, and main.cpp file for this problem; I have to write it in c++ using only and the header funciton as libraries?

======================================================================================

Write a rational number class. Recall a rational number is 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 (default) constructor to make rational object without any argument. The constructor sets the rational object's numerator to 0, and denominator to 1.

2.one constructor with two int parameters. This constructor uses the two values to initialize the rational object's numerator and denominator respectively. If the denominator value is 0, the constructor should generate an assertion failure.

3.one constructor with one int parameter. The constructor should set 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 reads from standard input the value for the calling object. The input should be in the form of 2/3 or 27/51.

an output function that displays the calling object in the terminal. The output should also be in the form of 2/3, i.e., numberator/denominator.

One setter function that sets the numerator and denominator of the calling object.

Two getter functions that return the numerator and denominator respectively.

You should also implement a non-member function, Sum(), that adds two rational objects. The function that takes two rational objects as parameters, and returns the sum of these rational numbers. Note the following formular for adding two rational numbers:

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

You can make the above function a friend function of class rational.

Main() and testing requirement

1.Basic testing

The following code segment demonstrates 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

a.set (3,10); //set as numerator to 1, denominator to 10

a.output(); //display 3/10

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

2. Use rational class to calculate the following sum:

1/1 + 1/2 + 1/3 + 1/4 + ... + 1/n

First prompt the user to enter a positive integer, n.

Dynamically allocate an array rational objects with the above entered size

rational * fractions = new rational[n];

write a for loop that calls set function to set each of these objects to be 1/1, 1/2, 1/3, 1/4, ... ..

fractions[3].set (1,4);

use your Sum() function to calculate the sum (as a rational number) of all these n rational numbers, display the sum

free up the memory used by the array by deleting the dynamic array

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!