有谁知道关于MyString类的加法怎么写??

就是想用MyString实现类似于string类的功能
主要是想问返回值怎么弄??
要能实现连加和类似于a=b+c的赋值。
万分感谢!
参数就用 MyString & a,MyString & b 吧。

千巷猫影
浏览 354回答 1
1回答

12345678_0001

#include<iostream>using namespace std;class String;istream& operator>>(istream& in,String& s);ostream& operator<<(ostream& out,String& s);class String{public:enum {MaxSize=100};friend istream& operator>>(istream& in,String& s);friend ostream& operator<<(ostream& out,String& s);String(){str=new char[MaxSize];Size=0;}String(String& s);String& operator+(String& s);void operator=(char *s);void operator=(String& s);char operator[](int i);bool empty(){if(Size==0)return 1;elsereturn 0;}int size() { return Size;}int length() {return Size;}int max_size() {return MaxSize;}~String() {delete[] str;}private:char *str;int Size;};String::String(String& s){str=new char[MaxSize];Size=0;*this=s;}String& String::operator+(String& s2){static String s1;s1=*this;s1=s1+s2;return s1;}void String::operator=(char *s){int i=0;Size=0;while(s[i]!='\0')str[Size++]=s[i++];}void String::operator=(String& s){int i;Size=0;for(i=0;i<s.Size;i++)str[Size++]=s[i];}char String::operator[](int i){return str[i];}istream& operator>>(istream& in,String& s){char c[s.MaxSize];gets(c);s=c;return in;}ostream& operator<<(ostream& out,String& s){int i;for(i=0;i<s.Size;i++)out<<s.str[i];return out;}int main(){system("pause");return 0;}这是我写的String类的一部分
打开App,查看更多内容
随时随地看视频慕课网APP