diff --git a/ch01/README.md b/ch01/README.md index 80e8234a..05044c3a 100644 --- a/ch01/README.md +++ b/ch01/README.md @@ -337,4 +337,5 @@ You can use data/book.txt as the records. compile and execute the bookstore program presented in this section. It is the same as Exercise 1.22. + ![run](https://db.tt/C6OOPuzA) diff --git a/ch02/README.md b/ch02/README.md index 616fbd26..49b90214 100644 --- a/ch02/README.md +++ b/ch02/README.md @@ -550,7 +550,7 @@ decltype(r) d = i; More? Look at [here](http://stackoverflow.com/questions/21369113/what-is-the-difference-between-auto-and-decltypeauto-when-returning-from-a-fun) and [here](http://stackoverflow.com/questions/12084040/decltype-vs-auto) -##Exercise2.39 +##Exercise 2.39 >Compile the following program to see what happens when you forget the semicolon after a class definition. Remember the message for future reference. @@ -564,7 +564,7 @@ return 0; Error message: [Error] expected ';' after struct definition -##Exercise2.40 +##Exercise 2.40 >Write your own version of the Sales_data class. just added some your own define. like this: @@ -581,7 +581,7 @@ struct Sale_data } ``` -##Exercise2.41 +##Exercise 2.41 >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. @@ -717,7 +717,7 @@ int main() } ``` -##Exercise2.42 +##Exercise 2.42 >Write your own version of the Sales_data.h header and use it to rewrite the exercise from § 2.6.2(p. 76) diff --git a/ch04/README.md b/ch04/README.md index a556d3a4..2eee1747 100644 --- a/ch04/README.md +++ b/ch04/README.md @@ -10,6 +10,7 @@ indicate the order in which the operands are grouped: * vec.begin() //=> *(vec.begin()) * vec.begin() + 1 //=> (*(vec.begin())) + 1 ``` + ##Exercise 4.3 >Order of evaluation for most of the binary operators is left undefined to give the compiler opportunities for optimization. diff --git a/docs/ch01/ex1.1.md b/docs/ch01/ex1.1.md deleted file mode 100644 index 346e8e20..00000000 --- a/docs/ch01/ex1.1.md +++ /dev/null @@ -1,11 +0,0 @@ -##Exercise 1.1 - -> Review the documentation for your compiler and determine what file naming convention it uses. Compile and run the main program from page 2. - -### Windows - -![windows](https://db.tt/XGeGsg7O) - -### Linux - -![Linux](https://db.tt/2xKWuztU) diff --git a/docs/ch01/ex1.12_1.15.md b/docs/ch01/ex1.12_1.15.md deleted file mode 100644 index c9945a75..00000000 --- a/docs/ch01/ex1.12_1.15.md +++ /dev/null @@ -1,82 +0,0 @@ -##Exercise 1.12 -> What does the following for loop do? What is the final value -of sum? -```cpp -int sum = 0; -for (int i = -100; i <= 100; ++i) -sum += i; -``` - -the loop sums the numbers from -100 to 100. the final value of sum is zero. - -##Exercise 1.13 -> Rewrite the exercises from § 1.4.1 (p. 13) using for loops. - -Ex1.9: -```cpp -#include - -int main() -{ - int sum = 0; - for (int i=50; i<=100; ++i) - sum += i; - - std::cout << "the sum is: " << sum << std::endl; - - return 0; -} -``` - -Ex1.10: -```cpp -#include - -int main() -{ - for (int i=10; i>=0; --i) - std::cout << i << std::endl; - - return 0; -} -``` - -Ex1.11: -```cpp -#include - -int main() -{ - int val_small = 0, val_big = 0; - std::cout << "please input two integers:"; - std::cin >> val_small >> val_big; - - if (val_small > val_big) - { - int tmp = val_small; - val_small = val_big; - val_big = tmp; - } - - for (int i=val_small; i<=val_big; ++i) - std::cout << i << std::endl; - - return 0; -} -``` - -##Exercise 1.14 -> Compare and contrast the loops that used a for with those -using a while. Are there advantages or disadvantages to using either form? - -If you need a pattern which is using a variable in a condition and incrementing that variable in the -body. You should use `for` loop. Else the `while` loop is more simple. - -Want to know more? look at [this](http://stackoverflow.com/questions/1600282/guideline-while-vs-for) - -##Exercise 1.15 -> Write programs that contain the common errors discussed in -the box on page 16. Familiarize yourself with the messages the compiler -generates. - -**JUST READ IT!** diff --git a/docs/ch01/ex1.16.md b/docs/ch01/ex1.16.md deleted file mode 100644 index ff36d6a9..00000000 --- a/docs/ch01/ex1.16.md +++ /dev/null @@ -1,32 +0,0 @@ -##Exercise 1.16 - -> Write your own version of a program that prints the sum of a set of integers read from cin. - -Many people confused about this exercise, such as [this](http://www.cplusplus.com/forum/beginner/104169/) and [this](http://stackoverflow.com/questions/17841424/how-to-write-this-while-loop-as-a-for-loop). - -In my opinion, the exercise aim to write the program without "**END-OF-FILE**". - -**BUT**, the [code](http://www.cplusplus.com/forum/beginner/104169/#msg561450) in first link is not correct. - -The following are my own version: - -```cpp -#include - -int main() -{ - int limit = 0, sum = 0, value = 0; - std::cout << "How many integers would you like to enter?"; - std::cin >> limit; - - // assume we don't know what is EOF(End-Of-File). - while (std::cin >> value && (--limit != 0)) - sum += value; - - std::cout << sum + value << std::endl; - - return 0; -} -``` - -Watch out for "sum + value" in the `cout` line. diff --git a/docs/ch01/ex1.17_1.19.md b/docs/ch01/ex1.17_1.19.md deleted file mode 100644 index 9ced273c..00000000 --- a/docs/ch01/ex1.17_1.19.md +++ /dev/null @@ -1,21 +0,0 @@ -##Exercise 1.17 - -> What happens in the program presented in this section if the input values are all equal? What if there are no duplicated values? - -If the input values are all equal, it will print nothing unless you enter `EOF`. - -If there are no duplicated values, when different values input, a new line will be printed if you click `Enter`. - -##Exercise 1.18 - -> Compile and run the program from this section giving it only equal values as input. Run it again giving it values in which no number is repeated. - -![run](https://db.tt/F38zExnq) - -##Exercise 1.19 - -> Revise the program you wrote for the exercises in § 1.4.1 (p. 13) that printed a range of numbers so that it handles input in which the first number is smaller than the second. - -Yes, we should use `if` to judge which is bigger. - -review this [code](https://github.com/pezy/Cpp-Primer/blob/master/ch01/ex1_11.cpp) diff --git a/docs/ch01/ex1.2.md b/docs/ch01/ex1.2.md deleted file mode 100644 index ab6995ba..00000000 --- a/docs/ch01/ex1.2.md +++ /dev/null @@ -1,13 +0,0 @@ -##Exercise 1.2 - -> Exercise 1.2: Change the program to return -1. A return value of -1 is often treated as an indicator that the program failed. Recompile and rerun your program to see how your system treats a failure indicator from main. - -###Windows - -![windows](https://db.tt/DIJd9eZb) - -###Linux - -![linux](https://db.tt/lhzXhpCt) - -**255**? why? please look at [this](http://www.tldp.org/LDP/abs/html/exitcodes.html) diff --git a/docs/ch01/ex1.20_1.22.md b/docs/ch01/ex1.20_1.22.md deleted file mode 100644 index 84b8c03d..00000000 --- a/docs/ch01/ex1.20_1.22.md +++ /dev/null @@ -1,31 +0,0 @@ -##Exercise 1.20 - -> http://www.informit.com/title/032174113 contains a copy of Sales_item.h in the Chapter 1 code directory. Copy that file to your working directory. Use it to write a program that reads a set of book sales transactions, writing each transaction to the standard output. - -[Here](https://github.com/pezy/Cpp-Primer/blob/master/ch01/ex1_20.cpp) is the code. - -**You need to enable C++11 support in your compiler. -With GCC and Clang, this can be done with the `-std=c++11` option.** - -**(Never say it again.)** - -How to test it? use the `book.txt` in `data` folder. And do it like this: - -![run](https://db.tt/fm8iHtkF) - -##Exercise 1.21 -> Write a program that reads two Sales_item objects that have the same ISBN and produces their sum. - -The program should check whether the objects have the same ISBN.(Have a look at 1.5.2, surprise!) - -[Code](https://github.com/pezy/Cpp-Primer/blob/master/ch01/ex1_21.cpp) - -##Exercise 1.22 - -> Write a program that reads several transactions for the same ISBN. Write the sum of all the transactions that were read. - -Tips: this program will appear in the section 1.6. - -[Here](https://github.com/pezy/Cpp-Primer/blob/master/ch01/ex1_22.cpp) is the code. - -![run](https://db.tt/UlkuvpAS) diff --git a/docs/ch01/ex1.23_1.25.md b/docs/ch01/ex1.23_1.25.md deleted file mode 100644 index e1953961..00000000 --- a/docs/ch01/ex1.23_1.25.md +++ /dev/null @@ -1,23 +0,0 @@ -##Exercise 1.23 -> Write a program that reads several transactions and counts -how many transactions occur for each ISBN. - -Tip: please review the `1.4.4`. - -[Here](https://github.com/pezy/Cpp-Primer/blob/master/ch01/ex1_23.cpp) is the code. - -##Exercise 1.24 -> Test the previous program by giving multiple transactions -representing multiple ISBNs. The records for each ISBN should be grouped -together. - -You can use data/book.txt as the records. - -![run](https://db.tt/EeDI7lvN) - -##Exercise 1.25 -> Using the Sales_item.h header from the Web site, -compile and execute the bookstore program presented in this section. - -It is the same as Exercise 1.22. -![run](https://db.tt/C6OOPuzA) diff --git a/docs/ch01/ex1.3_1.6.md b/docs/ch01/ex1.3_1.6.md deleted file mode 100644 index 6d6c9336..00000000 --- a/docs/ch01/ex1.3_1.6.md +++ /dev/null @@ -1,67 +0,0 @@ -##Exercise 1.3 -> Write a program to print Hello, World on the standard output. - -```cpp -#include - -int main() -{ - std::cout << "Hello, World" << endl; - return 0; -} -``` - -##Exercise 1.4 -> Our program used the addition operator, +, to add two numbers. Write a program that uses the multiplication operator, *, to print the product instead. - -```cpp -#include - -int main() -{ - std::cout << "Enter two numbers:" << std::endl; - int v1 = 0, v2 = 0; - std::cin >> v1 >> v2; - std::cout << "The product of " << v1 << " and " << v2 - << " is " << v1 * v2 << std::endl; - return 0; -} -``` - -##Exercise 1.5 - -> We wrote the output in one large statement. Rewrite the program to use a separate statement to print each operand. - -```cpp -#include - -int main() -{ - std::cout << "Enter two numbers:" << std::endl; - int v1 = 0, v2 = 0; - std::cin >> v1 >> v2; - std::cout << "The product of "; - std::cout << v1; - std::cout << " and "; - std::cout << v2; - std::cout << " is "; - std::cout << v1 * v2; - std::cout << std::endl; - return 0; -} -``` - -##Exercise 1.6 -> Explain whether the following program fragment is legal. - -It's illegal. - -**[Error] expected primary-expression before '<<' token** - -Fixed it: remove the spare semicolons. - -```cpp -std::cout << "The sum of " << v1 - << " and " << v2 - << " is " << v1 + v2 << std::endl; -``` diff --git a/docs/ch01/ex1.7_1.8.md b/docs/ch01/ex1.7_1.8.md deleted file mode 100644 index 8821528f..00000000 --- a/docs/ch01/ex1.7_1.8.md +++ /dev/null @@ -1,48 +0,0 @@ -##Exercise 1.7 - -> Compile a program that has incorrectly nested comments. - -Example: -```cpp -/* -* comment pairs /* */ cannot nest. -* ''cannot nest'' is considered source code, -* as is the rest of the program -*/ -int main() -{ - return 0; -} -``` - -Compiled result(g++): - -![result](https://db.tt/CqQKu8GQ) - -##Exercise 1.8 - -> Indicate which, if any, of the following output statements are legal: -```cpp -std::cout << "/*"; -std::cout << "*/"; -std::cout << /* "*/" */; -std::cout << /* "*/" /* "/*" */; -``` -> After you’ve predicted what will happen, test your answers by compiling a -program with each of these statements. Correct any errors you encounter. - -Compiled result(g++): - -![result](https://db.tt/mrL9hDCS) - -Corrected? just added a quote: -```cpp -std::cout << "/*"; -std::cout << "*/"; -std::cout << /* "*/" */"; -std::cout << /* "*/" /* "/*" */; -``` - -Output: - - /**/ */ /* diff --git a/docs/ch02/ex2.15_2.25.md b/docs/ch02/ex2.15_2.25.md deleted file mode 100644 index 1fcc5ba6..00000000 --- a/docs/ch02/ex2.15_2.25.md +++ /dev/null @@ -1 +0,0 @@ -##Exercise 2.15 >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. initializer must be an object. (c): valid. (d): invalid. a reference must be initialized. ``` ##Exercise 2.16 >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. let d equal 3.14159. (b): invalid. r1 must be a double object. (c): valid. but value will be truncated. (d): valid. but value will be truncated. ``` ##Exercise 2.17 >What does the following code print? ```cpp int i, &ri = i; i = 5; ri = 10; std::cout << i << " " << ri << std::endl; ``` `10, 10` ##Exercise 2.18 >Write code to change the value of a pointer. Write code to change the value to which the pointer points. ```cpp int a = 0, b = 1; int *p1 = &a, *p2 = p1; // change the value of a pointer. p1 = &b; // change the value to which the pointer points *p2 = b; ``` ##Exercise 2.19 >Explain the key differences between pointers and references. ####definition: the pointer is "points to" anyother type. the reference is "another name" of an **object**. ####key difference: 1. a reference is another name of an **already existing** object. a pointer is an object in its **own right**. 2. Once initialized, a reference remains **bound to** its initial object. There is **no way** to rebind a reference to refer to a different object. a pointer can be **assigned** and **copied**. 3. a reference always get the object to which the reference was intially bound. a single pointer can point to **several different objects** over its lifetime. 4. a reference must be initialized. a pointer need **not be** initialized at the time it is defined. ####Usage advise: Look at [here](http://www.parashift.com/c%2B%2B-faq-lite/refs-vs-ptrs.html) ##Exercise 2.20 >What does the following program do? ```cpp int i = 42; int *p1 = &i; *p1 = *p1 * *p1; ``` `p1` pointer to `i`, `i`'s value changed to 1764(42*42) ##Exercise 2.21 >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, cannot initialize a variable of type 'double *' with an rvalue of type 'int *' (b): illegal, cannot initialize a variable of type 'int *' with an lvalue of type 'int' (c): legal. ``` ##Exercise 2.22 Assuming p is a pointer to int, explain the following code: ```cpp if (p) // ... if (*p) // ... ``` if (p) // whether p is nullptr? if (*p) // whether the value pointed by p is zero? ##Exercise 2.23 >Given a pointer p, can you determine whether p points to a valid object? If so, how? If not, why not? ```cpp if (p) // p points to a valid object else // p points to a invalid object ``` ##Exercise 2.24 >Why is the initialization of p legal but that of lp illegal? ```cpp int i =42; void *p=&i; long *lp=&i; ``` Because the type `void*` is a special pointer type that can hold the address of any object. But we cannot initialize a variable of type `long *` with an rvalue of type `int *` ##Exercise 2.25 >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 is a pointer to int, i is an int, r is a reference to int i. (b): ip is a valid, null pointer. (c): ip is a pointer to int, and ip2 is an int. ``` \ No newline at end of file diff --git a/docs/ch02/ex2.1_2.2.md b/docs/ch02/ex2.1_2.2.md deleted file mode 100644 index 5bfee369..00000000 --- a/docs/ch02/ex2.1_2.2.md +++ /dev/null @@ -1,51 +0,0 @@ -##Exercise 2.1 -> What are the differences between int, long, long long, -and short? Between an unsigned and a signed type? Between a float and -a double? - -C++ guarantees `short` and `int` is **at least** 16 bits, `long` **at least** 32 bits, `long long` **at least** 64 bits. - -The `unsigned` and `signed` type have the **same size**. for example, sizeof(unsigned short) == sizeof(short). - -The C and C++ standards do not specify the representation of float, double and long double. -It is possible that all three implemented as IEEE double-precision. -Nevertheless, for most architectures (gcc, MSVC; x86, x64, ARM) float is indeed a IEEE **single-precision** floating point number (binary32), -and double is a IEEE **double-precision** floating point number (binary64). - -Usage: - -- Use `int` for integer arithmetic. `short` is usually too small and, in practice, -`long` often has the same size as `int`. If your data values are larger than -the minimum guaranteed size of an `int`, then use `long long`. -(In a word: short < **int** < long < long long) - -- Use an unsigned type when you know that the values cannot be negative. -(In a word: no negative, unsigned.) - -- Use double for floating-point computations; float usually does not have -enough precision, and the cost of double-precision calculations versus -single-precision is negligible. In fact, on some machines, double-precision -operations are faster than single. The precision offered by long double -usually is unnecessary and often entails considerable run-time cost. -(In a word: float < **double** < long double) - -Reference: - -- [What are the criteria for choosing between short / int / long data types?](http://www.parashift.com/c++-faq/choosing-int-size.html) -- [Difference between float and double](http://stackoverflow.com/questions/2386772/difference-between-float-and-double) -- Advice: Deciding which Type to Use(This book.) - -##Exercise 2.2 ->To calculate a mortgage payment, what types would you use -for the rate, principal, and payment? Explain why you selected each type. - -use `double`, or also `float`. - -The rate most like that: 4.50 % per year. -The principal most like that: $854.36 -The payment most like that: $1,142.36 - -Reference: - -- [mortgage-calculator](http://www.bankrate.com/calculators/mortgages/mortgage-calculator.aspx) -- [What's in a Mortgage Payment?](http://www.homeloanlearningcenter.com/mortgagebasics/whatsinamortgagepayment.htm) diff --git a/docs/ch02/ex2.26_2.32.md b/docs/ch02/ex2.26_2.32.md deleted file mode 100644 index c87b3a48..00000000 --- a/docs/ch02/ex2.26_2.32.md +++ /dev/null @@ -1,83 +0,0 @@ -##Exercise 2.26 ->Which of the following are legal? For those that are illegal, -explain why. - -```cpp -const int buf; // illegal, buf is uninitialized const. -int cnt = 0; // legal. -const int sz = cnt; // legal. -++cnt; ++sz; // illegal, attempt to write to const object(sz). -``` - -##Exercise 2.27 -> Which of the following initializations are legal? Explain why. - -```cpp -int i = -1, &r = 0; // illegal, r must refer to an object. -int *const p2 = &i2; // legal. -const int i = -1, &r = 0; // legal. -const int *const p3 = &i2; // legal. -const int *p1 = &i2; // legal -const int &const r2; // illegal, r2 must initialize. -const int i2 = i, &r = i; // legal. -``` - -##Exercise 2.28 ->Explain the following definitions. Identify any that are illegal. - -```cpp -int i, *const cp; // illegal, cp must initialize. -int *p1, *const p2; // illegal, p2 must initialize. -const int ic, &r = ic; // illegal, ic must initialize. -const int *const p3; // illegal, p3 must initialize. -const int *p; // legal. a pointer to const int. -``` - -##Exercise 2.29 ->Uing the variables in the previous exercise, which of the -following assignments are legal? Explain why. - -```cpp -i = ic; // legal. -p1 = p3; // illegal. p3 is a pointer to const int. -p1 = ⁣ // illegal. ic is a const int. -p3 = ⁣ // legal. -p2 = p1; // legal. -ic = *p3; // illegal. ic is a const int. -``` - -##Exercise 2.30 ->For each of the following declarations indicate whether the -object being declared has top-level or low-level const. -```cpp -const int v2 = 0; int v1 = v2; -int *p1 = &v1, &r1 = v1; -const int *p2 = &v2, *const p3 = &i, &r2 = v2; -``` - -v2 is top-level const, p2 is low-level const. -p3: right-most const is top-level, left-most is low-level. - -Exercise 2.31 ->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. - -```cpp -r1 = v2; // legal, top-level const in v2 is ignored. -p1 = p2; // illegal, p2 has a low-level const but p1 doesn't. -p2 = p1; // legal, we can convert int* to const int*. -p1 = p3; // illegal, p3 has a low-level const but p1 doesn't. -p2 = p3; // legal, p2 has the same low-level const qualification as p3. -``` - -##Exercise 2.32 ->Is the following code legal or not? If not, how might you -make it legal? - - int null = 0, *p = null; - -illegal. -```cpp -int null = 0, *p = nullptr; -``` diff --git a/docs/ch02/ex2.33_2.35.md b/docs/ch02/ex2.33_2.35.md deleted file mode 100644 index 50eefa6f..00000000 --- a/docs/ch02/ex2.33_2.35.md +++ /dev/null @@ -1 +0,0 @@ -##Exercise 2.33 >Using the variable definitions from this section, determine what happens in each of these assignments: ```cpp a=42; // set 42 to int a. b=42; // set 42 to int b. c=42; // set 42 to int c. d=42; // ERROR, d is an int *. correct: *d = 42; e=42; // ERROR, e is an const int *. correct: e = &c; g=42; // ERROR, g is a const int& that is bound to ci. ``` ##Exercise 2.34 >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. [Here](https://github.com/pezy/Cpp-Primer/blob/master/ch02/ex2_34.cpp) is the code. ##Exercise 2.35 >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. ```cpp const int i = 42; auto j = i; const auto &k = i; auto *p = &i; const auto j2 = i, &k2 = i; ``` j and j2 are int. k and k2 are &int. p is a pointer to const int. [Here](https://github.com/pezy/Cpp-Primer/blob/master/ch02/ex2_35.cpp) is the code. \ No newline at end of file diff --git a/docs/ch02/ex2.36_2.38.md b/docs/ch02/ex2.36_2.38.md deleted file mode 100644 index 2bd1fd7f..00000000 --- a/docs/ch02/ex2.36_2.38.md +++ /dev/null @@ -1 +0,0 @@ -##Exercise 2.36 >In the following code, determine the type of each variable and the value each variable has when the code finishes: ```cpp int a = 3, b = 4; decltype(a) c = a; decltype((b)) d = a; ++c; ++d; ``` `c` is an int, `d` is a referance of `a`. all their value are `4`. ##Exercise 2.37 >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: ```cpp int a = 3, b = 4; decltype(a) c = a; decltype(a = b) d = a; ``` `c` is an int, `d` is a reference of int. ##Exercise 2.38 >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. The way `decltype` handles top-level const and references differs **subtly** from the way `auto` does. Another important difference between `decltype` and `auto` is that the deduction done by decltype depends on the **form** of its given expression. so the key of difference is **subtly** and **form**. ```cpp int i = 0, &r = i; // same auto a = i; decltype(i) b = i; // different auto c = r; decltype(r) d = i; ``` More? Look at [here](http://stackoverflow.com/questions/21369113/what-is-the-difference-between-auto-and-decltypeauto-when-returning-from-a-fun) and [here](http://stackoverflow.com/questions/12084040/decltype-vs-auto) \ No newline at end of file diff --git a/docs/ch02/ex2.39_2.42.md b/docs/ch02/ex2.39_2.42.md deleted file mode 100644 index 504d3bbd..00000000 --- a/docs/ch02/ex2.39_2.42.md +++ /dev/null @@ -1,178 +0,0 @@ -##Exercise2.39 ->Compile the following program to see what happens when -you forget the semicolon after a class definition. Remember the message for -future reference. -```cpp -struct Foo { /* empty */ } // Note: no semicolon -int main() -{ -return 0; -} -``` - -Error message: [Error] expected ';' after struct definition - -##Exercise2.40 ->Write your own version of the Sales_data class. - -just added some your own define. like this: - -```cpp -struct Sale_data -{ - std::string bookNo; - std::string bookName; - unsigned units_sold = 0; - double revenue = 0.0; - double price = 0.0; - //... -} -``` - -##Exercise2.41 ->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. - -####1.5.1 - -```cpp -#include -#include - -struct Sale_data -{ - std::string bookNo; - unsigned units_sold = 0; - double revenue = 0.0; -}; - -int main() -{ - Sale_data book; - double price; - std::cin >> book.bookNo >> book.units_sold >> price; - book.revenue = book.units_sold * price; - std::cout << book.bookNo << " " << book.units_sold << " " << book.revenue << " " << price; - - return 0; -} -``` - -####1.5.2 - -```cpp -#include -#include - -struct Sale_data -{ - std::string bookNo; - unsigned units_sold = 0; - double revenue = 0.0; -}; - -int main() -{ - Sale_data book1, book2; - double price1, price2; - std::cin >> book1.bookNo >> book1.units_sold >> price1; - std::cin >> book2.bookNo >> book2.units_sold >> price2; - book1.revenue = book1.units_sold * price1; - book2.revenue = book2.units_sold * price2; - - if (book1.bookNo == book2.bookNo) - { - unsigned totalCnt = book1.units_sold + book2.units_sold; - double totalRevenue = book1.revenue + book2.revenue; - std::cout << book1.bookNo << " " << totalCnt << " " << totalRevenue << " "; - if (totalCnt != 0) - std::cout << totalRevenue/totalCnt << std::endl; - else - std::cout << "(no sales)" << std::endl; - - return 0; - } - else - { - std::cerr << "Data must refer to same ISBN" << std::endl; - return -1; // indicate failure - } -} -``` - -####1.6 - -**so ugly as you see.** - -```cpp -#include -#include - -struct Sale_data -{ - std::string bookNo; - unsigned units_sold = 0; - double revenue = 0.0; -}; - -int main() -{ - Sale_data total; - double totalPrice; - if (std::cin >> total.bookNo >> total.units_sold >> totalPrice) - { - total.revenue = total.units_sold * totalPrice; - - Sale_data trans; - double transPrice; - while (std::cin >> trans.bookNo >> trans.units_sold >> transPrice) - { - trans.revenue = trans.units_sold * transPrice; - - if (total.bookNo == trans.bookNo) - { - total.units_sold += trans.units_sold; - total.revenue += trans.revenue; - } - else - { - std::cout << total.bookNo << " " << total.units_sold << " " << total.revenue << " "; - if (total.units_sold != 0) - std::cout << total.revenue/total.units_sold << std::endl; - else - std::cout << "(no sales)" << std::endl; - - total.bookNo = trans.bookNo; - total.units_sold = trans.units_sold; - total.revenue = trans.revenue; - } - } - - std::cout << total.bookNo << " " << total.units_sold << " " << total.revenue << " "; - if (total.units_sold != 0) - std::cout << total.revenue/total.units_sold << std::endl; - else - std::cout << "(no sales)" << std::endl; - - return 0; - } - else - { - std::cerr << "No data?!" << std::endl; - return -1; // indicate failure - } -} -``` - -##Exercise2.42 ->Write your own version of the Sales_data.h header and -use it to rewrite the exercise from § 2.6.2(p. 76) - -You can add some function in your header file. Look at [here](https://github.com/pezy/Cpp-Primer/blob/master/ch02/Sales_data.h). - -rewrite the exercise: - -- 1.5.1. [Code](https://github.com/pezy/Cpp-Primer/blob/master/ch02/ex2.42_1.cpp) -- 1.5.2. [Code](https://github.com/pezy/Cpp-Primer/blob/master/ch02/ex2.42_2.cpp) -- 1.6. [Code](https://github.com/pezy/Cpp-Primer/blob/master/ch02/ex2.42_3.cpp) diff --git a/docs/ch02/ex2.3_2.8.md b/docs/ch02/ex2.3_2.8.md deleted file mode 100644 index 8df13ba8..00000000 --- a/docs/ch02/ex2.3_2.8.md +++ /dev/null @@ -1,91 +0,0 @@ -##Exercise 2.3 -> What output will the following code produce? -```cpp -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; -``` - -Output(g++ 4.8): - -``` -32 -4294967264 -32 --32 -0 -0 -``` - -##Exercise 2.4 -> Write a program to check whether your predictions were -correct. If not, study this section until you understand what the problem is. - -[Here](https://github.com/pezy/Cpp-Primer/blob/master/ch02/ex2_4.cpp) is the code, please test it in your computer. - -##Exercise 2.5 -> 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): character literal, wide character literal, string literal, string wide character literal. - -(b): decimal, unsigned decimal, long decimal, unsigned long decimal, octal, hexadecimal. - -(c): double, float, long double. - -##Exercise 2.6 -> What, if any, are the differences between the following -definitions: -```cpp -int month = 9, day = 7; -int month = 09, day = 07; -``` - -The first line's integer is decimal, the second line's integer is octal. - -##Exercise 2.7 -> 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?(new line) "string" - -(b): 31.4 "long double" - -(c): 1024 "float" - -(d): 3.14 "long double" - -Reference: - -- [ASCII Table](http://www.asciitable.com/) - -##Exercise 2.8 -> 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. - -```cpp -#include - -int main() -{ - std::cout << 2 << "\115\012"; - std::cout << 2 << "\t\115\012"; - - return 0; -} - -``` diff --git a/docs/ch02/ex2.9_2.14.md b/docs/ch02/ex2.9_2.14.md deleted file mode 100644 index 32fc2331..00000000 --- a/docs/ch02/ex2.9_2.14.md +++ /dev/null @@ -1 +0,0 @@ -##Exercise 2.9 >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; (a): error: expected '(' for function-style cast or type construction. ```cpp int input_value = 0; std::cin >> input_value; ``` (b): warning: implicit conversion from 'double' to 'int' changes value from 3.14 to 3. ```cpp float i = { 3.14 }; ``` (c): error: use of undeclared identifier 'wage' ```cpp double salary = 9999.99; double wage = 9999.99; ``` (d): ok: but value will be truncated. ```cpp float i = 3.14; ``` ##Exercise 2.10 >What are the initial values, if any, of each of the following variables? ```cpp std::string global_str; int global_int; int main() { int local_int; std::string local_str; } ``` `global_str` and `local_str` are the empty string. `global_int` and `local_int` are the zero. ##Exercise 2.11 > 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. ##Exercise 2.12 >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`, `c`, `d` are invalid. ##Exercise 2.13 >What is the value of j in the following program? ```cpp int i = 42; int main() { int i = 100; int j = i; } ``` `100`. local object named reused hides global reused. ##Exercise 2.14 >Is the following program legal? If so, what values are printed? ```cpp int i = 100, sum = 0; for (int i = 0; i != 10; ++i) sum += i; std::cout << i << " " << sum << std::endl; ``` Yes.It is legal.Printed: `100, 45.` \ No newline at end of file diff --git a/docs/ch03/ex3.11.md b/docs/ch03/ex3.11.md deleted file mode 100644 index 60edb65e..00000000 --- a/docs/ch03/ex3.11.md +++ /dev/null @@ -1 +0,0 @@ -##Exercise 3.11 >Is the following range for legal? If so, what is the type of c? ```cpp const string s = "Keep out!"; for (auto &c : s){/*... */} ``` When you don't change `c`'s value, it's legal, else it's illegal. For example: cout << c; // legal. c = 'X'; // illegal. The type of `c` is `const char&`. read-only variable is not assignable. \ No newline at end of file diff --git a/docs/ch03/ex3.12_3.13.md b/docs/ch03/ex3.12_3.13.md deleted file mode 100644 index e98bb2d1..00000000 --- a/docs/ch03/ex3.12_3.13.md +++ /dev/null @@ -1 +0,0 @@ -##Exercise 3.12 >Which, if any, of the following vector definitions are in error? For those that are legal, explain what the definition does. For those that are not legal, explain why they are illegal. ```cpp vector> ivec; // legal(c++11), vectors. vector svec = ivec; // illegal, different type. vector svec(10, "null"); // legal, vector have 10 strings: "null". ``` ##Exercise 3.13 >How many elements are there in each of the following vectors? What are the values of the elements? ```cpp vector v1; // size:0, no values. vector v2(10); // size:10, value:0 vector v3(10, 42); // size:10, value:42 vector v4{10}; // size:1, value:10 vector v5{10, 42}; // size:2, value:10, 42 vector v6{10}; // size:10, value:"" vector v7{10, "hi"}; // size:10, value:"hi" ``` \ No newline at end of file diff --git a/docs/ch03/ex3.26.md b/docs/ch03/ex3.26.md deleted file mode 100644 index a7f3ab98..00000000 --- a/docs/ch03/ex3.26.md +++ /dev/null @@ -1,8 +0,0 @@ -##Exercise 3.26 ->In the binary search program on page 112, -why did we write `mid=beg+(end-beg)/2;` instead of `mid=(beg+end) /2;`? - -Because the iterator of vector don't define the `+` operator **between the two iterators**. -`beg + end` is illegal. - -We can only use the subtraction between the two iterators. diff --git a/docs/ch03/ex3.27_3.29.md b/docs/ch03/ex3.27_3.29.md deleted file mode 100644 index 925a77dd..00000000 --- a/docs/ch03/ex3.27_3.29.md +++ /dev/null @@ -1 +0,0 @@ -##Exercise 3.27 >Assuming txt_size is a function that takes no arguments and returns an int value, which of the following definitions are illegal? Explain why. ```cpp unsigned buf_size = 1024; int ia[buf_size]; // legal int ia[4 * 7 - 14]; // legal int ia[txt_size()]; // legal char st[11] = "fundamental"; // illegal, the string's size is 12. ``` ##Exercise 3.28 >What are the values in the following arrays? ```cpp string sa[10]; int ia[10]; int main() { string sa2[10]; int ia2[10]; } ``` please see 2.2.1. Variable Definitions -> Default Initialization. `std::string` isn't a build-in type. The initializer will set it empty. `ia` and `ia2` are build-type. But `ia` isn't in the function body, so it will be initalized to **zero**. `ia2` is in the function body. so it's value is **undefined**. You can also use gdb to debug the value when the code is running. ##Exercise 3.29: >List some of the drawbacks of using an array instead of a vector. 1. can't add elements to an array. 2. can't use auto to deduce the type from a list of initalizers. 3. no arrays of references. 4. can't use template and iterator. 5. vector have lots of useful methods and algorithms. \ No newline at end of file diff --git a/docs/ch03/ex3.3.md b/docs/ch03/ex3.3.md deleted file mode 100644 index 177b2a6f..00000000 --- a/docs/ch03/ex3.3.md +++ /dev/null @@ -1 +0,0 @@ -##Exercise 3.3 >Explain how whitespace characters are handled in the string input operator and in the getline function. The `getline` function takes an input stream and a string. This function reads the given stream up to and including the first newline and stores what it read—not including the newline—in its string argument. After getline sees a newline, even if it is the first character in the input, it stops reading and returns. If the first character in the input is a newline, then the resulting string is the empty string. `getline` function whitespace handling, do not ignore the beginning of the line blank characters read characters until it encounters a line break, read to termination and discard newline (line breaks removed from the input stream but is not stored in the string object). [Read more](http://www.cplusplus.com/reference/string/string/getline/) \ No newline at end of file diff --git a/docs/ch03/ex3.30.md b/docs/ch03/ex3.30.md deleted file mode 100644 index 3f2f5410..00000000 --- a/docs/ch03/ex3.30.md +++ /dev/null @@ -1,11 +0,0 @@ -##Exercise 3.30 ->Identify the indexing errors in the following code: -```cpp -constexpr size_t array_size = 10; -int ia[array_size]; -for (size_t ix = 1; ix <= array_size; ++ix) - ia[ix] = ix; -``` - -The size of ia is 10, so the index of value should less than 10. -ix **cannot** equal the array_size. diff --git a/docs/ch03/ex3.33.md b/docs/ch03/ex3.33.md deleted file mode 100644 index b92a7273..00000000 --- a/docs/ch03/ex3.33.md +++ /dev/null @@ -1,10 +0,0 @@ -##Exercise 3.33 ->What would happen if we did not initialize the scores array in the program -on page 116? - -If we did not initialize the scores array. the array is undefined. the value -will be Unknown. - -Look like this: - -![result](https://db.tt/3T4TQoo8) diff --git a/docs/ch03/ex3.34.md b/docs/ch03/ex3.34.md deleted file mode 100644 index 85a8524f..00000000 --- a/docs/ch03/ex3.34.md +++ /dev/null @@ -1,12 +0,0 @@ -##Exercise 3.34 ->Given that p1 and p2 point to elements in the same array, what does the following code do? -Are there values of p1 or p2 that make this code illegal? -```cpp -p1 += p2 - p1; -``` - -we assume p1 and p2 point to an array arr. so `p1 = &arr[0]`; and `p2 = &arr[0]`. -p2 - p1 is the distance of arr[0] to arr[0], and must be zero. -so `p1 += 0;` can not change the p1's point. - -`p1 += p2 - p1;` same as `p1 = p2;`. If p2 and p1 are legal, this code always legal. diff --git a/docs/ch03/ex3.37_3.38.md b/docs/ch03/ex3.37_3.38.md deleted file mode 100644 index 783d2928..00000000 --- a/docs/ch03/ex3.37_3.38.md +++ /dev/null @@ -1,26 +0,0 @@ -##Exercise 3.37 ->What does the following program do? -```cpp -const char ca[] = {'h', 'e', 'l', 'l', 'o'}; -const char *cp = ca; -while (*cp) { - cout << *cp << endl; - ++cp; -} -``` - -Print all the elements of the array. - -##Exercise 3.38 ->In this section, we noted that it was not only illegal but meaningless to try to add two pointers. -Why would adding two pointers be meaningless? - - -Because Subtracting two points gives a logically explainable result - the offset -in memory between two points. Similarly, you can subtract or add an integral number to/from a pointer, -which means "move the pointer up or down". Adding a pointer to a pointer is something which is hard to -explain. The result is meaningless. - ----- -References: -- [Why can't I add pointers](http://stackoverflow.com/questions/2935038/why-cant-i-add-pointers) diff --git a/docs/ch03/ex3.7.md b/docs/ch03/ex3.7.md deleted file mode 100644 index a93d6961..00000000 --- a/docs/ch03/ex3.7.md +++ /dev/null @@ -1,11 +0,0 @@ -##Exercise 3.7 ->What would happen if you define the loop control variable in the previous -exercise as type char? Predict the results and then change your program -to use a char to see if you were right. - -No different. - - auto& c : str - -We use `auto` to let the compiler determine the type of `c`. -which in this case will be `char&`. diff --git a/docs/ch03/ex3.9.md b/docs/ch03/ex3.9.md deleted file mode 100644 index 0845c342..00000000 --- a/docs/ch03/ex3.9.md +++ /dev/null @@ -1 +0,0 @@ -##Exercise 3.9 >What does the following program do? Is it valid? If not, why not? ```cpp string s; cout << s[0] << endl; ``` invalid in theory, but the compiler passes. `s` is empty, so `s[0]` is undefined. But the compiler always define `s[0]` with `\0`, so you can use `s[0]` in the `cout`. \ No newline at end of file diff --git a/docs/ch04/ex4.13_4.16.md b/docs/ch04/ex4.13_4.16.md deleted file mode 100644 index 82857a8b..00000000 --- a/docs/ch04/ex4.13_4.16.md +++ /dev/null @@ -1,39 +0,0 @@ -##Exercise 4.13 ->What are the values of i and d after each assignment? -```cpp -int i; double d; -d = i = 3.5; // i = 3, d = 3.0 -i = d = 3.5; // d = 3.5, i = 3 -``` - -##Exercise 4.14 ->Explain what happens in each of the if tests: -```cpp -if (42 = i) // complie error: expression is not assignable -if (i = 42) // true. -``` - -##Exercise 4.15 ->The following assignment is illegal. Why? How would you correct it? -```cpp -double dval; int ival; int *pi; -dval = ival = pi = 0; -// pi is a pointer to int. -// can not assign to 'int' from type 'int *' -// correct it: -dval = ival = 0; -pi = &ival; -``` - -##Exercise 4.16 ->Although the following are legal, -they probably do not behave as the programmer expects. Why? -Rewrite the expressions as you think they should be. -```cpp -if (p = getPtr() != 0) -if (i = 1024) -// why? always true. use an assigment as a condition. -// correct it -if ((p=getPtr()) != 0) -if (i == 1024) -``` diff --git a/docs/ch04/ex4.17_4.19.md b/docs/ch04/ex4.17_4.19.md deleted file mode 100644 index 0aabaf43..00000000 --- a/docs/ch04/ex4.17_4.19.md +++ /dev/null @@ -1,27 +0,0 @@ -##Exercise 4.17 ->Explain the difference between prefix and postfix increment. - -The postfix operators increment(or decrement) the operand but yield a copy -of the original, unchanged value as its result. - -The prefix operators return the object itself as an **lvalue**. - -The postfix operators return a copy of the object's original value as an **rvalue**. - -##Exercise 4.18 ->What would happen if the while loop on page 148 that prints -the elements from a vector used the prefix increment operator? - -It will print from the second element and will dereference the v.end() at last.(It's undefined and very dangerous) - -##Exercise 4.19 ->Given that ptr points to an int, that vec is a vector, -and that ival is an int, explain the behavior of each of these expressions. -Which, if any, are likely to be incorrect? Why? How might each be corrected? -```cpp -ptr != 0 && *ptr++ // check ptr is not a nullptr. and check the pointer value. -ival++ && ival // check ival and ival+1 whether equal zero. -vec[ival++] <= vec[ival] // incorrect. It is an **undefined behavior.** -// correct: -vec[ival] <= vec[ival+1] -``` diff --git a/docs/ch04/ex4.1_4.3.md b/docs/ch04/ex4.1_4.3.md deleted file mode 100644 index a8a7173c..00000000 --- a/docs/ch04/ex4.1_4.3.md +++ /dev/null @@ -1,25 +0,0 @@ -##Exercise 4.1 ->What is the value returned by 5 + 10 * 20/2? - -105 - -##Exercise 4.2 ->Using Table 4.12 (p. 166), parenthesize the following expressions to -indicate the order in which the operands are grouped: -```cpp -* vec.begin() //=> *(vec.begin()) -* vec.begin() + 1 //=> (*(vec.begin())) + 1 -``` -##Exercise 4.3 ->Order of evaluation for most of the binary operators is left -undefined to give the compiler opportunities for optimization. -This strategy presents a trade-off between efficient code generation -and potential pitfalls in the use of the language by the programmer. -Do you consider that an acceptable trade-off? Why or why not? - -Yes, I think it necessary to hold the trade-off. -Because the speed always the biggest advantage of C++. Sometimes, we need -the compiler's features for efficient work. But if you are not a expert. I -have to advice you do not touch the undefined behaviors. - -For an instance, `cout << i << ++i <Assuming that iter is a vector::iterator, -indicate which, if any, of the following expressions are legal. -Explain the behavior of the legal expressions and -why those that aren’t legal are in error. -```cpp -*iter++; // return *iter, then ++iter. -(*iter)++; // illegal, *iter is a string, cannot increment value. -*iter.empty() // illegal, iter should use '->' to indicate whether empty. -iter->empty(); // indicate the iter' value whether empty. -++*iter; // illegal, string have not increment. -iter++->empty(); // return iter->empty(), then ++iter. -``` diff --git a/docs/ch04/ex4.21_4.24.md b/docs/ch04/ex4.21_4.24.md deleted file mode 100644 index 05694320..00000000 --- a/docs/ch04/ex4.21_4.24.md +++ /dev/null @@ -1,31 +0,0 @@ -##Exercise 4.23 ->The following expression fails to compile due to operator precedence. -Using Table 4.12 (p. 166), explain why it fails. How would you fix it? -```cpp -string s = "word"; -string pl = s + s[s.size() - 1] == 's' ? "" : "s" ; -``` - -Operator Precedence: `?:` < `+` -Fix it: -```cpp -string pl = s + (s[s.size() - 1] == 's' ? "" : "s") ; -``` - -##Exercise 4.24 ->Our program that distinguished between high pass, pass, -and fail depended on the fact that -the conditional operator is right associative. -Describe how that operator would be evaluated -if the operator were left associative. - -if the operator were left associative. -```cpp -finalgrade = (grade > 90) ? "high pass" : (grade < 60) ? "fail" : "pass"; -``` -would same as : -```cpp -finalgrade = ((grade > 90) ? "high pass" : (grade < 60)) ? "fail" : "pass"; -``` -if `grade > 90`, first conditional operator's result is `high pass`. so the finalgrade is always fail. -It's contradictory obviously. diff --git a/docs/ch04/ex4.4_4.7.md b/docs/ch04/ex4.4_4.7.md deleted file mode 100644 index bbf3bc6b..00000000 --- a/docs/ch04/ex4.4_4.7.md +++ /dev/null @@ -1,36 +0,0 @@ -##Exercise 4.4 ->Parenthesize the following expression to show how it is evaluated. -Test your answer by compiling the expression (without parentheses) -and printing its result. -```cpp -12 / 3 * 4 + 5 * 15 + 24 % 4 / 2 -// parenthesize -((12/3)*4) + (5*15) + ((24%4)/2) -// 16 + 75 + 0 = 91 -// print: 91 -``` - -##Exercise 4.5 ->Determine the result of the following expressions. -```cpp --30 * 3 + 21 / 5 // -90+4 = -86 --30 + 3 * 21 / 5 // -30+63/5 = -30+12 = -18 -30 / 3 * 21 % 5 // 10*21%5 = 210%5 = 0 --30 / 3 * 21 % 4 // -10*21%4 = -210%4 = -2 -``` - -##Exercise 4.6 ->Write an expression to determine whether an int value is even or odd. - -```cpp -i%2 == 0 ? "even" : "odd" -``` - -##Exercise 4.7 ->What does overflow mean? Show three expressions that will overflow. - -```cpp -short svalue = 32767; ++svalue; // -32768 -unsigned uivalue = 0; --uivalue; // 4294967295 -unsigned short usvalue = 65535; ++usvalue; // 0 -``` diff --git a/docs/ch04/ex4.8_4.12.md b/docs/ch04/ex4.8_4.12.md deleted file mode 100644 index 185682be..00000000 --- a/docs/ch04/ex4.8_4.12.md +++ /dev/null @@ -1,43 +0,0 @@ -##Exercise 4.8 ->Explain when operands are evaluated in the logical AND, -logical OR, and equality operators. - -logical `AND` : `true` only if both its operands evaluated to `true`; -logical `OR` : `true` if either of its operands evaluated to `true`; -equality operators : `true` only if its operands are equal. - -##Exercise 4.9 ->Explain the behavior of the condition in the following if: -```cpp -const char *cp = "Hello World"; -if (cp && *cp) -``` - -cp is a pointer to `const char *`, and it's not a nullptr. true. - -`*cp` is a const char: 'H', and it is explicit a nonzero value. true. - -true && true = true. - -##Exercise 4.10 ->Write the condition for a while loop that would read ints from -the standard input and stop when the value read is equal to 42. - -```cpp -int i = 0; -while(cin >> i && i != 42) -``` - -##Exercise 4.11 ->Write an expression that tests four values, a, b, c, and d, -and ensures that a is greater than b, which is greater than c, -which is greater than d. - -```cpp -a>b && b>c && c>d -``` - -##Exercise 4.12 ->Assuming i, j, and k are all ints, explain what i != j < k means. - -if `j - - - {{NAME}} :: Answer Note - - - - -
-
-

{{NAME}} :: index

-
-
- {{CONTENT}} -
-
- - - comments powered by Disqus -
- - Fork me on GitHub - - - - - -