天天看點

C++ Primer Plus (Six Edition) Chapter 3, Review

Q1. Why does C++ have more than one integer type?

A:  In order to give users the option of choosing the integer type that best meets a program's particular requirements and save system memory also  for which

     may speed up the calculation.

Q2. Declare variables matching the following descriptions:

       a. A short integer with the value 80

       b. An unsigned int integer with the value 42,110

       c. An integer with the value 3,000,000,000

A:    a. short int  a = 80;

       b. unsigned int  b = 42110;

       c. long long c  = 3,000,000,000;

       Note: Usually you should not make int be large enough to hold 3,000,000,000.

Q3. What safeguards does C++ provide to keep you from exceeding the limits of an integer type? 

A:    Sateguards are not provided, still we can use the climits header file to determine what the limits are.

Q4. What is the distinction between 31L and 33?

A:    The constant 33L is type long, while 33 is type int.

Q5. Consider the two C++ statements that follow:

       char grade = 65;

       char grade = 'A';

       Are they equivalent?

A:   No, they only have the same effect on some systems, and ' A ' is a type char, whereas 65 is a type int.

Q6. How could you use C++ to find out which character the code 88 represents? Come up with at least two ways.

A:  Here are for  ways:

     char c = 88;

      cout << c << endl;

     cout.put(char(88));

     cout << char(88) << endl;

     cout << (char)88 << endl;

Q7. Assigning a long value to a float can result in a rounding error. What about assigning long to double? long long to double?

A:   It depends on how large the two types are. If long is 4 bytes, there's no loss, for the largest long value would be about 2 billion, which is 10 digits.

      And because double provides at least 13 significant figures, no rounding would be needed. The long long type, on the other hand, can reach 19 

     digits, which exceeds the 13 significant figures guaranteed for double. Also note that long long is an integer type, while double is not.

Q8. Evaluate the following expressions as C++ would:

       a. 8 * 9 + 2

       b. 6 * 3 / 4

       c. 3 / 4 * 6

       d. 6.0 * 3 / 4

       e. 15 % 4

A:   They successively yield 74, 4, 0, 4.5, 3.

Q9. Suppose x1 and x2 are two type double variables that you want to add as integers and assign to an integer variable. Construct a C++ statement for doing so. What if 

       you want to add them as type double and then convert to int?

A: 1.    int pos = (int) x1 + (int) x2;      // or  int pos = int (x1) + int (x2);

     2.   int pos =  (int) (x1 + x2);         // or  int pos = int (x1 + x2); 

Q10. What is the variable type for each of the following declarations?

         a. auto cars = 15;

         b. auto iou = 150.37f;

         c. auto level = ' B ';

         d. auto crat = U ' /U00002155 ' ;

         e. auto fract = 8.25f /  2.5;

A:    They successively are type int, float, char, char32_t and double.

繼續閱讀