利用函数的重载功能编写子函数,返回一串string型字符串的连接结果
时间: 2024-02-05 22:12:25 浏览: 33
以下是利用函数重载的方式编写一个子函数,实现返回一串string型字符串的连接结果:
```c++
#include <iostream>
#include <string>
using namespace std;
string join(string str1, string str2) { // 重载函数,用于连接两个字符串
return str1 + str2;
}
string join(string str1, string str2, string str3) { // 重载函数,用于连接三个字符串
return str1 + str2 + str3;
}
int main() {
string str1 = "Hello, ";
string str2 = "world!";
string str3 = "How are you today?";
string result1 = join(str1, str2);
string result2 = join(str1, str2, str3);
cout << "Result 1: " << result1 << endl;
cout << "Result 2: " << result2 << endl;
return 0;
}
```
在上面的代码中,我们定义了两个重载函数`join`,分别用于连接两个字符串和三个字符串。在`main`函数中,我们定义了三个字符串,并分别将它们作为参数传入不同的`join`函数中,得到连接结果并输出。
输出结果如下:
```
Result 1: Hello, world!
Result 2: Hello, world!How are you today?
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)