函数增强部分-课后任务
我要参与
函数增强部分-课后任务
学习任务 1.5k
等88人参与
来源: 第17周 / 物联网/嵌入式工程师

任务

  1. 观察如下代码,写出两个不同的重载函数,完成浮点数和字符串操作
int my_swap(int a,int b);
  • 1
代码块
复制 预览
复制成功!
  1. 找出如下代码中的错误,并修复它
#include <iostream>

int main(void)
{
    cout << calc(,100,200) << endl;
    return 0;
}

int calc(int a = 10,int b,int c)
{
    return (a + b + c);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
代码块
复制 预览
复制成功!
去发布

登录后即可发布作业,立即

我的作业

全部作业 88

北城半夏4806197
#include<iostream>

using namespace std;


int my_swap(int &a,int &b)
{
	int t = a;
	a = b;
	b = t;
}

int my_swap(float &a,float &b)
{
	float t = a;
	a = b;
	b = t;
}

int my_swap(string &a,string &b)
{
	string t = a;
	a = b;
	b = t;
}

int main()
{
	int i1 = 10,i2 = 20;
	my_swap(i1,i2);
	cout << "i1 = " << i1 <<", i2 = " << i2 << endl;

	float f1 = 12.1,f2 = 21.2;
	my_swap(f1,f2);
	cout << "f1 = " << f1 <<", f2 = " << f2 << endl;
	
	string s1 = "hello",s2 = "world";
	my_swap(s1,s2);
	cout << "s1 = " << s1 << ",s2 = " << s2 << endl;

	return 0;
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
代码块
复制 预览
复制成功!

图片描述
错误:需要对calc函数进行一个声明,并且int a给了默认值后,b和c都必须有默认值。缺少using namespace std

#include <iostream>

using namespace std;

extern int calc(int a = 10,int b=1,int c=3);


int main(void)
{
    cout << calc(10,100,200) << endl;
    return 0;
}

int calc(int a,int b,int c)
{
    return (a + b + c);
}


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
代码块
复制 预览
复制成功!
提交于  2025-05-16 21:36:53
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师