建立一个字符串类用成员函数编写程序重载运算符“+”,使该运算符实现两个字符串的连接?

建立一个字符串类用成员函数编写程序重载运算符“+”,使该运算符实现两个字符串的连接。


哆啦的时光机
浏览 2057回答 3
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;}虽然没分但我还是帮了啊,这题我也是刚研究不久。注:编译通过。
打开App,查看更多内容
随时随地看视频慕课网APP