自定義string類

#ifndef _MY_STRING_H__
#define _MY_STRING_H__
#include <iostream>
using namespace std;

namespace MyDefine{

class MyString{

public:
    MyString();
    ~MyString();
    MyString(const char* lpStr);
    MyString(const MyString & another);

    //+
    MyString operator+(const MyString & another);

    //+
    MyString operator+(const char* lpStr);
    //+=
    MyString& operator+=(const MyString & another);

    //+=
    MyString& operator+=(const char* lpStr);

    //=
    MyString& operator=(const MyString &another);

    //[]=
    char& operator[](int index);

    //==
    bool operator==(const MyString& another);

    //!=
    bool operator!=(const MyString& another);

    //c_str()
    const char* c_str();

    friend ostream& operator<<(ostream& out, MyString obj);
    //friend ostream& operator<<(ostream& out, MyString& obj);
    friend istream& operator>>(istream& in, MyString& obj);

private:
   int len;
   char* lpStr;

};

}

#endif

 


#include <string.h>
#include "MyString.h"

namespace MyDefine{

MyString::MyString()
{
   this->len = 0;
   this->lpStr = new char[1];
   this->lpStr[0] = '\0';
}

MyString::MyString(const char* lpStr)
{
   if(NULL!=lpStr){
        this->len = strlen(lpStr);
        this->lpStr = new char[this->len+1];
        strcpy(this->lpStr, lpStr);
   }else{
      //this->len = 0;
      //this->lpStr = new char[1];
      //this->lpStr[0] = '\0';
      throw "not support null ptr";
   }
}

MyString::~MyString()
{
   if(NULL!=lpStr){
      delete[] this->lpStr;
      this->lpStr = NULL;
      this->len = 0;
   }
 //  printf("~MyString()\n");
}
 

/MyString a(b);
MyString::MyString(const MyString & another){

   //if(another.lpStr == NULL){
   //    this->len = 0;
   //    this->lpStr = NULL;
   //    return;//null
   //}//default not null, make other action simple
   this->len = another.len;
   this->lpStr = new char[this->len+1];
   strcpy(this->lpStr, another.lpStr);
}

//+
MyString MyString::operator+(const MyString & another){
  MyString tempObj;
  delete[] tempObj.lpStr;
  tempObj.lpStr = new char[this->len + another.len + 1];
  strcpy(tempObj.lpStr, this->lpStr);
  strcpy(tempObj.lpStr+this->len, another.lpStr);
  tempObj.len = this->len + another.len;
  return tempObj;
}

//+
MyString MyString::operator+(const char* lpStr){
  return (*this + MyString(lpStr));
}


    //+=
MyString& MyString::operator+=(const MyString & another){
   return (*this) += another.lpStr;
}


    //+=
MyString& MyString::operator+=(const char* lpStr){
   int olen = strlen(lpStr);
   char* lpTemp = new char[this->len+olen+1];
   strcpy(lpTemp, this->lpStr);
   strcpy(lpTemp+this->len, lpStr);
   delete[] this->lpStr;
   this->lpStr=lpTemp;
   this->len += olen;
   return *this;
}

//=
MyString& MyString::operator=(const MyString &another){
  if(this == &another){
     return *this;
  }
  delete[] this->lpStr;
  this->lpStr = new char[another.len+1];
  strcpy(this->lpStr, another.lpStr);
  this->len = another.len;
  return *this;
}

//[]
char& MyString::operator[](int index){
  if(this->len<=index){
     throw "out of array len";
  }
  return this->lpStr[index];
}

//==
bool MyString::operator==(const MyString& another){
   if(this->len != another.len){
       return false;
   }
   if(strcmp(this->lpStr, another.lpStr)==0){
       return true;
   }
   return false;
}

//!=
bool MyString::operator!=(const MyString& another){
    return !(*this==another);
}

//c_str()
const char* MyString::c_str(){
    return this->lpStr;
}

ostream& operator<<(ostream& out, MyString obj){
    return out<<obj.lpStr;
}
/*
ostream& operator<<(ostream& out, MyString& obj){
    return out<<obj.lpStr;
}
*/
istream& operator>>(istream& in, MyString& obj){
   delete[] obj.lpStr;//imposible null
   //obj.len = 0;
   char* lpBuf = new char[2048];
   memset(lpBuf, 0, sizeof(lpBuf));
   in>>lpBuf;
   obj.len = strlen(lpBuf);
   obj.lpStr = new char[obj.len+1];
   strncpy(obj.lpStr, lpBuf, obj.len);
   obj.lpStr[obj.len]='\0';
   return in;
}

#include <stdio.h>

#include "MyString.h"
using namespace MyDefine;


int main(){
   MyString a;
   MyString b("hello world");
   MyString c(b);

   cout<<"a="<<a<<endl;
   cout<<"b="<<b<<endl;
   cout<<"c(b)="<<c<<endl;
   a = c;
   cout<<"(a=c)="<<a<<endl;

   cout<<"cin>>a"<<endl;
   cin>>a;
   cout<<"a="<<a<<endl;

   cout<<"a+c="<<(a+c)<<endl;
   cout<<"a="<<a<<endl;
   cout<<"c="<<c<<endl;
   cout<<"c[2]="<<c[2]<<endl;
   cout<<"c.c_str()=" << c.c_str()<<endl;
   cout<<c+"str"<<endl;
   cout<<(c==b)<<endl;
   cout<<(c!=b)<<endl;

   cout<<"a+=b"<<(a+=b)<<endl;
   cout<<"b+=xx="<<(b+="abc")<<endl;
   getchar();
   return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章