Chapter 13
Programming Exercise #10 Page 983Additional
RequirementsUse a template so
the class works with any kind of number (i.e. int,
float, double)fractionType num1(5, 6);fractionType num1(5.1, 6.2);Tip: get
your class working with integer values first for numerator and denominator and
convert to a template after it is working with ints.When converting to a template you may need to move all of your
implementation into the .h header file. Submit source code, a screenshot with a time stamp of code execution, and a text file of the code. All code should include comments
pg._983.pdf
Unformatted Attachment Preview
Print Preview
1 of 11
https://ng.cengage.com/static/nbreader/ui/apps/nbreader/print_preview/pr…
Chapter 13: Overloading and Templates: 13-7c Programming Exercises
Book Title: C++ Programming: From Problem Analysis to Program Design
Printed By: Robert Woodley (woodley.robert39@gmail.com)
© 2018 Cengage Learning, Cengage Learning
Chapter Review
13-7c Programming Exercises
1. This chapter uses the class rectangleType to illustrate how to overload
the operators +, *, ==, !=, >>, and <<. In this exercise, first redefine the class rectangleType by declaring the instance variables as protected and then overload additional operators as defined in parts a to c. a. Overload the pre- and post-increment and decrement operators to increment and decrement, respectively, the length and width of a rectangle by one unit. (Note that after decrementing the length and width, they must be positive.) b. Overload the binary operator - to subtract the dimensions of one rectangle from the corresponding dimensions of another rectangle. If the resulting dimensions are not positive, output an appropriate message and do not perform the operation. c. The operators == and != are overloaded by considering the lengths and widths of rectangles. Redefine the functions to overload the relational operator by considering the areas of rectangles as follows: Two rectangles are the same, if they have the same area; otherwise, the rectangles are not the same. Similarly, rectangle yard1 is greater than rectangle yard2 if the area of yard1 is greater than the area of yard2. Overload the remaining relational operators using similar definitions. d. Write the definitions of the functions to overload the operators defined in parts a to c. e. Write a test program that tests various operations on the class rectangleType. 2. a. Redo Programming Exercise 1 by overloading the operators as nonmembers of the class rectangleType. 10/31/2019, 3:42 PM Print Preview 2 of 11 https://ng.cengage.com/static/nbreader/ui/apps/nbreader/print_preview/pr... b. Write a test program that tests various operations on the class rectangleType. 3. a. Chapter 11 defined the class boxType by extending the definition of the class rectangleType. In this exercise, derive the class boxType from the class rectangleType, defined in Exercise 1, add the functions to overload the operators +, -, *, ==, !=, <=, <, >=, >, and
pre- and post-increment and decrement operators as members of the
class boxType. Also overload the operators << and >>. Overload the
relational operators by considering the volume of the boxes. For
example, two boxes are the same if they have the same volume.
b. Write the definitions of the functions of the class boxType as defined
in part a.
c. Write a test program that tests various operations on the class
rectangleType.
4.
a. Redo Programming Exercise 3 by overloading the operators as
nonmembers of the class boxType.
b. Write a test program that tests various operations on the class
boxType.
5.
a. Extend the definition of the class clockType by overloading the postincrement operator function as a member of the class clockType.
b. Write the definition of the function to overload the post-increment
operator for the class clockType as defined in part a.
6.
a. The increment and relational operators in the class clockType are
overloaded as member functions. Rewrite the definition of the class
clockType so that these operators are overloaded as nonmember
functions. Also, overload the post-increment operator for the class
clockType as a nonmember.
b. Write the definitions of the member functions of the class clockType
as designed in part a.
c. Write a test program that tests various operations on the class as
designed in parts a and b.
7.
a. Extend the definition of the class complexType so that it performs the
10/31/2019, 3:42 PM
Print Preview
3 of 11
https://ng.cengage.com/static/nbreader/ui/apps/nbreader/print_preview/pr…
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:
If (c, d) is nonzero:
b. Write the definitions of the functions to overload the operators – and /
as defined in part a.
c. Write a test program that tests various operations on the class
complexType. Format your answer with two decimal places.
8.
a. Rewrite the definition of the class complexType so that the arithmetic
and relational operators are overloaded as nonmember functions.
b. Write the definitions of the member functions of the class
complexType as designed in part a.
c. Write a test program that tests various operations on the class
complexType as designed in parts a and b. Format your answer with
two decimal places.
9.
a. Extend the definition of the class newString as follows:
i. Overload the operators + and += to perform the string
concatenation operations.
ii. Add the function length to return the length of the string.
b. Write the definition of the function to implement the operations defined
in part a.
c. Write a test program to test various operations on the newString
objects.
10. Rational fractions are of the form a/b, in which a and b are integers and b≠0.
In this exercise, by “fractions” we mean rational fractions. Suppose a/b and c/d
are fractions. Arithmetic operations on fractions are defined by the following
rules:
10/31/2019, 3:42 PM
Print Preview
4 of 11
https://ng.cengage.com/static/nbreader/ui/apps/nbreader/print_preview/pr…
Fractions are compared as follows: a/b op c/d if ad op bc, in which op is any of
the relational operations. For example, a/b < c/d if ad < bc. Design a class— say, fractionType—that performs the arithmetic and relational operations on fractions. Overload the arithmetic and relational operators so that the appropriate symbols can be used to perform the operation. Also, overload the stream insertion and stream extraction operators for easy input and output. Write a C++ program that, using the class fractionType, performs operations on fractions. Among other things, test the following: Suppose x, y, and z are objects of type fractionType. If the input is 2/3, the statement should store 2/3 in x. The statement should output the value of x + y in fraction form. The statement should store the sum of x and y in z in fraction form. Your answer need not be in the lowest terms. 11. Recall that in C++, there is no check on an array index out of bounds. However, during program execution, an array index out of bounds can cause serious problems. Also, in C++, the array index starts at 0. Design and implement the class myArray that solves the array index out of bounds problem and also allows the user to begin the array index starting at any integer, positive or negative. Every object of type myArray is an array of type int. During execution, when accessing an array component, if the index is out of bounds, the program must terminate with an appropriate error message. Consider the following statements:x The statement in Line 1 declares list to be an array of five components, the component type is int, and the components are: list[0], list[1], ..., 10/31/2019, 3:42 PM Print Preview 5 of 11 https://ng.cengage.com/static/nbreader/ui/apps/nbreader/print_preview/pr... list[4]; the statement in Line 2 declares myList to be an array of 11 components, the component type is int, and the components are: myList[2], myList[3], ..., myList[12]; the statement in Line 3 declares yourList to be an array of 14 components, the component type is int, and the components are: yourList[-5], yourList[-4], ..., yourList[0], ..., yourList[8]. Write a program to test the class myArray. 12. Programming Exercise 11 processes only int arrays. Redesign the class myArray using class templates so that the class can be used in any application that requires arrays to process data. 13. Design a class to perform various matrix operations. A matrix is a set of numbers arranged in rows and columns. Therefore, every element of a matrix has a row position and a column position. If A is a matrix of five rows and six columns, we say that the matrix A is of the size 5 × 6 and sometimes denote it as . Clearly, a convenient place to store a matrix is in a two-dimensional array. Two matrices can be added and subtracted if they have the same size. Suppose and are two matrices of the size m × n, in which denotes the element of A in the ith row and the jth column, and so on. The sum and difference of A and B are given by: The multiplication of A and B (A ∗ B) is defined only if the number of columns of A is the same as the number of rows of B. If A is of the size m × n and B is of the size n × t, then is of the size m × t and the element is given by the formula: Design and implement a class matrixType that can store a matrix of any size. Overload the operators +, -, and * to perform the addition, subtraction, and multiplication operations, respectively, and overload the operator << to output a matrix. Also, write a test program to test various operations on the matrices. 14. a. In Programming Exercise 4 in Chapter 10, we defined a class romanType to implement Roman numbers in a program. In that exercise, we also implemented a function, romanToPositiveInteger, to convert a Roman number into its equivalent positive integer. Modify the definition of the class romanType so that the member 10/31/2019, 3:42 PM Print Preview 6 of 11 https://ng.cengage.com/static/nbreader/ui/apps/nbreader/print_preview/pr... variables are declared as protected. Use the class newString, as designed in Programming Exercise 9, to manipulate strings. Furthermore, overload the stream insertion and stream extraction operators for easy input and output. The stream insertion operator outputs the Roman number in the Roman format. Also, include a member function, positiveIntegerToRoman, that converts a positive integer to an equivalent Roman number format. Write the definition of the member function positiveIntegerToRoman. For simplicity, we assume that only the letter I can appear in front of another letter and that it appears only in front of the letters V and X. For example, 4 is represented as IV, 9 is represented as IX, 39 is represented as XXXIX, and 49 is represented as XXXXIX. Also, 40 will be represented as XXXX, 190 will be represented as CLXXXX, and so on. b. Derive a class extRomanType from the class romanType to do the following: In the class extRomanType, overload the arithmetic operators +, -, *, and / so that arithmetic operations can be performed on Roman numbers. Also, overload the pre- and post-increment and decrement operators as member functions of the class extRomanType. To add (subtract, multiply, or divide) Roman numbers, add (subtract, multiply, or divide, respectively) their positive integer representations and then convert the result to the Roman number format. For subtraction, if the first number is smaller than the second number, output a message saying that, “Because the first number is smaller than the second, the numbers cannot be subtracted”. Similarly, for division, the numerator must be larger than the denominator. Use similar conventions for the increment and decrement operators. Write the definitions of the functions to overload the operators described in part b. Write a program to test your class. 15. In Example 13-10, the class template listType is designed to implement a list in a program. For illustration purposes, that example included only the sorting operation. Extend the definition of the class template to include the remove and search operations. Write the definitions of the member functions 10/31/2019, 3:42 PM Print Preview 7 of 11 https://ng.cengage.com/static/nbreader/ui/apps/nbreader/print_preview/pr... to implement the class template listType. Also, write a test program to test various operations on a list. 16. Consider the class dateType given in Chapter 11. In this class, add the functions to overload the increment and decrement operators to increase the date by a day and decrease the date by a day, respectively; relational operators to compare two dates; and stream operators for easy input and output. (Assume that the date is input and output in the form MM-DD-YYYY.) Also write a program to test your class. 17. Write a program that uses C++11 random number generators to generate 25 real numbers between 10 and 100. 18. Programming Exercise 15, Chapter 11, describes how to design the class pointType to implement a point. Redo this programming exercise so that the class pointType: a. Overloads the stream insertion operator, <<, for easy output. b. Overloads the stream extraction operator, >>, for easy input. (A point is
input as (a, b).)
c. Overloads the assignment operator to copy a point into another point.
d. Overloads the binary operator +, as a member function, so that it returns
the distance between two points.
e. Overloads the operator ==, as a member function, so that it returns
true if two points are the same; false otherwise.
f. Overloads the operator !=, as a member function, so that it returns
true if two points are not the same; false otherwise.
g. Write a program to test your class.
19. Programming Exercise 18, Chapter 10, describes how to design the class
lineType to implement a line. Redo this programming exercise so that the
class lineType:
a. Overloads the stream insertion operator, <<, for easy output. b. Overloads the stream extraction operator, >>, for easy input. (The line
is input as (a, b, c).)
10/31/2019, 3:42 PM
Print Preview
8 of 11
https://ng.cengage.com/static/nbreader/ui/apps/nbreader/print_preview/pr…
c. Overloads the assignment operator to copy a line into another line.
d. Overloads the unary operator +, as a member function, so that it returns
true if a line is vertical; false otherwise.
e. Overloads the unary operator -, as a member function, so that it returns
true if a line is horizontal; false otherwise.
f. Overloads the operator ==, as a member function, so that it returns
true if two lines are equal; false otherwise.
g. Overloads the operator ||, as a member function, so that it returns
true if two lines are parallel; false otherwise.
h. Overloads the operator &&, as a member function, so that it returns
true if two lines are perpendicular; false otherwise.
Write a program to test your class.
20. Consider the classes class cashRegister and dispenserType given in
the Programming Example “Juice Machine” in Chapter 10.
a. In the class cashRegister, add the functions to overload the binary
operators + and – to add and subtract an amount in a cash register; the
relational operators to compare the amount in two cash registers; and
the stream insertion operator for easy output.
b. The class dispenserType, in the Programming Example “Juice
Machine” in Chapter 10, is designed to implement a dispenser to hold
and release products. In this class, add the functions to overload the
increment and decrement operators to increment and decrement the
number of items by one, respectively, and the stream insertion operator
for easy output.
c. Write a program to test the classes designed in parts a and b.
21. (Stock Market) Write a program to help a local stock trading company
automate its systems. The company invests only in the stock market. At the
end of each trading day, the company would like to generate and post the
listing of its stocks so that investors can see how their holdings performed that
day. We assume that the company invests in, say, 10 different stocks. The
desired output is to produce two listings, one sorted by stock symbol and
another sorted by percent gain from highest to lowest.
10/31/2019, 3:42 PM
Print Preview
9 of 11
https://ng.cengage.com/static/nbreader/ui/apps/nbreader/print_preview/pr…
The input data is provided in a file in the following format:
For example, the sample data is:
The first line indicates that the stock symbol is MSMT, today’s opening price
was 112.50, the closing price was 115.75, today’s high price was 116.50,
today’s low price was 111.75, yesterday’s closing price was 113.50, and the
number of shares currently being held is 6723823.
The listing sorted by stock symbols must be of the following form:
Develop this programming exercise in two steps. In the first step (part a),
design and implement a stock object. In the second step (part b), design and
implement an object to maintain a list of stocks.
a. (Stock Object) Design and implement the stock object. Call the class
that captures the various characteristics of a stock object stockType.
The main components of a stock are the stock symbol, stock price, and
number of shares. Moreover, we need to output the opening price,
closing price, high price, low price, previous price, and the percent
gain/loss for the day. These are also all the characteristics of a stock.
Therefore, the stock object should store all this information.
Perform the following operations on each stock object:
i. Set the stock information.
10/31/2019, 3:42 PM
Print Preview
10 of 11
https://ng.cengage.com/static/nbreader/ui/apps/nbreader/print_preview/pr…
ii. Print the stock information.
iii. Show the different prices.
iv. Calculate and print the percent gain/loss.
v. Show the number of shares.
a.1. The natural ordering of the stock list is by
stock symbol. Overload the relational
operators to compare two stock objects by
their symbols.
a.2. Overload the insertion operator, <<, for easy output. a.3. Because the data is stored in a file, overload the stream extraction operator, >>, for easy
input.
For example, suppose infile is an ifstream object and the input file
was opened using the object infile. Further suppose that myStock is
a stock object. Then, the statement
reads the data from the input file and stores it in the object myStock.
(Note that this statement reads and stores the data in the relevant
components of myStock.)
b. Now that you have designed and implemented the class stockType
to implement a stock object in a program, it is time to create a list of
stock objects.
Let us call the class to implement a list of stock objects
stockListType.
The class stockListType must be derived from the class
listType, which you designed and implemented in the previous
exercise. However, the class stockListType is a very specific class,
designed to create a list of stock objects. Therefore, the class
stockListType is no longer a template.
10/31/2019, 3:42 PM
Print Preview
11 of 11
https://ng.cengage.com/static/nbreader/ui/apps/nbreader/print_preview/pr…
Add and/or overwrite the operations of the class listType to
implement the necessary operations on a stock list.
The following statement derives the class stockListType from the
class listType.
The member variables to hold the list elements, the length of the list,
and the maximum size of the list were declared as protected in
the class listType. Therefore, these members can be directly
accessed in the class stockListType.
Because the company also requires you to produce the list ordered by
the percent gain/loss, you need to sort the stock list by this component.
However, you are not to physically sort the list by the component
percent gain/loss. Instead, you will provide a logical ordering with
respect to this component.
To do so, add a member variable, an array, to hold the indices of the
stock list ordered by the component percent gain/loss. Call this array
sortIndicesGainLoss. When printing the list ordered by the
component percent gain/loss, use the array sortIndicesGainLoss to
print the list. The elements of the array sortIndicesGainLoss will tell
which component of the stock list to print next.
c. Write a program that uses these two classes to automate the company’s
analysis of stock data.
Chapter 13: Overloading and Templates: 13-7c Programming Exercises
Book Title: C++ Programming: From …
Purchase answer to see full
attachment

We can help you complete this assignment or another one similar to this. Just hit "Order Now" to get started!

error: Content is protected !!