华清远见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、牛客网

相关推荐
杨筱毅1 小时前
【C++】【常见面试题】最简版带大小和超时限制的LRU缓存实现
c++·面试
陌路202 小时前
C23构造函数与析构函数
开发语言·c++
_OP_CHEN2 小时前
C++进阶:(二)多态的深度解析
开发语言·c++·多态·抽象类·虚函数·多态的底层原理·多态面试题
金色熊族4 小时前
装饰器模式(c++版)
开发语言·c++·设计模式·装饰器模式
Dream it possible!4 小时前
LeetCode 面试经典 150_链表_旋转链表(64_61_C++_中等)
c++·leetcode·链表·面试
CS创新实验室7 小时前
典型算法题解:长度最小的子数组
数据结构·c++·算法·考研408
我有一些感想……7 小时前
浅谈 BSGS(Baby-Step Giant-Step 大步小步)算法
c++·算法·数论·离散对数·bsgs
j_xxx404_7 小时前
C++ STL:string类(3)|operations|string类模拟实现|附源码
开发语言·c++
Elias不吃糖9 小时前
Linux 环境适应 Day 1 全面笔记
linux·c++·笔记
无限进步_10 小时前
C语言字符串连接实现详解:掌握自定义strcat函数
c语言·开发语言·c++·后端·算法·visual studio