Guest User

Untitled

a guest
Apr 22nd, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.00 KB | None | 0 0
  1. Write a class definition for a fraction class. That is, define your own class that will represent a fraction in your program. Each variable of type fraction represents a single fraction. That means that the class has two member variables that should be private: the numerator and the denominator of the fraction. The class also must have the following public member functions:
  2.  
  3. fraction(); // constructor that defines a default fraction of 0/1
  4. fraction(int n, int d); // constructor that defines a fraction n/d
  5. fraction plus(fraction second); // adds second to the member fraction and returns the result in a new fraction
  6. fraction minus(fraction second); // subtracts second from the member fraction and returns the result in a new fraction
  7. fraction times(fraction second); // multiplies second and the member fraction and returns the result in a new fraction
  8. fraction divides(fraction second); // divides the member fraction by second and returns the result in a new fraction
  9. double toDecimal(); // returns the decimal value of the member fraction
  10. void input(); // read a fraction of the form x/y from the user
  11. void output(); // print the fraction in the form x/y to the screen
  12.  
  13. Finally, the class should define one private member function:
  14.  
  15. void reduce(); // reduce the member fraction to simplest terms, which should be done automatically any time that the value of the member fraction changes
  16.  
  17. Note that the reduce() function should use Euclid’s method (as we did in Lab 7) to find the greatest common divisor of the numerator and denominator. You may do that directly in the reduce() function, or you may add another private member function to the class just to calculate the GCD.
  18.  
  19. You will also have to write a driver program to test your fraction class. Be sure that you test all the member functions for correctness in the driver. That is, your driver should allow you (and the instructor!) to verify that each of the member functions in your class works correctly.
Add Comment
Please, Sign In to add comment