- Exercise 2.1
- Exercise 2.2
- Exercise 2.3
- Exercise 2.4
- Exercise 2.5
- Exercise 2.6
- Exercise 2.7
- Exercise 2.8
- Exercise 2.9
- Exercise 2.10
- Exercise 2.11
- Exercise 2.12
- Exercise 2.13
- Exercise 2.14
- Exercise 2.15
- Exercise 2.16
- Exercise 2.17
- Exercise 2.18
- Exercise 2.19
- Exercise 2.20
- Exercise 2.21
- Exercise 2.22
- Exercise 2.23
- Exercise 2.24
- Exercise 2.25
- Exercise 2.26
- Exercise 2.27
- Exercise 2.28
- Exercise 2.29
- Exercise 2.30
- Exercise 2.31
- Exercise 2.32
- Exercise 2.33
- Exercise 2.34
- Exercise 2.35
- Exercise 2.36
- Exercise 2.37
- Exercise 2.38
- Exercise 2.39
- Exercise 2.40
- Exercise 2.41
- Exercise 2.42
What are the differences between int, long, long long, and short? Between an unsigned and a signed type? Between a float and a double?
-
(a)
short, int, long, long long have differenct sizes:
short: at least 16bits;
int: at least as large as short;
long: at least 32bits, and at least as large as int;
long long: at least 64bits, and at least as large as long, introduced by new standard.
-
(b)
A signed type represents negative or positive numbers(including zero);
An unsigned type represents only values greater than or equal to zero.
-
(c)
float: single-precision floating-point value, at least yields 6 significant digits;
double: double-precision floating-point value, at least yields 10 significant digits.
To calculate a mortgage payment, what types would you use for the rate, principal, and payment? Explain why you selected each type.
rate: double
principal: double
payment: double
Reason:
- none of the above can be represented only by integer in real world, so we should use floating point types.
- double provides higher precision than float.
What output will the following code produce?
unsigned u = 10, u2 = 42;
std::cout << u2 - u << std::endl;
std::cout << u - u2 << std::endl;
int i = 10, i2 = 42;
std::cout << i2 - i << std::endl;
std::cout << i - i2 << std::endl;
std::cout << i - u << std::endl;
std::cout << u - i << std::endl;
The output(compiled by g++):
32
4294967264 // 2^32 - (42 - 10)
32
-32
0
0
Write a program to check whether your predictions were correct. If not, study this section until you understand what the problem is.
Determine the type of each of the following literals. Explain the differences among the literals in each of the four examples:
(a) 'a', L'a', "a", L"a"
(b) 10, 10u, 10L, 10uL, 012, 0xC
(c) 3.14, 3.14f, 3.14L
(d) 10, 10u, 10., 10e-2
(a)
'a' - char
L'a' - wchar_t
"a" - const char[2]
L"a" - const wchar_t[2]
(b)
10 - int
10u - unsigned int
10L - long
10uL - unsigned long
012 - octal int
0xC - hexadecimal int
(c)
3.14 - double
3.14f - float
3.14L - long double
(d)
10 - int
10u - unsigned int
10. - double
10e-2 - double
What, if any, are the differences between the following definitions:
int month = 9, day = 7;
int month = 09, day = 07;
The first line uses decimal numbers as initializers, while the second line uses octal numbers as initializers. But 9 is not a octal number, so it's an error.
What values do these literals represent? What type does each have?
(a) "Who goes with F\145rgus?\012"
(b) 3.14e1L
(c) 1024f
(d) 3.14L
(a) "Who goes with Fergus?", string literal
(b) 31.4, double
(c) error, 1024 is not a floating-point number, thus cannot be appended with f
(d) 3.14, long double
Using escape sequences, write a program to print 2M followed by a newline. Modify the program to print 2, then a tab, then an M, followed by a newline.
Explain the following definitions. For those that are illegal, explain what’s wrong and how to correct it.
(a) std::cin >> int input_value;
(b) int i = { 3.14 };
(c) double salary = wage = 9999.99;
(d) int i = 3.14;
See Exer02_09.cpp.
What are the initial values, if any, of each of the following variables?
std::string global_str;
int global_int;
int main()
{
int local_int;
std::string local_str;
}
global_str: empty string
global_int: 0
local_int: undefined value
local_str: empty string
Explain whether each of the following is a declaration or a definition:
(a) extern int ix = 1024;
(b) int iy;
(c) extern int iz;
(a) definition
(b) definition
(c) declaration
Which, if any, of the following names are invalid?
(a) int double = 3.14;
(b) int _;
(c) int catch-22;
(d) int 1_or_2 = 1;
(e) double Double = 3.14;
(a) invalid, C++ keywords cannot be used as identifiers.
(b) valid, see Page046_underline.cpp for more details.
(c) invalid, only underscore, digits and letters are permitted.
(d) invalid, identifiers cannot begin with a digit.
(e) valid, upper case and lower case letters are dinstinct.
What is the value of j in the following program?
int i = 42;
int main()
{
int i = 100;
int j = i;
}
j = 100, local variable i hides global variable with the same name.
Is the following program legal? If so, what values are printed?
int i = 100, sum = 0;
for (int i = 0; i != 10; ++i)
sum += i;
std::cout << i << " " << sum << std::endl;
legal, output is
45 100
Which of the following definitions, if any, are invalid? Why?
(a) int ival = 1.01;
(b) int &rval1 = 1.01;
(c) int &rval2 = ival;
(d) int &rval3;
(a) valid.
(b) invalid, non-const reference cannot be initialized with a literal.
(c) valid.
(d) invalid, reference must be initialized.
Which, if any, of the following assignments are invalid? If they are valid, explain what they do.
int i = 0, &r1 = i; double d = 0, &r2 = d;
(a) r2 = 3.14159;
(b) r2 = r1;
(c) i = r2;
(d) r1 = d;
(a) valid.
(b) valid.
(c) valid.
(d) valid.
All of the assignments above are not initializations. Plain references cannot be bound to another type, but could be assigned with another compatible type.
What does the following code print?
int i, &ri = i;
i = 5; ri = 10;
std::cout << i << " " << ri << std::endl;
10 10
Write code to change the value of a pointer. Write code to change the value to which the pointer points.
int *p = 0;
int val1 = 0, val2 = 2;
p = &val1;
*p = val2;
Explain the key differences between pointers and references.
Key differences:
- references are not objects; pointers are objects.
- references must be initialized and can only be bound to one object, but pointers can be bound to different objects and don't have to be initialized.
What does the following program do?
int i = 42;
int *p1 = &i; *p1 = *p1 * *p1;
The program initializes pointer p1 with i and changes the value of i to its square by dereferencing the pointer p1.
Explain each of the following definitions. Indicate whether any are illegal and, if so, why.
int i = 0;
(a) double* dp = &i;
(b) int *ip = i;
(c) int *p = &i;
(a) illegal, plain pointers and the object they point to must have the same type.
(b) illegal, cannot initialize a pointer with an int, even the value the the int is 0.
(c) legal.
Assuming p is a pointer to int, explain the following code:
if (p) // ...
if (*p) // ...
if (p)
judges if the value of pointer p is 0.
if (*p)
judges if the value of the object that p points to is 0.
Given a pointer p, can you determine whether p points to a valid object? If so, how? If not, why not?
No, we cannot determine if p points to a valid object. Because we cannot distinguish a valid address from an invalid one.
Why is the initialization of p legal but that of lp illegal?
int i = 42;
void *p = &i;
long *lp = &i;
Because we can use a void pointer to point to object of any type, otherwise the type of the pointer and the object it points to must match.
Determine the types and values of each of the following variables.
(a) int* ip, i, &r = i;
(b) int i, *ip = 0;
(c) int* ip, ip2;
(a)
ip: int pointer, uninitialized
i: int, uninitialized
r: int reference, bound to i
(b)
i: int, uninitialized
ip: int pointer, initialized to 0
(c)
ip: int pointer, uninitialized
ip2: int, uninitialized
Which of the following are legal? For those that are illegal, explain why.
(a) const int buf;
(b) int cnt = 0;
(c) const int sz = cnt;
(d) ++cnt; ++sz;
(a) illegal, const object must be initialized.
(b) legal.
(c) legal.
(d) illegal, sz is a const and thus its value cannot be changed.
Which of the following initializations are legal? Explain why.
(a) int i = -1, &r = 0;
(b) int *const p2 = &i2;
(c) const int i = -1, &r = 0;
(d) const int *const p3 = &i2;
(e) const int *p1 = &i2;
(f) const int &const r2;
(g) const int i2 = i, &r = i;
(a) illegal; we can not initialize a plain reference with a literal.
(b) legal.
(c) legal; we can initialize a const reference with a literal.
(d) legal; define a const pointer to a const int and initialize it with &i2.
(e) legal.
(f) illegal; reference must be initialized.
(g) legal; const reference can be bound to a plain variable.
Explain the following definitions. Identify any that are illegal.
(a) int i, *const cp;
(b) int *p1, *const p2;
(c) const int ic, &r = ic;
(d) const int *const p3;
(e) const int *p;
(a) illegal; define a int variable and a const pointer of int, const pointers must be initialized.
(b) illegal; define a plain pointer to int and a const pointer to int, const pointers must be initialized.
(c) illegal; const variable must be initialized.
(d) illegal; pointer to const must be initialized.
(e) legal; define a pointer to const int.
Uing the variables in the previous exercise, which of the following assignments are legal? Explain why.
(a) i = ic;
(b) p1 = p3;
(c) p1 = ⁣
(d) p3 = ⁣
(e) p2 = p1;
(f) ic = *p3;
(a) legal; we can assign to a plain variable with a const or non-const variable; top-level const are ignored.
(b) illegal; we cannot assign to a plain pointer with a pointer to const; low-level const won't be ignored.
(c) illegal; plain pointer cannot point to const variable.
(d) illegal; const pointers cannot be assigned.
(e) illegal; const pointers cannot be assigned.
(f) illegal; const variables cannot be assigned.
For each of the following declarations indicate whether the object being declared has top-level or low-level const.
const int v2 = 0; int v1 = v2;
int *p1 = &v1, &r1 = v1;
const int *p2 = &v2, *const p3 = &i, &r2 = v2;
v2: top-level const.
v1: non-const.
p1: non-const.
r1: non-const.
p2: low-level const.
p3: top-level and low-level const.
r2: low-level const.
Given the declarations in the previous exercise determine whether the following assignments are legal. Explain how the top-level or low-level const applies in each case.
r1 = v2;
p1 = p2;
p2 = p1;
p1 = p3;
p2 = p3;
(a) legal; top-level const are ignored.
(b) illegal; low-level const won't be ignored, thus a non-const pointer cannot be assigned with a low-level const pointer.
(c) legal; a pointer to const can be assigned with another variable.
(d) illegal; the reason is the same as in (b).
(e) legal; top-level const are ignored, both p2 and p3 are low-level const.
Is the following code legal or not? If not, how might you make it legal?
int null = 0, *p = null;
Illegal. It's illegal to assign a int variable to a pointer, even if the variable's value is 0.(page 54)
Using the variable definitions from this section, determine what happens in each of these assignments:
a=42; b=42; c=42;
d=42; e=42; g=42;
a=42; // legal; a is a plain int
b=42; // legal; b is a plain int
c=42; // legal; c is a plain int
d=42; // illegal; d is a plain pointer
e=42; // illegal; e is a pointer to const
g=42; // illegal; g is a const int
Write a program containing the variables and assignments from the previous exercise. Print the variables before and after the assignments to check whether your predictions in the previous exercise were correct. If not, study the examples until you can convince yourself you know what led you to the wrong conclusion.
Determine the types deduced in each of the following definitions. Once you’ve figured out the types, write a program to see whether you were correct.
const int i = 42;
auto j = i; const auto &k = i; auto *p = &i; const auto j2 = i, &k2 = i;
j: int; top-level const is ignored.
k: const reference to int.
p: pointer to const int.
j2: const int.
k2: const reference to int.
In the following code, determine the type of each variable and the value each variable has when the code finishes:
int a = 3, b = 4;
decltype(a) c = a;
decltype((b)) d = a;
++c;
++d;
c: int;
d: reference to int.
When the code finishes:
a = 4;
b = 4;
c = 4;
b = 4;
Assignment is an example of an expression that yields a reference type. The type is a reference to the type of the left-hand operand. That is, if i is an int, then the type of the expression i = x is int&. Using that knowledge, determine the type and value of each variable in this code:
int a = 3, b = 4;
decltype(a) c = a;
decltype(a = b) d = a;
c: int;
d: reference to int.
Describe the differences in type deduction between decltype and auto. Give an example of an expression where auto and decltype will deduce the same type and an example where they will deduce differing types.
Difference:
- auto would ignore top-level const, decltype won't:
const int a = 42;
auto b = a; // b is int
decltype(a) c = a; // c is const int
- auto regards references as the types they refer to, decltype regards reference as reference:
const int a = 42, &ri = a;
auto b = ri; // b is const int
decltype(ri) c = a; // c is const reference to int
- auto doesn't differentiate rvalue from lvalue, decltype does:
int a = 42;
auto b = (a); // b is int
decltype((a)) c = a; // c is reference to int
Compile the following program to see what happens when you forget the semicolon after a class definition. Remember the message for future reference.
struct Foo { /* empty */ } // Note: no semicolon
int main()
{
return 0;
}
Write your own version of the Sales_data class.
Use your Sales_data class to rewrite the exercises in § 1.5.1(p. 22), § 1.5.2(p. 24), and § 1.6(p. 25). For now, you should define your Sales_data class in the same file as your main function.
Exer02_41_1.cpp | Exer02_41_2.cpp | Exer02_41_3.cpp
Write your own version of the Sales_data.h header and use it to rewrite the exercise from § 2.6.2(p. 76)