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++ (10th 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)
- Write a class Teacher that contains the attribute teacher name, age and address. It alsocontains member function to input and display its attributes. Write another class Author Writeanother class Writer that contains the attributes writer name, address and number of books writtenby him. It also contains member functions to input and display its attributes. Write a third classScholar that inherits both Teacher and Writer classes. Note: Solve this using c++arrow_forwardc++ Implement a MyInt class. An object in the MyInt class must be able to hold a single dynamically allocated integer. MyInt should have the following members: A member variable ptr of type int *.A parameterized constructor that takes nums of type int as input parameters.A destructor.A superimposed assignment operator (). The copy must be deep.A function set that sets the value of the number.A function get that returns the value of the number.The members must be encapsulated in accordance with the convention. Memory leaks must be avoided. To test your class, create objects and call functions according to the comments below. All tests must be validated. Input Expected output5 11 27 5 11 271 2 3 1 2 319 21 30 19 21 30arrow_forwardJustify why it is desirable to have accessors to private types rather than making the types public for the following three reasons.arrow_forward
- Design 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_forwardGiven 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 membersof two private class A and B and printthe final result using friend functionswap(). Class A has private member xand class B has private member y. Useany member function to input thevalues of private membersarrow_forward
- Write 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_forwardCreate parent class Shapes with protected parameters width and height and function printarea() and a virtual function cal_area(). Create two sub classes Rectangle and Sqaure. In main() create 2 pointers of Shapes and assign Rectangle and Square to it. Call printarea function with the pointers. Note: Use C++ language with well explaination. After performing this program. Implement this program using an abstract class.arrow_forward
- Explain why accessors to private types are preferable than make the types public for the following three reasons.arrow_forwardWhich of the following best illustrates the concept of polymorphism? Why? That a) and b) have different answers. That e) and f) have different answers. That k) and l) have different answers.arrow_forwardA C++ assignment Implement the GradedActivity class. Copying from the pdfs is fine. Create a new class Assignment which is derived from GradedActivity. It should have three private member ints for 3 different parts of an assignment score: functionality (max 50 points), efficiency (max 25 points), and style (max 25 points). Create member function set() in Assignment which takes three parameter ints and sets the member variables. It should also set its score member, which is inherited from GradedActivity, using the setScore() function, to functionality + efficiency + style. Signature: void Assignment::set(int, int, int) Create a main program which instantiates an Assignment, asks the user for its functionality, efficiency, and style scores, and prints out the score and letter grade for the assignment. ___________________________________________________ Examples from the pdfs: d PassFailExam::set(int q, int m, int p) { double numericScore, pointsEach; numQuestions =…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