自己实现string<char>类

来源:互联网 发布:腾讯安全软件管家 编辑:程序博客网 时间:2024/06/06 09:12
#include<iostream>#include<string.h>using namespace std;enum{ARRLEN=100};class String{public:public:String(const char *str = NULL);String(const String &other);~ String(void);String&  operator=(const String &other);String   operator+(const String &other);//优先级高与 friend 系列加法重载String   operator+(const char *str);//优先级高与 friend 系列加法重载friend String operator+(const String &st,const char *str);friend String operator+(const char *str,const String &st);friend String operator+(const String &st,const String &st2);friend ostream & operator <<(ostream&,String &str);    friend istream & operator >>(istream&,String &str);void show(){cout<<"m_data="<<m_data<<endl;}private:char *m_data;};String::String(const char *str){if(NULL == str){m_data = new char[1];*m_data = '\0';}else{int len = strlen(str)+1;m_data=new char[len];stpcpy(m_data,str);}cout<<"\t\tString::String(const char *str)"<<endl;}String::String(const String &other){int len = strlen(other.m_data)+1;m_data=new char[len];stpcpy(this->m_data,other.m_data);cout<<"\t\tString::String(const String &other)"<<endl;}String::~ String(){delete [] m_data;cout<<"\t\tString::~ String()"<<endl;}String& String::operator=(const String &other){if(this == &other){return *this;}delete [] m_data;int len = strlen(other.m_data)+1;m_data=new char[len];stpcpy(this->m_data,other.m_data);cout<<"\t\tString & String::operate =(const String &other)"<<endl;return *this;}String String::operator+(const String &other){String ret; //The addition can not modify the object itselfint ori_len = strlen(this->m_data);int oth_len = strlen(other.m_data);char* new_data = new char[ori_len + oth_len + 1 ];memcpy(new_data,m_data,ori_len);memcpy(new_data+ori_len,other.m_data,oth_len+1);ret.m_data= new_data;cout<<"\t\tString  String::operate+(const String &other)"<<endl;return ret;}String String::operator+(const char *str){String ret;if( NULL == str)return *this;int ori_len = strlen(this->m_data);int oth_len = strlen(str);char* new_data = new char[ori_len + oth_len + 1 ];memcpy(new_data,m_data,ori_len);memcpy(new_data+ori_len,str,oth_len+1);ret.m_data = new_data;cout<<"\t\tString  String::operate+(const char *str)"<<endl;return ret;}String operator+(const String &st,const char *str){String ret;int s_len = strlen(st.m_data);int c_len = strlen(str);char* new_data = new char[s_len + c_len + 1 ];memcpy(new_data,st.m_data,s_len);memcpy(new_data+s_len,str,c_len+1);ret.m_data = new_data;cout<<"\t\tString operator+(const String &st,const char *str)"<<endl;return ret;}String operator+(const String &st1,const String &st2){String ret;int s_len1 = strlen(st1.m_data);int s_len2 = strlen(st2.m_data);char* new_data = new char[s_len1 + s_len2 + 1 ];memcpy(new_data,st1.m_data,s_len1);memcpy(new_data+s_len1,st2.m_data,s_len2+1);ret.m_data = new_data;cout<<"\t\tString operator+(const String &st,const String &st2)"<<endl;return ret;}String operator+(const char *str,const String &st){String ret;int s_len = strlen(st.m_data);int c_len = strlen(str);char* new_data = new char[s_len + c_len + 1 ];memcpy(new_data,str,c_len);memcpy(new_data+c_len,st.m_data,s_len+1);ret.m_data = new_data;cout<<"\t\tString operator+(const char *str,const String &st)"<<endl;return ret;}ostream & operator <<(ostream& output,String &str){output<<str.m_data;    return output;}istream & operator >>(istream& input,String &str)//Do not know how much space to allocate{delete [] str.m_data;str.m_data = new char[ARRLEN];input>>str.m_data;return input; }int main(){String *s1 = new String("abc");String *s2 = new String("123");String *s3 = new String();String *s4 = new String();*s3 = *s2+*s1;//s3->show();cout<<*s3<<endl;cout <<"========================================"<<endl;*s4 = *s3 +"friend";//s4->show();cout<<*s4<<endl;cout <<"========================================"<<endl;*s4 = *s3 +"friend"+*s1;//s4->show();cout<<*s4<<endl;cout <<"========================================"<<endl;*s4 = "friend"+*s1;//s4->show();cout<<*s4<<endl;cout <<"========================================"<<endl;//s1->show();//s2->show();//s3->show();//s4->show();cin>> *s1;cout <<"========================================"<<endl;cout<<*s1<<endl;cout<<*s2<<endl;cout<<*s3<<endl;cout<<*s4<<endl;return 0;}


 
原创粉丝点击