华清远见25072班C++学习假期10.3作业

1、

第二章 引用内联重载

一.选择题

1、适宜采用inline定义函数情况是(C)

A. 函数体含有循环语句

B. 函数体含有递归语句

C. 函数代码少、频繁调用

D. 函数代码多、不常调用

2、假定一个函数为A(int i=4, int j=0) {;}, 则执行"A (1);"语句后,i和j的值分别为(A)

A. 1和0

B. 1和4

C. 4和0

D. 4和1

3、下列有关重载函数的说法中正确的是(C)

A. 重载函数必须具有不同的返回值类型

B. 重载函数参数个数必须相同

C. 重载函数必须有不同的形参列表

D. 重载函数名可以不同

4、两个同名函数构成重载关系,以下那个选项不要求不同。B

A. 参数个数

B. 返回类型

C. 参数类型

D. 函数头后有无const

二.填空题

1、C++语言中如果调用函数时,需要改变实参或者返回多个值,应该采取___引用____方式

2、执行下列代码

string str("HelloC++");

cout<<str.substr(5,3);

程序的输出结果是__C++_

3、下面是一个输入半径,输出其面积和周长的C++程序,在下划线处填上正确的语句。

#include <iostream>

#define pi 3.14

using namespace std_____;

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;

_int i=32;

cin>>a;

if(a>='A'&&a<='Z'___)

a=a+i;

cout<<a<<endl;

}

5、执行下列代码

int i=230;

cout <<"i="<<hex <<i<<endl;

程序的输出结果为__i=e6_。

三、编程题

3.1 有以下重载函数定义:

void f();

void f(int x);

void f(int m, int n);

void f(double d1, double d2=3.14);

则以下调用哪些是错误的,哪些可以并与那个函数匹配,编程验证

f('A'); void f(int x);

f( 5 ) ; void f(int x);

f( 5.5 ); void f(double d1, double d2=3.14);

f(10, 20); void f(int m, int n);

f(10, 23.4) 错误!无匹配函数

f(10.8, 24.87) void f(double d1, double d2=3.14);

cpp 复制代码
#include <iostream>
using namespace std;

// 重载函数定义
void f() { cout << "调用 f()" << endl; }
void f(int x) { cout << "调用 f(int x),x=" << x << endl; }
void f(int m, int n) { cout << "调用 f(int m, int n),m=" << m << ", n=" << n << endl; }
void f(double d1, double d2=3.14) { cout << "调用 f(double d1, double d2),d1=" << d1 << ", d2=" << d2 << endl; }

int main() {
    f('A');       // 匹配 f(int x),x=65
    f(5);         // 匹配 f(int x),x=5
    f(5.5);       // 匹配 f(double, double),d1=5.5, d2=3.14
    f(10, 20);    // 匹配 f(int, int),m=10, n=20
    //f(10, 23.4); // 编译报错:无匹配函数
    f(10.8, 24.87); // 匹配 f(double, double),d1=10.8, d2=24.87
    return 0;
}

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);

cpp 复制代码
#include <iostream>
#include <string>
int plus(int a, int b)
{
    return a + b;
}

double plus(double a, double b)
{
    return a + b;
}
std::string plus(const std::string &a, const std::string &b)
{
    return a + b;
}
int main()
{
    int n = plus(3, 4);
    std::cout << "int plus: " << n << std::endl;
    std::cout << "==============================" <<std::endl;
    double d = plus(3.2, 4.2);
    std::cout << "double plus: " << d << std::endl;
    std::cout << "==============================" <<std::endl;
    std::string s = plus("he", "llo");
    std::cout << "string plus(字面量): " << s << std::endl;
    std::cout << "==============================" <<std::endl;
    std::string s1 = "aaa";
    std::string s2 = "bbb";
    std::string s3 = plus(s1, s2);
    std::cout << "string plus(对象): " << s3 << std::endl;
    return 0;
}

思考:

(1)给string版本的函数传送参数最有效的方式是什么?

答:使用常量引用的方式,在前面加上一个const防止意外修改实参

(2)double d = plus(3, 4.2)能否调用 ?

答:能调用,C++会进行隐式类型的转换将int 3转换成double 3.0然后与double 4.2进行运算​​​​​​​

2、牛客网

相关推荐
Hard but lovely4 小时前
C++---》stl : pair 从使用到模拟实现
c++·后端
林中青木4 小时前
读写INI文件源码(点击关注)
c++·ini文件读取
running thunderbolt5 小时前
项目---网络通信组件JsonRpc
linux·服务器·c语言·开发语言·网络·c++·性能优化
青岛少儿编程-王老师5 小时前
CCF编程能力等级认证GESP—C++6级—20250927
java·c++·算法
ajassi20006 小时前
开源 C++ QT QML 开发(十)通讯--串口
c++·qt·开源
Janspran6 小时前
监控系统2 - framebuffer
c++
代码AC不AC6 小时前
【C++】AVL树的模拟实现
c++·avl树·底层原理
_w_z_j_6 小时前
C++----bitmap位图的使用
开发语言·c++
BingeBlog6 小时前
[01] Qt的UI框架选择和对比
开发语言·c++·笔记·qt·ui·开源软件