建立一个字符串类用成员函数编写程序重载运算符“+”,使该运算符实现两个字符串的连接。
3 回答
米脂
TA贡献1836条经验 获得超3个赞
#include<iostream>
#include<string>
#include<cstring>
using namespace std;
class String
{
private:
char *a;
public:
String(): a(0) {}
String(char *b)
{
a=new char [strlen(b)+1];
strcpy(a,b);
}
String operator+( const String &t)
{
String temp;
temp.a=new char [ strlen(a)+strlen(t.a)+1 ];
strcpy(temp.a,a);
strcat(temp.a,t.a);
return temp;
}
void display()
{
cout<<a<<endl;
}
};
int main()
{
String x("abcd");
String y("xyz");
String z;
z=x+y;
z.display();
return 0;
}
虽然没分但我还是帮了啊,这题我也是刚研究不久。
注:编译通过。
添加回答
举报
0/150
提交
取消