当前位置:文档之家› C++Primer中文版_第4版_第七章_函数_习题解答_文字word版

C++Primer中文版_第4版_第七章_函数_习题解答_文字word版

第七章函数题目00What is the difference between a parameter and an argument?形参和实参有什么区别?【解答】形参是在函数定义的形参表中进行定义,是一个变量,其作用域为整个函数。

而实参出现在函数调用中,是一个表达式。

进行函数调用时,用传递给函数的实参对形参进行初始化。

题目01Indicate which of the following functions are in error and why. Suggesthow you might correct the problems.下列哪些函数是错误的?为什么?请给出修改意见。

(a) int f() {string s;// ...return s;}(b) f2(int i) { /* ... */ }(c) int calc(int v1, int v1) /* ... */ }(d) double square(double x) return x * x;【解答】(a)是错误的。

因为函数头中所定义的返回值类型为int,return语句世纪返回的表达式的类型为string,两个类型不同,而string类型又不能隐式转换为int类型。

可修改为:string f(){string s;//…Return s;}(b)是错误的。

因为该函数定义中没有指定返回类型,在标准C++中,定义函数时不指定返回类型是非法的。

可修改为:Int f2(int i){/*…*/}(c)是错误的。

缺少括住函数体在左花括号,而且两个形参不应该同名。

可修改为:Int caic(int v1,intv2){/*…*/}(d)是错误的。

缺少括住函数体的一对花括号。

可修改为:Double square(double x){return x*x;}题目02Write a program to take two int parameters and generate the result ofraising the first parameter to the power of the second. Write a programto call your function passing it two ints. Verify the result.编写一个带有两个int 型形参的函数,产生第一个参数的第二个参数次幂的值。

编写程序传递两个int 数值调用该函数,请检验其结果。

【解答】//7-3.cpp//函数Power带有两个int型形参,产生第一个参数的第二个参数次幂的值。

//主函数传递两个int型数值调用power函数#include<iostream>Using namespace std;Int power(int x,inty)//该函数返回x的y次幂{Int result=1;For (int loop=1;loop<=y;++loop)Result *=x;Return result;}Int main(){Int xval,yval;Cout<<”enter two integers(the second one should be equal to or bigger than 0):”<<endl;Cin>>xval>>yval;If(yval<0){Cout<<”the second integer should be equal to or bigger than 0”<<endl;Return 1;}Cout<<”result of raising ”xval<<””to the power of <<yval<<”is ”<<power(xval,yval)<<endl;Return 0;}注意,当输入的证书较大时,该power函数的计算结果容易溢出。

题目03Write a program to return the absolute value of its parameter.编写一个函数,返回其形参的绝对值。

【解答】可编写如下abs函数,返回形参x的绝对值:Int abs(int x){Retrun x>0?x:-x;}题目04Write a function that takes an int and a pointer to an int and returnsthe larger of the int value of the value to which the pointer points.What type should you use for the pointer?编写一个函数,该函数具有两个形参,分别为int 型和指向int 型的指针,并返回这两个int 值之中较大的数值。

考虑应将其指针形参定义为什么类型?【解答】函数代码如下:Int getBigger(int x,const int*y){Return x>*y ? x: *y;}该函数无需修改指针形参所指向的值,因此,为了保护指针形参所指向的值,将指针形参定义为指向const对象的指针。

题目05Write a function to swap the values pointed to by two pointers to int.Test the function by calling it and printing the swapped values.编写函数交换两个int 型指针所指向的值,调用并检验该函数,输出交换后的值。

【解答】//7-6.cpp//函数swap交换两个int型指针所指向的值。

//主函数调用swap函数,输出交换后的值#include<iostream>Using namespace std;Void swap(int *x,int *y)//该函数交换x和y所指向的值{Int temp;Temp=*x;*x=*y;*y=temp;}Int main(){Int xval,yval;Cout<<”enter two integers:”<<endl;Cin>>xval>>yval;Cout<<”before swapped”<<”x= ”<<xval<<”y= ”<<yval<<endl;Swap(&xval,&yval);Cout<<”after swapped:”<<”x=”<<xval<<”y=”<<yval<<endl;Return 0;}题目06Explain the difference in the following two parameter declarations:解释下面两个形参声明的不同之处:void f(T);void f(T&);【解答】前者声明的时T类型的形参。

在f中修改形参的值不会影响调用f时所传递的实参的值。

后者声明的时T类型的引用形参。

在f中修改形参的值世纪上相当于修改待用f时所传递的实参的值。

题目07Give an example of when a parameter should be a reference type. Give anexample of when a parameter should not be a reference.举一个例子说明什么时候应该将形参定义为引用类型。

再举一个例子说明什么时候不应该将形参定义为引用。

【解答】如果希望通过函数调用修改实参的值,就应该将形参定义为引用类型。

例如,用swap函数交换两数的值。

如果不将形参定义为指针类型,则需要直接修改实参的值,应该将形参定义为引用类型。

Void swap(int &v1,int &v2){Int tmp=v2;V2=v1;V1=temp;}除了swap函数这种情况外,为了通过一次函数调用获得多个结果值,也可以使用引用形参。

另外,在向函数传递大型对象时,为了避免复制实参以提高效率,以及使用无法复制的类类型(其复制构造函数为private的类类型)座位形参类型时,也应该将形参定义为引用类型。

但这是使用形参的目的是为了避免复制实参,所以应该将实参定义为const引用。

如果不需要通过函数调用修改实参的值,就不应该将形参定义为引用类型。

例如,在求绝对值的函数中,形参就不宜定义为引用类型。

题目08Change the declaration of occurs in the parameter list of find_val(defined on page 234) to be a nonreference argument type and rerun theprogram. How does the behavior of the program change?将第7.2.2 节定义的find_val 函数的形参表中occurs 的声明修改为非引用参数类型,并重新执行这个程序,该函数的行为发生了什么改变?【解答】调用该函数后,ctr的值将不变(保持调用该函数之前的原值,不再能反映42出现的次数)。

因为在函数体中修改的时形参occurs(即cr的局部副本),对实参ctr不产生影响。

题目09The following program, although legal, is less useful than it might be.Identify and correct the limitation on this program:下面的程序虽然是合法的,但可用性还不够好,指出并改正该程序的局限:bool test(string& s) { return s.empty(); }【解答】其局限在于:此处使用引用形参的唯一目的是避免复制实参,但没有将形参定义为const引用,从而导致不能使用字符串字面值来调用该函数(因为非const引用形参只能与完全同类型的非const对象关联)。

可更正为:Bool test(const string &s){ return s.empty();}题目10When should reference parameters be const? What problems might arise ifwe make a parameter a plain reference when it could be a const reference?何时应将引用形参定义为const 对象?如果在需要const 引用时,将形参定义为普通引用,则会出现什么问题?【解答】如果使用引用形参的唯一目的是避免复制实参,则应将应用形参定义为const对象。

相关主题