Chapter 2 Exercises

2.9、

  (A) illegal, variables to be defined before entering

  (B) illegal, there is the risk of loss of information

  (C) illegal, wage undefined

  (D) is converted to an integer

 

2.10

  global_str: empty string

  global_int : 0

  local_int: garbage value

  local_str: garbage value

 

2.11

(A) the definition statement +

(B) the definition of declaration +

(C) statement

 

2.12

(A) double keyword

(B) legal

(C) can not occur '-'

(D) does not begin with a number

(E) legal but does not meet the naming conventions

 

 

2.13

100

 

2.14

Legal: 10045

 

2.15

a legitimate, converted

b illegal, can not bind a literal reference

c legitimate

d not legitimate, reference must be initialized

 

2.16

a legitimate, assigned to d

b legitimate, assigned to the i d

c legitimate, assigned to the d i, the conversion will occur

d legal, assigned to the d i, will warn

 

2.17

10 10

 

2.18

#include <iostream>
int main()
{
 int value1 = 5, value2 = 10;
 int* pvalue1 = &value1;
 std::cout << "pvalue1 = " << pvalue1 << std::endl;
 std::cout << "*pvalue1 = " << *pvalue1 << std::endl;
 std::cout << "value1 = " << value1 << std::endl;
 *pvalue1 = 6;
 std::cout << "*pvalue1 = 6 : " << std::endl;
 std::cout << "pvalue1 = " << pvalue1 << std::endl;
 std::cout << "*pvalue1 = " << *pvalue1 << std::endl;
 std::cout << "value1 = " << value1 << std::endl;
 pvalue1 = &value2;
 std::cout << "pvalue1 = &value2 : " << std::endl;
 std::cout << "pvalue1 = " << pvalue1 << std::endl;
 std::cout << "*pvalue1 = " << *pvalue1 << std::endl;
 std::cout << "value1 = " << value1 << std::endl;
 return 0;
}
 
2.19、
1) The reference itself is not an object, a pointer to the object itself is
2) reference must be defined initial value, not after unbundling. Pointer can not be given initial value, the program is running you can change the position of the pointer
 
2.20、
Int type variable i is defined and initialized 42;
Int type pointer p pointing to defined points and variable I;
Dereference operators use to access the object and the object pointed to by the multiplication;
 
2.21、
a) is not legitimate, does not match the type b) is not legitimate, does not match the type c) legitimate
 
2.22、
True, if the pointer is not a null pointer, and false otherwise
If the object pointed to the pointer p is not 0 True, the object pointed to is 0 and false
 
2.23、
Can not,
 
2.24
Type mismatch
 
2.25
a) ip: int type pointer i: int type variable (an object); b) i: the object type int ip: int type pointer and initialized to null pointer c) ip is an int type pointer ip2 is of type int objects
 
2.26
a) illegal: const object must be initialized when declared
b) legal
c) legal
d) not legal; sz operation is const object can not be changed its own value
 
2.27
a) not valid: the initial value must be a reference to an object type
b) If i2 is an object of type int is legitimate
c) method, references initialization constants may be used as an initial value any expression
d) with B);
e) the same b);
f) not legal, the object itself is not a reference can not be defined with the const
g) If i can be converted to an int is legitimate
 
2.28、
a) illegal, define const object must be initialized
b) illegal, with a)
c) not legally, with a)
d) illegal, with a)
e) declare a pointer of type int
 
2.29
a) legitimate, constant amount assigned to the very legitimate
b) illegal, p3 not only that it is constant and constant directivity
c) illegal, ic is constant
d) illegal, p3 itself is const object can only be initiated when the assignment
e) illegal, p2 itself is const object can only be initiated when the assignment
f) illegal, ic is constant
 
2.30
v2 top const, v1 no const, p1 no const & r1 not const,
p2 underlying const, p3 const both top and bottom is const, & r2 is the bottom const
 
2.31
r1 = v2; legitimate
p1 = p2; illegal,
p2 = p1; legal,
p1 = p3; illegal
p2 = p3; legal
 
2.32
Illegal: * p = nullptr, (can not assign an int pointer)
 
2.33
a = 42; // a result of 42 is assigned to an int a
b = 42; // b 42 is assigned to the result Int type b
c = 42; // supra
d = 42; // illegal, d is an integer pointer
e = 42; // illegal, e is an integer constant pointer
g = 42; // illegal, g is an integer constant reference
 
2.34
#include <iostream>
using namespace std;

int main ()
{
    int i = 0, &r = i;
    auto a = r; // a is an integer (r is an alias of i, and i is an integer)
    const int ci = i, &cr = ci;
    auto b = ci; // b is an integer (const top ci characteristic is ignored)
    auto c = cr; // c is an integer (cr is an alias ci, ci itself is a top const)
    auto d = & i; // d is a pointer to an integer (integer address is a pointer to an integer)
    auto e = & ci; // e is an integer constant pointer (address of the underlying object is a constant const)
    const auto f = ci; // ci is deduced type int, f is const int
    auto & g = ci; // g is an integer constant reference bound to ci
    
    a = 42;
    cout << "a = " << a << endl;
    b = 42;
     cout << "b = " << b << endl;
    c = 42;
     cout << "c = " << c << endl;
    d = 42; // error: not assign a value "int" type to "int *" type of entity
     cout << "d = " << d << endl;
    e = 42; // error: not assign a value "int" type to "const int *" type of entity
     cout << "e = " << e << endl;
    g = 42; // given: g is an integer constant reference ci bind to modified expression must be left value
     cout << "g = " << g << endl;

     return 0;

}
 
 
2.35
const int i = 42;
auto j = i; // j is an integer (top const i is ignored)
const auto & k = i; // k is an integer constant reference bound to i
auto * p = & i // p is a pointer to a pointer integer constant
const auto j2 = i, & k2 = i; // j2 is an integer constant, k2 is a reference to integer constants
Code:
#include <iostream>
using namespace std;

int main ()
{
    const int i = 42;
    auto j = i;
    const auto &k = i;
    auto *p = &i;
    const auto j2 = i, &k2 = i;

    cout << "i = " << i << endl;
    cout << "j = " << j << endl;
    cout << "k = " << k << endl;
    cout << "p = " << p << endl;
    cout << "j2 = " << j2 << endl;
    cout << "k2 = " << k2 << endl;

    return 0;
}
operation result:

 

 2.36

a is an int 

b is an int

c is an int

d is a reference type int

Value: a == 4 b == 4 c == 4 d == 4 

 

2.37

a int type 3

b int type 4

c int type 3

d int type reference 3 // decltype (a = b) is not operational, the value of a constant

 

2.38

// I did not quite get to know these two things

2.39

Tip lack;

 

2.40

struct Sales_data

{

  std::string bookNo;

  std::string author;

  int sold = 0;

  double revenue = 0.0;

};

 

 

Guess you like

Origin www.cnblogs.com/xiaogaogao/p/11731140.html