文档详情

c++小程序搜集(国外英文资料).doc

发布:2017-06-07约1.02万字共24页下载文档
文本预览下载声明
c++小程序搜集(国外英文资料) C + + small program collection Question 1. Analyze the execution results of the following program # include iostream. H Void swap (int x, int y) { Int temp. Temp = x; X = y; Y = temp; } Void main () { Int x = 10, y = 20; Swap (x, y); The cout } Solution: The function here USES a reference call, so the output is x = 20, and y = 10 Note: in a function call, the invocation of a reference is the same effect as the call to the address, but more succinctly and intuitively. -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- Question 2. Analyze the execution results of the following program # include iostream. H Void main () { Int a [] = {10, 20, 30, 40}. Int * pb = pa; Pb++; Cout. } Solution: Pa is the pointer to the array, first pointing to a [0], pb is the reference of pa, and when the pb+ is executed, the pa points to a [1], so the output is: 20 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - Question 3. Analyze the execution results of the following program # include iostream. H The class Sample { Int x; Public: The Sample () {}; Sample int a. } Sample a {x = a.x + + 10; } Void disp (). } }; Void main () { Sample s1 (2), s2 (s1); S1. Disp (); S2. Disp (); } Solution: Sample class Sample (Sample a) a copy constructor is a constructor, add a object x 1 then add 10 after the object is assigned to the current x, because is a reference object, so the output is: X is equal to 3 over/plus X is equal to 12 over 2 plus 10 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- Question 4. Analyze the execution r
显示全部
相似文档