Chapter 2 Exercises

Exercise 2

1. Explanation of terms

Reference: The so-called reference is to give an object an alias, and use the alias to access the object. In other words, the new object and the original object share the same address.

Inline function: An inline function is a function declared using the inline keyword.

Overloaded functions: Overloaded functions are often used to give a common name to operations that have similar behavior but different data types.

2. Fill in the blanks

(1) In general, programs written in C++ language are composed of functions and classes .  

(2) C++ has two kinds of comment symbols, one is // and the other is /*,,,,*/ .

(3) To use C++ style input and output, the header file iostream must be included in the program .

(4) cin is a predefined standard input stream object, >> is an input operator, also known as an extraction operator.

(5) cout is a predefined standard output stream object, << is an output operator, also known as an insertion operator.

(6) The value of a pointer is the address value of the object it points to . The type of a pointer is the type of the object it points to . The content of a pointer is the value of the object it points to .

(7) C++ uses the operator & to define a reference, and the access to the reference is the access to the object it refers to .

(8) When a function call appears before the function definition, the function must be declared with the function prototype first .

(9) C++ has two parameter passing mechanisms: pass-by -value and pass- by - reference .

(10) A function declared with the keyword inline is called an inline function.

(11) Operator new is used for dynamic memory allocation, and operator delete is used to release dynamically allocated memory.

(12) The output of the following program is x=10, y=10 ; x=100, y=100

#include<iostream>
using namespace std;
int main()
{
   int x=10,&y=x;
   cout<<"x="<<x<<",y="<<y<<endl;
   int *p=&y;
   *p=100;
   cout<<"x="<<x<<",y="<<y<<endl;
   return 0;
}

3. Multiple-choice questions (choose at least one, you can choose more than one)

(1) In the definition of integer pointer variables p2 and p3, the error is (A).

A .int p1,*p2,p3;     B  .int *p2,p1,*p3;      C .int p1,*p2=&p1,*p3;        D.int *p2,p1,*p3=&p1;

(2) If there is a definition "double xx=3.14,*pp=&xx;", then *pp is equivalent to (C).

A .&xx                   B .*xx                        C.3.14                               D.xx

(3) (C) in the description of the reference below is wrong.

A. A reference is an alias for a variable or object B. When creating a reference, initialize it     

C. A variable of any type can be used for reference initialization D. A reference has the same address as the object it represents

(4) When the function does not return a value, (A) function type should be selected.

A.void B.int C. Not sure D.float

(5) In the definition format of the function, in the following components, (D) can be omitted.

A. Function name B. Function body C. Return value type D. Function parameters

(6) For overloaded functions, the following statement is incorrect (D).

A. The types of parameters are different B. The order of parameters is different C. The number of parameters is different D. The number, type and order of parameters are the same, but the return value type of the function is different

(7) Among the following descriptions about setting default values ​​of function parameters, (D) is correct.

A. There is no rule on the order of setting default values ​​of function parameters B. Default values ​​cannot be set when a function has one parameter

C. Default parameters should be set in the prototype of the function, but not in the definition statement of the function D. Expressions can be used to set default parameters, but global variables are not available in expressions

(8) The following statement is correct (BC).

A. All functions can be declared as inline functions       

B. Functions with loop statements and switch statements cannot be declared as inline functions

C. Using inline functions can speed up the execution of the program, but it will increase the size of the program code

D. Using inline functions can reduce program code size, but slow down program execution

(9) If a function is not too complicated, but needs to be called frequently, (A) should be selected.

A. Inline function B. Overloaded function C. Recursive function D. Nested function

(10) C++ has made many improvements to the C language. The following description makes the C language undergo a qualitative change, that is, it is (D) that has changed from process-oriented to object-oriented.

A. Added some new operators B. Allows function overloading and allows to set default parameters

C. stipulates that function declarations must use prototypes D. introduces the concept of classes and objects

Fourth, judgment questions 

(1) In a C++ program, no variable without definition or description shall be used. (right) 

(2) When using const to describe a constant, it is not necessary to indicate the type. (wrong)

(3) References can be initialized with arbitrary variables when they are created. (wrong)

(4) A calling function that returns a reference can be used as an lvalue. (right)

(5) The function can have no parameters or no return value. (right)

(6) Two functions without parameters cannot be overloaded. (right)

(7) Functions can set default parameters, but it is not allowed to set all parameters of a function as default parameters. (right)

(8) The space allocated by operator new is released by operator delete. (right)

5. Short answer questions

(1) What is the definition of namespace?

A: Namespaces are used to prevent naming conflicts.

(2) What is the use of citations?

Answer: In addition to independent references, in C++ programs, the main use of references is as function parameters and function return values.

(3) Compare the similarities and differences between direct calls and reference calls.

Answer: In the pass-by-value mechanism, the value of an expression that is an actual parameter is copied into an object identified by the corresponding parameter name as the initial value of the parameter. The access and modification of the formal parameters of the function body are all operated on this identification object, regardless of the actual parameters, that is, the transmission of data is one-way. When using a reference as a formal parameter of a function, use the variable name for the actual parameter of the calling function. The actual parameter is passed to the formal parameter, which is equivalent to the operation of the formal parameter in the called function. The essence is the direct operation of the actual parameter, that is, the transmission of data is bidirectional.

(4) What is the function of inline function? What are its characteristics?

Answer: An inline function is a function declared using the inline keyword. When the program is compiled, the compilation system replaces the place where the inline function call occurs in the program with the function body, thereby reducing the time overhead.

      Note when using inline functions:

      A recursive function cannot be defined as an inline function. 

     Inline functions are generally suitable for small functions that do not contain complex structures such as switch and while and have only 1 to 5 statements. Otherwise, the compilation system regards the function as an ordinary function.

     Inline functions can only be used after they are defined, otherwise the compilation system will also treat the function as a normal function. Exception interface declarations are also not allowed for inline functions.

(5) Must the parameter name in the function prototype be the same as the parameter name in the function definition and the parameter name in the function call?

A: Not necessarily consistent. So the parameters are distinguished by position and type rather than name.

(6) What is used to distinguish when overloading functions?

A: The compilation system will determine which function to use based on the type and number of function parameters.

Six, program analysis questions (write the output of the program, and analyze the results)

#include<iostream>
using namespace std;
intmain()
{
   int num=50;
   int& ref=num;
   ref=ref+10;
   cout<<"num="<<num<<endl;
   num=num+40;
   cout<<"ref="<<ref<<endl;
   return 0;
}

The output is as follows:


The results are analyzed as follows:

   In the program, an object num of type int is defined, and an initial value of 50 is assigned to it; then a reference refv of type int is defined, and it is associated with num. ref=num=50, ref=ref+10, num=ref+10=50+10, so num=60. And num=num+40=60+40, ref=num, so ref=100.

    Seven, programming questions

Write a complete C++ program, use the system function pow(x, y) to calculate the value of x^y, pay attention to including the header file cmath.

The procedure is as follows:

//xiti2-1.cpp
#include<iostream>
#include<cmath>
using namespace std;
intmain()
{
   float x,y;
   cout<<"please input 2 floats to x,y:"<<endl;
   cin>>x>>y;
   float z=pow(x,y);
   cout<<"pow("<<x<<","<<y<<")="<<z<<endl;
   return 0;
}

The results are as follows:


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325723012&siteId=291194637