Nothing Special   »   [go: up one dir, main page]

Mon Multipl: L T (Hint

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Identifiers are often used as variables.

A C++ variable has three possible


storage classes: automatic, static, and dynamic. C++ provides operators new
and delete to dynamically allocate and release storage at run time. It is also
possible for an identifier to serve as a reference to a variable or lvalue. A
reference must be initialized when declared and thus becomes an alias for
the given lvalue. Reference parameters are often used to avoid copying in
argument passing and to collect return values from a function. Pass objects by
reference whenever possible. When a reference or pointer argument does not
collect a return value, it should be declared read-only with the const specifier
to bar modifications to it. Whenever a member function does not alter its host,
declare the host const.
Identifiers also serve as function names. Overloading assigns multiple
meanings to the same function name. Different definitions for the same func-
tion name must have distinct signatures. The C++ compiler uses a systematic
function call resolution procedure based on the number and types of the actual
arguments to pick the correct version of an overloaded function. A function
can have optional arguments with default values. Passing a variable number
of arguments whose types are known only at run time is possible.
Identifiers and expressions have types. Built-in rules govern the conversion
of types when an operator is given different data types. Type conversions also
take place when passing arguments to a function and when explicitly requested
in a program.
The two classes, Fraction and Cirbuf, show how a class encapsulates data
and functions to define well-behaved objects. These examples illustrate topics
presented in this chapter and demonstrate OOP techniques. Furthermore, they
are revisited frequently in later chapters.

1. Class member names have class scope and are generally not recognized outside
the class without qualification. Can you think of any class members that are
recognized without being qualified?

2. Write a gcd that is nonrecursive and that can take any int arguments, not just
nonnegative ones.

3. Write a function lcm that takes two integer arguments and returns the least com-
mon multiple of all the arguments (e.g., lcm (-15,12) is 60). Modify operator-
and operator+ in Fraction to use lcm. (Hint: Use gcd.)
5. Add to the class Fraction a member function floor, which returns the largest
integer less than or equal to the fraction. (Hint: Consider both positive and
negative fractions.) Also add const declarations in every appropriate place for
Fraction.

const int* ai const double& bi


int * const ai double & const bi
const int * const ai const double & const bi

7. A proper fraction lies between -1 and 1.Add to Fraction a member function


iSProper () .

8. Add a member to CirBuf so that the capacity of a circular buffer object can
be increased dynamically. The member prototype should be int grow(int n),
which causes the capacity to increase by n characters. Any unconsumed char-
acters in the buffer remain. A false is returned when grow() fails.

9. Examine closely the function parti tion used in our quicksort (Section 3.4).
Can you show that, after the while loop, the element a [i] is not less than pe?
Is it an improvement to the partition code to modify the exchange call to
exchange (a, i++, j--)?

10. Given an array of distinct integers, write an efficient program to find the median,
the element of the array whose value is in the middle. Namely, roughly half of
the elements are over and half are under the median. (Hint: Modify parti tion.)

11. List the explicit conversion notations and explain their meaning using your
own words.

12. Write a function sumthat produces the total of an indefinite number of argu-
ments uniformly of type int, float, or double. Make sumalways return double
for now.

13. Consider the following code for the function swap.Is there any syntax problem
here? Does the function achieve its intended purpose? Why?
void swap(int& a, int& b)
{ int& tmp = bi
b = ai
14. In a function header, does it make sense to declare as const any formal param-
eter other than a pointer or a reference? Why? Is it possible to use the same
identifier both as a variable and as a function name? What about an enumtag?
A typedef name?

15. Write a program that creates an array frac3rr with new,initializes each ele-
ment frac_arr [i] with the Fraction object i~l' displays the array, and then
deletes it.

16. A stack is a first-in/last-out buffer. If the numbers 1.0 2.0 3.0 are entered
I I

into the buffer in that order, then they are taken out of the stack in the sequence
3.0, 2.0 I 1. O.The operation push enters an item on the top of a stack, and
the operation pop removes an item from the top of the stack. These are the
only two operations that are allowed to modify a stack. Following the circular
buffer example in Section 3.12, implement a class Stack for type double.

17. Exercise RP-1: Use the Stack object in Exercise 16 to implement a program rp
to evaluate a reverse Polish expression. For example,

displays the value of 3.1 / -4.2 + (5.3 - 6.4) * 7.5. (Hint: Use the library
function strtod.)

19. (a) If you have a function sumat file scope and a function sumin a class, is the
function sumconsidered to be overloaded? Why? (b) If the file scope sumhas
a different signature than the class scope sum,can you call the file scope sum
from a class member function without using the scope operator : :?

20. A function compareis a natural candidate for overloading. Write versions that
compare integers, floating-point numbers, strings, characters, and fractions.

21. In C++, the return type of a function is not considered part of the signature
of a function. Does this prevent you from defining functions that, in effect,
make the return value part of the signature? If not, explain the mechanism you
would use.

You might also like