建立一个字符串类strtype实现字符串的连接
时间: 2024-04-30 12:20:07 浏览: 19
好的,以下是一个简单的strtype类实现字符串连接的示例代码:
```c++
#include <iostream>
#include <cstring>
class strtype
{
private:
char* str;
public:
strtype(const char* s = "") // 构造函数
{
str = new char[strlen(s) + 1];
strcpy(str, s);
}
~strtype() // 析构函数
{
delete[] str;
}
void join(const char* s) // 字符串连接函数
{
char* tmp = new char[strlen(str) + strlen(s) + 1];
strcpy(tmp, str);
strcat(tmp, s);
delete[] str;
str = tmp;
}
void display() const // 显示字符串
{
std::cout << str << std::endl;
}
};
int main()
{
strtype s1("hello");
s1.join(" world!");
s1.display();
return 0;
}
```
运行结果:
```
hello world!
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![ppt](https://img-home.csdnimg.cn/images/20210720083527.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![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)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/pdf](https://img-home.csdnimg.cn/images/20210720083512.png)