10.6 (Memory Allocation and Deallocation Operators) Compare and contrast dynamic memory allocation and deallocation operators new, new [], delete and delete[].
10.7 (Overloading the Parentheses Operator) One nice example of overloading the function call operator () is to allow another form of double-array subscripting popular in some programming languages. Instead of saying
chessBoard[row][column]
for an array of objects, overload the function call operator to allow the alternate form
chessBoard(row, column)
Create a class DoubleSubscriptedArray that has similar features to class Array in Figs. 10.10–10.11. At construction time, the class should be able to create a DoubleSubscriptedArray of any number of rows and columns. The class should supply operator() to perform double-subscripting operations. For example, in a 3-by-5 DoubleSubscriptedArray called chessBoard, the user could write chessBoard(1, 3) to access the element at row 1 and column 3. Remember that operator() can receive any number of arguments. The underlying representation of the DoubleSubscriptedArray could be a single-subscripted array of integers with rows * columns number of elements. Function operator() should perform the proper pointer arithmetic to access each element of the underlying array. There should be two versions of operator()—one that returns int& (so that an element of a DoubleSubscriptedArray can be used as an lvalue) and one that returns int. The class should also provide the following operators: ==, !=, =, << (for outputting the DoubleSubscriptedArray in row and column format) and >> (for inputting the entire DoubleSubscriptedArray contents).
10.8 (Complex Class) Consider class Complex shown in Figs. 10.14–10.16. The class enables operations on so-called complex numbers. These are numbers of the form realPart + imaginaryPart *i, where i has the value
−1−−−√
Modify the class to enable input and output of complex numbers via overloaded >> and << operators, respectively (you should remove the toString function from the class).
Overload the multiplication operator to enable multiplication of two complex numbers as in algebra.
Overload the == and != operators to allow comparisons of complex numbers.
After doing this exercise, you might want to read about the Standard Library’s complex class (from header <complex>).
10.9 (HugeInteger Class) A machine with 32-bit integers can represent integers in the range of approximately –2 billion to +2 billion. This fixed-size restriction is rarely troublesome, but there are applications in which we would like to be able to use a much wider range of integers. This is what C++ was built to do, namely, create powerful new data types. Consider class HugeInteger of Figs. 10.17–10.19, which is similar to the HugeInteger class in Exercise 9.14. Study the class carefully, then respond to the following:
Describe precisely how it operates.
What restrictions does the class have?
Overload the * multiplication operator.
Overload the / division operator.
Overload all the relational and equality operators.
[Note: We do not show an assignment operator or copy constructor for class HugeInteger, because the assignment operator and copy constructor provided by the compiler are capable of copying the entire array data member properly.]
10.10 (RationalNumber Class) Create a RationalNumber(fractions) class like the one in Exercise 9.6. Provide the following capabilities:
Create a constructor that prevents a 0 denominator in a fraction, reduces or simplifies fractions that are not in reduced form and avoids negative denominators.
Overload the addition, subtraction, multiplication and division operators for this class.
Overload the relational and equality operators for this class.
10.11 (Polynomial Class) Develop class Polynomial. The internal representation of a Polynomial is an array of terms. Each term contains a coefficient and an exponent—e.g., the term
2x4
has the coefficient 2 and the exponent 4. Develop a complete class containing proper constructor and destructor functions as well as set and get functions. The class should also provide the following overloaded operator capabilities:
Overload the addition operator (+) to add two Polynomials.
Overload the subtraction operator (-) to subtract two Polynomials.
Overload the assignment operator to assign one Polynomial to another.
Overload the multiplication operator (*) to multiply two Polynomials.
Overload the addition assignment operator (+=), subtraction assignment operator (-=), and multiplication assignment operator (*=).
10.12 (DollarAmount Class Enhancement) Enhance class DollarAmount from Exercise 5.32 with overloaded addition (+), subtraction (-), multiplication (*) and division (/) operators.
10.13 (DollarAmount Class Enhancement) Enhance class DollarAmount from Exercise 10.12 to make the overloaded addition, subtraction and multiplication operators commutative.