Write a rational number class. This problem will be revisited in Chapter 11, where operator overloading will make the problem much easier. For now we will use member functions add, sub, mul, div, and less that each carry out the operations +, -, *, /, and <. For example, a + b will be written a.add(b), and a < b will be written a.less(b).
Define a class for rational numbers. 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, 65/4, 16/5. You should represent rational numbers by 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 constructors to make objects out of pairs of int values; this is a constructor with two int parameters. Since every int is also a rational number, as in 2/1 or 17/1, you should provide a constructor with a single int parameter. Provide member functions input and output that take an istream and ostream argument, respectively, and fetch or write rational numbers in the form 2/3 or 37/51 to or from the keyboard (and to or from a file). Provide member functions add, sub, mul, and div that return a rational value. Provide a function less that returns a bool value. These functions should do the operation suggested by the name. Provide a member function neg that has no parameters and returns the negative of the calling object.
Provide a main function that thoroughly tests your class implementation.
The following formulas will be useful in defining functions.
a/b + c/d = (a * d + b * c) / (b * d) a/b − c/d = (a * d − b * c) / (b * d) (a/b) * (c/d) = (a * c) / (b * d) (a/b) / (c/d) = (a * d) / (c * b) −(a/b) = (−a/b) (a/b) < (c/d) means (a * d) < (c * b) (a/b) == (c/d) means (a * d) == (c * b) |
Let any sign be carried by the numerator; keep the denominator positive.
Trending nowThis is a popular solution!
Chapter 10 Solutions
Problem Solving with C++ (9th Edition)
Additional Engineering Textbook Solutions
Modern Database Management
Database Concepts (8th Edition)
Introduction To Programming Using Visual Basic (11th Edition)
Computer Science: An Overview (12th Edition)
Starting Out with C++: Early Objects (9th Edition)
Computer Science: An Overview (13th Edition) (What's New in Computer Science)
- C++ program Implement a class with the required data fields, constructor, and setter/getter for each of the private data members, and a print() member function. In your program, the class must be fully tested. Implement a class Book. A book has a title, author, year of publication, and ISBN number. Implement a member function toBibtex() that returns a BibTeX string that describes the book. Also please if you are able give me documentation of your work because i need it too.arrow_forwardCreate a class Test with twoprivate integer data members: xand y and one memberfunction: getdata() for takinginput of x and y. Create twonon-member friend functions:task1() and task2() to class Test,such as task1() is displayingsquares of integers from 1 to xand task2() is finding the sum ofall integers from 1 to y. [in c++]arrow_forwardWrite a simple function template for predicate function isEqualTo that compares its two arguments of the same type with the equality operator (==) and returns true if they are equal and false otherwise. Use this function template in a program that calls isEqualTo only with a variety of fundamental types. Now write a separate version of the program that calls isEqualTo with a user-defined class type, but does not overload the equality operator. What happens when you attempt to run this program? Now overload the equality operator (with the operator function) operator==. Now what happens when you attempt to run this program?arrow_forward
- OCAML programming (Sliding tiles) The mechanics described in this exercise can be used to implement a game sliding tiles. 1. Create a type slidingTile, consisting of a char matrix and two integers xand y. The integers x and y are the coordinates of an empty tile. 2. Create a function val slide : slidingTile -> int -> int -> slidingTile = <fun> that given a slidingTile and two integers, that represent a location in thematrix, it slides the tile from the specified location to the empty tile, thenreturns the altered slidingTile. If provided location is not adjacent to theempty tile, or out of bounds, then return the slidingTile unaltered. 3. Create a function val print_tile : slidingTile -> unit = <fun> that prints a slidingTile on screen with the corresponding characters fromthe matrix. However, print an empty space where the empty tile is instead.arrow_forwardJustify why it is desirable to have accessors to private types rather than making the types public for the following three reasons.arrow_forwardDesign and code a class in C++ that represents a single month of the year. It should have a single integer-typed data member to represent the month. Make sure to provide proper construction, access, and mutation for this member as well as input and output methods. In addition, methods for comparing two months chronologically for ordering would be useful as would a method to advance to the next month (note that January follows December and begins the cycle anew) or previous month. In fact, thinking about it, it might be good to be able to advance to a month an arbitrary number of whole months in the future or past: January '+' 5 = June; March '-' 5 = October. Place your Month class in a library. Write a test application (driver) for your class. Oh, and the programmers who commissioned this class wanted to make sure it could handle not only integer-valued representation of a Month but also either whole-word names or three-letter abbreviations for the Months. That means that they should…arrow_forward
- Given the Class Definition for ClockType discussed extensively in class, write what would have to be added to the FUNCTION DEFINITION for the Class ClockType to overload the “= =”, i.e., the comparison “equal-equal sign,” here: NOTE: In other words, WHAT WOULD BE ADDED TO THE CLASS DEFINITION ONLY? Note: ThePrivate Members are: int hr; // contains the hours int min; // contains the minutes int sec; // contains the secondsarrow_forwardWrite a program to swap members of two private class A and B and print final result using friend function swap(). Class A has private member x and class B has private member function y.Use any member to input the values of private memebersarrow_forwardGiven these three arguments, please explain why it is better to have accessors for private types rather than making them public.arrow_forward
- Write a C++ code for the following question as quickly as you can. But please do the same as asked in the Question...arrow_forwardThis is in c++. Create a class of function objects called StartsWith that satisfies the following specification: when initialized with character c, an object of this class behaves as a unary predicate that determines if its string argument starts with c. For example, StartsWith(’a’) is a function object that can be used as a unary predicate to determine if a string starts with an a. So, StartsWith(’a’)("alice") would return true but StartsWith(’a’)("bob") would return false. The function objects should return false when called on an empty string. Test your function objects by using one of them together with the STL algorithm count_if to count the number of strings that start with some letter in some vector of strings. So basically, I want a class that returns true or false based on if the name that is typed starts with the character that I put down.arrow_forwardIn C++, can I get a code example for a function that will return the intersection items for two sets. It will return/print out the shared (intersection) items for them. I am looking for an actual function preferably for a set class, comparing one instance of a set class with another, but definitely NOT a STL keyword. Thank you.arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education