【无标题】

第二章  引用内联重载
一.选择题
   1、适宜采用inline定义函数情况是(C)
A. 函数体含有循环语句
B. 函数体含有递归语句
C. 函数代码少、频繁调用
D. 函数代码多、不常调用substr
   2、假定一个函数为A(int i=4, int j=0) {;}, 则执行“A (1);”语句后,i和j的值分别为(A)
A. 10
B. 14
C. 40
D. 41
3、下列有关重载函数的说法中正确的是(C)
A. 重载函数必须具有不同的返回值类型
B. 重载函数参数个数必须相同
C. 重载函数必须有不同的形参列表
D. 重载函数名可以不同
 4、两个同名函数构成重载关系,以下那个选项不要求不同。C
A. 参数个数
B. 返回类型
C. 参数类型
D. 函数头后有无const
 
二.填空题
1、C++语言中如果调用函数时,需要改变实参或者返回多个值,应该采取___引用____方式
2、执行下列代码
string    str("HelloC++");
cout<<str.(53);
程序的输出结果是__C++_
3、下面是一个输入半径,输出其面积和周长的C++程序,在下划线处填上正确的语句。
#include <iostream>
#define  pi   3.14
_________;
int main()
{ double rad;
cout<<"rad=";
cin>>rad;

double l=2.0*pi*rad;
double s=pi*rad*rad;
cout<<"\n The long is:"<<l<<endl;
cout<<"The area is:"<<s<<endl;}
4、程序实现大写字母转换成小写字母。
#include <iostream.h>
int main()
{ char a;
__cout << "请输入一个大写字母" << endl;_____;
cin>>a;
int i=32;
if( a>='A'||a<='Z')
a=a+i;
cout<<a<<endl;
}
5、执行下列代码
int i=230;
cout <<"i="<<hex <<i<<endl;
程序的输出结果为_i=0XE6__。
三、编程题
3.1   有以下重载函数定义:
        void  f()void  f(int x);
        void  f(int m, int  n);
        void  f(double  d1,  double  d2=3.14);

      则以下调用哪些是错误的,哪些可以并与那个函数匹配,编程验证
         f(‘A’);调用f(int x) x=65;
         f( 5 ) ;调用f(int x) x=5;
         f( 5.5 ); 调用f(double d1,double d2):d1=5.5, d2=3.14使用默认参数
         f(10, 20);调用f(int m,int n):m=10,n=20;
         f(10, 23.4):没有匹配函数
         f(10.8, 24.87):调用f(double d1,double d2):d1=10.8,d2=24.87;
 3.2   创建一个函数plus(),它把两个数值加在一起,返回它们的和,提供处理int、doble和string类型的重载版本,测试它们是否能处理下面的调用
        int   n = plus(3,4);
        double  d = plus(3.2,4.2);
            string s = plus(“he”, “llo”);
            string s1 = “aaa” ;  string s2 = “bbb”;
            string s3 = plus(s1,s2);
            思考:(1)给string版本的函数传送参数最有效的方式是什么?引用
                 (2double  d = plus(3, 4.2)能否调用 ?不能


#include <iostream>

using namespace std;
void  f(){
    cout<<"f1"<<endl;
};
void  f(int x){
    cout<<"f2"<<endl;
    cout<<"x="<<x<<endl;
};
void  f(int m, int  n){
    cout<<"f3"<<endl;
    cout<<"m="<<m<<" n="<<n<<endl;
};
void  f(double  d1,  double  d2=3.14){
    cout<<"f4"<<endl;
    cout<<"d1="<<d1<<" d2="<<d2<<endl;
};

int main()
{
    f('A');
    cout<<"============="<<endl;
    f(5);
    cout<<"==========="<<endl;
    f(5.5);
    cout<<"=============="<<endl;
    f(10,20);
    cout<<"==========="<<endl;
//    f(10,23.4);
    cout<<"============"<<endl;
    f(10.8,24.87);
    cout<<"============"<<endl;


    return 0;
}

#include <iostream>

using namespace std;
int plus(int a,int b){
    return a+b;
}
double plus(double a, double b){
    return a+b;
}
string plus(string &a,string &b){
    return a+b;
}
int main()
{
//都不行
    int   n = plus(3,4);
    double  d = plus(3.2,4.2);
    string s = plus(“he”, “llo”);
    string s1 = “aaa” ;  string s2 = “bbb”;
    string s3 = plus(s1,s2);
    return 0;
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值