C + + Primer Fifth Edition exercise answer chapter II

Source: Internet
Author: User

Exercise 2.1

Q: What is the difference between type int, long, long long and short, and what is the difference between unsigned and signed types? What is the difference between a float and a double?

A:int, long, long long and short sizes differ in the range of data represented. Unsigned can only represent 0 and positive numbers, and unsigned can also represent negative numbers. Float is a single-precision floating-point number, double doubles, in general, float accounts for 4 bytes and double is 8 bytes.

Exercise 2.2

Q: What type of data should I choose for interest rate, principal and payment when calculating a mortgage loan? Explain your reasons.

A: The interest rate should be expressed in unsigned double, and the principal and payment should be expressed using unsigned float. Because interest rates generally have more decimal digits, and there are no negative numbers, the principal and payment scale is small and there is no negative number.

Exercise 2.3

Q: Read the program, write the results

unsigned u = ten, U2 = 42;

Std::cout << u2-u << Std::endl; 32

Std::cout << u-u2 << Std::endl; -32 of the complement 4294967264

int i = ten, i2 = 42;

Std::cout << i2-i << Std::endl; 32

Std::cout << i-i2 << Std::endl; -32

Std::cout << i-u << Std::endl; 0

Std::cout << u-i << Std::endl; 0

Exercise 2.5

Q: Indicates the data type of the following literal

A: (1) ' A ' < character literal, l ' a ' < wide character literal, type is wchar_t>, ' a ' < string literal;, l ' a ' < wide character string literals >

(2) 10< decimal notation, 10u< unsigned integral type, 10l< long integer, 10ul< unsigned long integer, 012< octal;, 0xc< hex >

(3) 3.14,< floating point > 3.14f< single-precision floating-point literal, type is FLOAT>, 3.14l< extended precision floating-point literal, type is long double>

(4) 10< decimal notation, 10u< unsigned integer, 10.< floating-point type, 10e-2< float literal >

Exercise 2.6

Q: Are there any differences between the two sets of definitions below?

A:

Int month = 9, day = 7; The defined month and day are 10 binary

int month =, day = 07; The defined month and day are octal, and month will error because 09 is out of range. Error:invalid Digit ' 9 ' in octal constant

Exercise 2.7

Q: What does the following literal mean? What are the respective data types?

A: (1) "Who goes with f\145rgus?\012"---\145 represents the lowercase letter "E" and \012 represents a newline character. Output: Who goes with Fergus?

(2) 3.14e1l---Represents the extended floating-point type, which is a long double

(3) 1024f---Single-precision floating-point literals, type float

(4) 3.14L---Represents the extended floating point type, which is a long double

Exercise 2.8

Q: Write a program with escape, first output 2M, then go to a new line, modify the program to make its first output 2, then output tab, then output m, and finally go to a new line.

A:cout << "\62\115\012";

cout << "\62\t\115\012";

Exercise 2.9

Q: Explain the meanings of the following definitions. For an illegal definition, explain where it's wrong

A: (1) std::cin >> int Input_value---error, the variable cannot be defined in the input output statement.

(2) int i = {3.14}; ---error, initializing an int variable with a floating-point type in the initialization list may lose data and the compiler will refuse to execute

(3) Double salary = wage = 9999.99; ---wage is undefined, if wage is defined, the statement executes normally, and eventually wage and salary are equal

(4) int i = 3.14; ---warning, there is an implicit conversion, I value is 3.

Exercise 2.10

Q: What are the initial values for the following variables?

A:std::string G_str; Initialize to an empty string

int g_int; Initialized to 0

void test210 ()

{

int local_int; Not initialized by standard local variables, and may also be initialized to null under the g++ compiler

Std::string local_str;

}

Exercise 2.11

Q: Indicates whether the following statement is declared or defined.

A: (1) extern int IX = 1024; Definition (any declaration that contains an explicit initialization becomes a definition)

(2) int iy; Declare and define (want to declare without definition, just before the variable name is extern)

(3) extern int iz; Statement

Exercise 2.12

Q: Please indicate which of the following names are illegal?

A: (1) int doube = 3.14; Illegal, double is a keyword and cannot be used as a variable name

(2) int _; Legal

(3) int Catch-22; Illegal, variable names can only contain letters, numbers, underscores

(4) int 1_or_2 = 1; Illegal, cannot start with a number

(5) Double double = 3.14; Legal

Exercise 2.13

Q: What is the value of J in the program below?

A:int i = 42;

void test213 ()

{

int i = 100;

int j = i;

cout << "j =" << J << Endl; j = 100, using local variable J.

}

Exercise 2.14

Q: Is the following procedure legal? Output what?

A:int i =100, sum = 0;

for (int i =0; I! = ten; ++i) {

sum + = i;

}

cout << i << "" << sum << Endl; i = +, sum = i in the 45,for loop, only works in the loop body, so the output is global variable i= 100, local variable sum is still within scope, so output local variable sum=45.

Exercise 2.15

Q: Which of the following definitions is not legal, why?

A: (1) int ival = 1.01; initialize int with float, illegal, warning

(2) int &rval1 = 1.01; Illegal, the initial value of the reference must be an object

(3) int &rval2 = ival; Legal

(4) Int &rval3; Illegal, the reference must be initialized.

Exercise 2.16

Q: Which of the following assignment is not legal, why?

A:int i = 0, &rl = i;

Double d = 0, &r2 = D;

(1) r2 = 3.14159; Legal, the value of D also becomes 3.14159

(2) r2 = R1; Legal, but implicit conversions

(3) i = R2; Legal, but implicit conversions

(4) r1 = D; Legal, but implicit conversions

Exercise 2.17

Q: What result does the following code output?

A:int I, &ri = i;

i = 5;

RI = 10;

cout << "i =" << i << "ri =" << ri << Endl; i = ten, RI = ten---Changing the value of RI will also change the value of I.

Exercise 2.18

Q: Write code to change the value of the pointer, respectively, and the value of the object that the pointer refers to.

A:int i = 20;

int j = 10;

int* p = &i;

cout << "p =" << p << Endl; p = 0x7ffee3a3ea3c

p = &j;

cout << "p =" << p << Endl; p = 0x7ffee3a3ea38

*p = 5;

cout << "*p =" << *p << "i =" << i << "j =" << J << Endl; *p = 5 I = j = 5

Exercise 2.19

Q: Describes the main differences between pointers and references.

A: (1) The reference must be initialized at the time of definition, and the pointer cannot be initialized

(2) A reference can point to only one object during its life cycle, and the pointer may point to different objects successively

(3) The pointer itself is an object that allows the pointer to be assigned and copied.

Exercise 2.20

Q: Describe the role of the following code

A:int i = 42;

int * p = &i;

*p = *p * *P; 42*42, ask for the square of I.

Exercise 2.21

Q: Explain whether the definitions below are illegal.

A:int i = 0;

(1) double* DP = &i; Illegal, cannot initialize the Doube pointer with a variable of type int

(2) int* IP = i; Illegal, cannot initialize pointer with int value

(3) int* p = &i; Legal

Exercise 2.22

Q: Suppose P is an int type pointer that explains what the following code means

A:If (p)//If the address is not 0

if (*p)//If the value referred to is true

Exercise 2.23

Q: Given the pointer p, can you tell if it points to a legitimate object?

A: No, if you interpret the pointer as an address on an envelope, there is no way to guarantee that the address you fill in must be populated. (Someone else's answer)

My understanding, given a pointer, first to determine whether the pointer is valid (not NULL, address legal, etc.), as if there is no way to judge the legitimate object, it may be found in the use of the process.

Exercise 2.24

Q: In the code below, why is P legal and LP illegal?

A:int i = 42;

void *p = &i; Because the void pointer type can hold the address of any object

Long *LP = &i; But a long pointer can only hold the address of a long object.

Exercise 2.25

Q: Describes the types and values of the following variables.

A: (1) int* IP, i, &r = i; IP is an int pointer type, I is an int, R is a reference type and initialized to I.

(2) int i, *p = 0; I is of type int, p is int pointer type, initialized to 0, not pointing to any object

(3) int* IP, ip2; IP is an int pointer type, IP2 is an int type

Exercise 2.26

Q: Which of the following sentences are legal, explain why.

A: (1) const int BUF; Illegal, declaring a const constant must be initialized at the same time

(2) int cnt = 0; Legal, declaring and initializing an int variable

(3) const int SZ = CNT; Valid, declares an int const constant, and initializes it.

(4) + + cnt; ++sz; Not valid, SZ is a constant and cannot be operated by + +.

Exercise 2.27

Q: Which of the following initialization is legal, explain why.

A: (1) int i =-1, &r = 0; Illegal, R is a reference, and initialization can only point to an object.

(2) int *const P2 = &i2; Legal, define a constant pointer of type int, initialize the address of i2, then the value of the pointer can not be changed again

(3) const int i =-1, &r = 0; Illegal, R is a reference. But the const int &r = 0; is legal.

(4) const int* Const P3 = &i2; Legal, p3 value can not change, *P3 can not change

(5) const int* P1 = &i2; Legal, pointer constant, P1 point value cannot be changed

(6) const int& const R2; Not valid, references cannot be const

(7) const int i2 = i, &r = i; Legal

Exercise 2.28

Q: Which of the following is legal, please specify.

A: (1) int i, *const CP; Illegal, define const type pointer to initialize, CP

(2) int *p1, *const p2; Illegal, ibid., p2 should be initialized.

(3) const int IC, &R = IC; Illegal, the IC is a const type and must be initialized

(4) const int *CONST p3; Illegal, P3 needs to be initialized

(5) const int *p; Legal, pointing is constant, but the value of the pointer is variable.

Exercise 2.29

Q: If you already have the variables defined in the previous question, which of the following is legal?

A: (1) i = IC; Legal

(2) P1 = p3; Illegal, int* cannot be initialized with const int*

(3) P1 = &ic; Legal

(4) P3 = &ic; Illegal, the value of P3 and the value that P3 points to cannot be changed

(5) P2 = p1; Illegal, the value of P2 cannot be changed

(6) IC = *P3; Illegal, IC is constant, cannot be changed

Exercise 2.30

Q: For the following statement, please indicate whether the object is declared as a top-level const or the underlying const.

A: (1) const int v2 = 0; int v1 = v2; V2 is the top-level const

(2) int *p1 = &v1, &r1 = v1; Non-const

(3) const int *P2 = &V2, *const p3 = &i, &r2 = v2; P2 is the bottom const,p3 is the bottom, P3 front is the top-level const, R2 is the underlying const.

Exercise 2.31

Q: Assuming that the variables in the above question are defined, determine which of the following statements is legal.

A: (1) r1 = v2; Legal, reference change value

(2) p1 = p2; P2 = p1; Illegal, p2 is the underlying const, the assignment object must also have the underlying const, p2 = p1 Legal

(3) P1 = p3; P2 = p3; Illegal, p3 is the underlying const, p2 = P3 legal.

Summary: The top-level const can be assigned to a value operation. However, the underlying const has limitations, and the objects copied into the copy must have the same underlying const.

Exercise 2.32

Q: Is the code below legal? explain why.

A:int NULL = 0, *p = null; int *p cannot be initialized with the int type. expected int null = 0, *p = &null/int *p = nullptr

Exercise 2.33

Q: Use the variables defined in this section to determine the running results of the following statements.

A: (1) a = 42; b = 42; c = 42; A, B, and C have values of 42

(2) d = 42; e = 42; g = 42; D:error, D is an int * type

E:error, E for int* type

G:error, G is a const int type and can no longer be assigned a value

Exercise 2.34

Q: Prove that the above inference is correct, write a program.

A:int i = 0, &r = i;

Auto A = R;

const int CI = i, &cr = CI;

Auto B = ci;

Auto C = CR;

Auto D = &i;

Auto E = &ci;

Auto &g = CI;

A = 42;

b = 42;

c = 42;

Std::cout << a << b << c << Std::endl;

>> d = 42;

>> e = 42;

>> g = 42;

Exercise 2.35

Q: Determine the type defined below and write program validation.

a:const int i = n;//I is a const int type

Auto J = i; j is of type int

Auto &k = i; K is a const int& type

Auto *p = &i; P is a const int* type

Const Auto J2 = i, &k2 = i; J2 is a const int type and K2 is a const int& type.

Exercise 2.36

Q: For the following code, indicate the value of each variable and the value after the end of the program.

A:int A = 3, b = 4; //

Decltype (a) c = A; c is of type int, and a is the initial value of C

Decltype ((b)) d = A; D is the int& type and is a reference

++c; The value after ++c is 4;

++d; The value after ++d is 4

Exercise 2.37

Q: Assignment is a class of typical expressions that generate references, and the type of reference is the type of the left value. That is, if I is an int, the type of the expression i=x is int&. The type of the following variables is indicated according to this feature.

A:int A = 3, b = 4; A, b are int

Decltype (a) c = A; C is int

Decltype (a = b) d = A; D is int&, is a reference.

Exercise 2.38

Q: Illustrate the difference between the Decltype specified type and auto.

A: The same point: the type is specified by the type of the known variable or expression. such as: int i = 2; auto J = i; Decltype (i) j = i; The two are the same.

Different points: (1) auto ignores the top-level const, but Decltype does not.

(2) The auto definition variable must have an initial value, but decltyple does not necessarily

Exercise 2.39

Q:

A:

Exercise 2.40

Q:

A:

Exercise 2.41

Q:

A:

Exercise 2.42

Q:

A:

C + + Primer Fifth Edition exercise answer chapter II

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.