Problem Solving with C++ - MyProgrammingLab
Problem Solving with C++ - MyProgrammingLab
10th Edition
ISBN: 9780134522418
Author: SAVITCH
Publisher: PEARSON
bartleby

Videos

Textbook Question
Book Icon
Chapter 10, Problem 7PP

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.

Blurred answer
Students have asked these similar questions
In three sentences, explain why it's preferable to have accessors for private types rather than public types.
Write a java code to implement four new functions DFA Union: Given Two DFAs, M1 and M2, create a DFA which accepts the union of the languages of M1 and M2. Intersection: Given Two DFAs, M1 and M2, create a DFA which accepts the intersection of the languages of M1 and M2. Difference: Given Two DFAs, M1 and M2, create a DFA which accepts the set difference of the languages of M1 and M2, that is, L(M1) - L(M2), the set of strings accepted by M1 but not M2. Complement: Given DFA M, create a DFA which accepts the complement of the language of M.
a. Extend the definition of the class complexType so that it performs the subtraction and division operations. Overload the operators subtraction and division for this class as member functions. If (a, b) and (c, d) are complex numbers, (a, b) - (c, d) = (a - c, b - d), If (c, d) is nonzero, (a, b)/(c, d) = ((ac + bd)/(c ^2+ d ^2 ), (-ad + bc)/(c ^2 + d^ 2 )) b. Write the definitions of the functions to overload the operators – and / as defined in part a. c. Write a test program that tests the various operations on the class complexType. Format your answer with two decimal places

Chapter 10 Solutions

Problem Solving with C++ - MyProgrammingLab

Ch. 10.2 - Below we have redefined the class DayOfYear from...Ch. 10.2 - Given the following class definition, write an...Ch. 10.2 - Prob. 13STECh. 10.2 - The private member function DayOfYear::checkDate...Ch. 10.2 - Suppose your program contains the following class...Ch. 10.2 - Suppose you change Self-Test Exercise 15 so that...Ch. 10.2 - Explain what public: and private: do in a class...Ch. 10.2 - a. How many public: sections are required in a...Ch. 10.2 - Give a definition for the function with the...Ch. 10.2 - Give a definition for the function with the...Ch. 10.2 - Give a definition for the function with the...Ch. 10.2 - Suppose your program contains the following class...Ch. 10.2 - How would you change the definition of the class...Ch. 10.2 - Prob. 24STECh. 10.3 - When you define an ADT as a C++ class, should you...Ch. 10.3 - When you define an ADT as a C++ class, what items...Ch. 10.3 - Suppose your friend defines an ADT as a C++ class...Ch. 10.3 - Redo the three- and two-parameter constructors in...Ch. 10.4 - How does inheritance support code reuse and make...Ch. 10.4 - Can a derived class directly access by name a...Ch. 10.4 - Suppose the class SportsCar is a derived class of...Ch. 10 - Solution to Practice Program 10.1 Redefine...Ch. 10 - Redo your definition of the class CDAccount from...Ch. 10 - Define a class for a type called CounterType. An...Ch. 10 - Write a grading program for a class with the...Ch. 10 - Redo Programming Project 1 (or do it for the first...Ch. 10 - Define a class called Month that is an abstract...Ch. 10 - Redefine the implementation of the class Month...Ch. 10 - My mother always took a little red counter to the...Ch. 10 - Write a rational number class. This problem will...Ch. 10 - Define a class called Odometer that will be used...Ch. 10 - Redo Programming Project 7 from Chapter 5 (or do...Ch. 10 - The U.S. Postal Service printed a bar code on...Ch. 10 - Consider a class Movie that contains information...
Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning
6 Stages of UI Design; Author: DesignerUp;https://www.youtube.com/watch?v=_6Tl2_eM0DE;License: Standard Youtube License