菜鸟编程遇困,求救中
日期:2006-06-12 荐:
菜鸟编程遇困,求救中那位大哥帮我看看这个咋编哈: 设计一个类String,每个对象能存储一个字符串。要求对该类重载“ ”,实现两个字符串的相加:比如s1=“abcd”,s2=“bcde”,则s1 s2=“abcdbcde”,并能输出一个对象所存储的字符串。字符串连接要使用函数:strcat(a,b)唉,我写的String不见了,还是很不错的。现在打算再在std::string外面包装一层。String类可以参看CString的代码来写。class String{private: char *s;public: String(char *s1) { delete []s; s = new [strlen(s1) 1]; strcpy(s,s1); } friend String operater (const String &s1,const String &s2);};String operator (const String &s1,const String &s2){ char *temp; temp = new [strlen(s1) strlen(s2) 1)]; strcat(temp,s1); strcat(temp,s2); return temp;} int main(){ String s1("abc"),s2("def"); cout<<s1 s2<<endl; return 0;}不好意思写了个大概,可能还有点问题望自己上机多调试class String{private: char *s;public: String(char *s1) { delete []s; s = new [strlen(s1) 1]; strcpy(s,s1); } friend String operater (const String &s1,const String &s2); ~String() { delete []s; }};String operator (const String &s1,const String &s2){ char *temp; temp = new [strlen(s1) strlen(s2) 1)]; strcat(temp,s1); strcat(temp,s2); return temp;} int main(){ String s1("abc"),s2("def"); cout<<s1 s2<<endl; return 0;}class String {char * str;public: String(char * s) { str=new char[strlen(s) 1]; strcpy(str,s); } ~String(char * s) { delete[] this->str; } String & operator (String & s) { char * tmp=new char[strlen(s) strlen(this->str) 1]; strcpy(tmp,this->str); strcat(tmp,s); delete[] this->str; this->str=tmp; return this; }friend ostream & operator << (String s,ostream &)}ostream & operator << (String s,ostream &) {ostream<<s->str;return ostream;}ostream & operator << (String s,ostream & oo) {oo<<s->str;return oo;}delete []s;麻烦,这个是咋个用的?什么意思?有啥功能?????new 的时候如果那个 new 后面接了一个 []比如 int *p=new int[5];那么delete的时候后面必须接一个[]就是delete []p;class String{private: char *s;public: String(char *s1) { delete []s; s = new [strlen(s1) 1]; strcpy(s,s1); } friend String operater (const String &s1,const String &s2); ~String() { delete []s; }};String operator (const String &s1,const String &s2){ String temp; temp.str = new [strlen(s1) strlen(s2) 1)]; strcat(temp.str,s1); strcat(temp.str,s2); return temp;} int main(){ String s1("abc"),s2("def"); cout<<s1 s2<<endl; return 0;}s = new int 对应 delete s;s = new int [5] 对应delete []s;String operator (const String &s1,const String &s2) 后面不是只能带一个参数吗?我用的是友员呀所以可以用两个class String{private: char *s;public: String(char *s1) { delete []s; s = new [strlen(s1) 1]; strcpy(s,s1); } friend String operater (const String &s1,const String &s2);};String operator (const String &s1,const String &s2){ char *temp; temp = new [strlen(s1) strlen(s2) 1)]; strcat(temp,s1); strcat(temp,s2); return temp;} =========构造函数里delete干吗?#include<iostream.h>#include<string.h>class String{protected: char *s; int len;public: String(){s=0;len=0;} String(const char *); ~String(){} void show(); void freeString(); void operator=(String &); friend String operator (const String &,const String &);}; String::String(const char *c) { len=strlen(c) 1; s=new char[len]; strcpy(s,c);}String operator (const String &a,const String &b){ String temp; temp.len=a.len b.len-1; temp.s=new char[temp.len]; strcpy(temp.s,a.s); strcat(temp.s,b.s); return temp;} void String::operator=(String &a) { freeString(); len=a.len; if(a.s)s=new char[len],strcpy(s,a.s); else s=0;} void String::show(){
cout<<s<<endl;}void String::freeString(){ if(s)delete []s;} int main(){ String a("Hello "),b("World!"),c; a.show(); b.show(); c=a b; c.show(); a.freeString(); b.freeString(); c.freeString(); return 0;}上机调试通过#include<iostream.h>#include<string.h>class str{private:char *s;public:str(){ s = new char [200];}str(char *s1){ s = new char[strlen(s1) 1]; strcpy(s,s1);} ~str() { delete []s; } char *get() { return s; } friendstr operator (const str &s1,const str &s2); void display() { cout<<this->s<<endl; } str &operator=(const str &s1) { delete []s; s = new char [strlen(s1.s) 1]; strcpy(s,s1.s); return *this; } friend ostream &operator<<(const ostream &os,str &s1);};str operator (const str &s1,const str &s2){ char *t = new char[strlen(s1.s) strlen(s2.s) 1]; strcpy(t,s1.s); strcat(t,s2.s); return str(t);}ostream &operator<<( ostream &os,str &s1){ os<<s1.get()<<endl; return os;}int main(){ str a("abc"),b("def"),c,d,e; c = a; d = b; e = c d; cout<<e<<endl; cout<<c<<endl; cout<<d<<endl; return 0;}
标签: