[Basic C++] function overloading -1
- 题目描述
- 答题区域
- 提交记录
- 成绩报告
- 排名情况
- 标准答案
- 相关讨论
出题
邮箱
:
:
贺子阳
1499677880@qq.com
评测
截止时间
:
:
实时评测
2018-03-27 10:00
空间限制
时间限制
:
:
32MB
1000ms
Description
写出3组Plus函数的声明与实现,类型分别为int,double,string
主函数如下,注意只需要实现Plus函数和包含相关的头文件。
int main() {
int n;
int a,b;
double c,d;
std::string str1,str2;
std::cin>>n;
while(n--)
{
std::cin>>a >> b ;
std::cin>>c >> d;
std::cin >> str1 >>str2;
std::cout <<myplus(a, b) << endl;
std::cout <<myplus(d, c) << endl;
std::cout <<myplus(str1, str2) << endl;
}
return 0;
}
Sample Input
1
2 3
2.5 3.5
women day
Sample Output
5
6
womenday
Hint
PS:please add std:: before using the string type
标准答案:
-
framework.cpp
1
#include <iostream>
2
#include <string>
3
#include "source.h"
4
using std::endl;
std::endl;
5
6
<span style="color:#75715e">// Function prototype</span>
7
<span style="color:#75715e">//int myplus(int a, int b);</span>
8
<span style="color:#75715e">//double myplus(double a, double b);</span>
9
<span style="color:#75715e">//std::string myplus(const std::string& a, const std::string& b);</span>
10
11
int main() {
main() {
12
int n;
int n;
13
int a,b;
int a,b;
14
double c,d;
double c,d;
15
std::string str1,str2;
std::string str1,str2;
16
std::cin>>n;
std::cin>>n;
17
while(n--)
while(n--)
18
{
{
19
std::cin>>a >> b ;
std::cin>>a >> b ;
20
std::cin>>c >> d;
std::cin>>c >> d;
21
std::cin >> str1 >>str2;
std::cin >> str1 >>str2;
22
std::cout <<myplus(a, b) << endl;
std::cout <<myplus(a, b) << endl;
23
std::cout <<myplus(d, c) << endl;
std::cout <<myplus(d, c) << endl;
24
std::cout <<myplus(str1, str2) << endl;
std::cout <<myplus(str1, str2) << endl;
25
}
}
26
return 0;
return 0;
27
}
28
-
source.h
-
framework.cpp
1
<span style="color:#75715e">// Problem#: 17678</span>
2
<span style="color:#75715e">// Submission#: 4634918</span>
3
<span style="color:#75715e">// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License</span>
4
<span style="color:#75715e">// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/</span>
5
<span style="color:#75715e">// All Copyright reserved by Informatic Lab of Sun Yat-sen University</span>
6
#include <iostream>
7
#include <cstring>
8
9
10
int myplus(int a,int b)
myplus(int a,int b)
11
{
12
return a+b;
return a+b;
13
}
14
15
double myplus(double c,double d)
myplus(double c,double d)
16
{
17
return c+d;
return c+d;
18
}
19
20
std::string myplus( std::string str1, std::string str2)
myplus( std::string str1, std::string str2)
21
{
22
return str1+str2;
return str1+str2;
23
}
24