C++中對string的封裝



實現C++中對string類的封裝
string類型,是C++對C基礎類型的封裝實現的,用法可以類比於基礎類型int ,char ,並且實現字符串直接的=(賦值),>,<,==,及[ ]運算。
今天我試着自己封裝了String類型。並對運算符進行重載,實現了類似於基礎類型的String的關係運算。
<pre style="margin-top: 0px; margin-bottom: 0px;"><span style="font-size:18px;"><span style=" color:#000080;">#ifndef</span><span style=" color:#c0c0c0;"> </span>MYSTRING_H</span>
#define MYSTRING_H
#include <stdlib.h>
class MyString
{
public:
    MyString(const char *str=NULL);	//帶默認參數的構造器
    char *c_ret();
    ~MyString();
    MyString *operator=(const MyString &another);//對賦值號的重載
    MyString operator+(const MyString & other);//對加號重載
    bool operator==(const MyString &other);//對等號重載
    bool operator>(const MyString &other);//對大於號重載
    bool operator<(const MyString &other);//對小於號重載
    char operator[](int idx); //對取下標重載
private:
    char *_str;
};

#endif // MYSTRING_H



<pre style="margin-top: 0px; margin-bottom: 0px;"><span style="font-size:18px;"><span style=" color:#000080;">#include</span><span style=" color:#c0c0c0;"> </span><span style=" color:#008000;">"mystring.h"</span></span>
#include <iostream>
#include <string.h>

MyString::MyString(const char *str)
{
    if(str==NULL)
    {
       _str=new char[1];
       *_str='\0';
    }
    else
    {
       int len =strlen(str);
       _str=new char[len+1];
       strcpy(_str,str);
    }
}

char *MyString::c_ret()
{
    return _str;
}

MyString::~MyString()
{
    delete []_str;
}

MyString &MyString::operator=(const MyString &another)  //返回MyString的引用,也可以返回this指針,用MyString *作返回值。可以實現連等即 a=b=c;
{
    if(this ->_str==another._str)		//如果爲賦值號左右判斷爲相同的對象,則直接返回this指針。	
        return this;
    else
    {
        delete _str;
        int len=strlen(another._str);
        _str=new char[len+1];
        strcpy(_str,another._str);
        return *this;
    }
}

MyString MyString::operator+(const MyString &other)	
{
    using std::cout;
    using  std::endl;
    MyString *p=new MyString;
    int len1=strlen(_str);
    int len2=strlen(other._str);
    p->_str=new char[len1+len2+1];
    memset(p->_str,0,len1+len2+1);
   strcat(p->_str,_str);
   strcat(p->_str,other._str);
   return *p;
}

bool MyString::operator==(const MyString &other)
{
   if(strcmp(_str,other._str)==0)
       return true;
   else
       return false;
}

bool MyString::operator>(const MyString &other)
{
    if(strcmp(_str,other._str)>0)
        return true;
    else
        return false;
}

bool MyString::operator<(const MyString &other)
{
    if(strcmp(_str,other._str)<0)
        return true;
    else
        return false;
}

char MyString::operator[](int idx)
{
    return this->_str[idx];
}




#include <iostream>
#include <mystring.h>
using namespace std;
int main()
{
   MyString m1("malian");
   MyString m2("majingjing");
   MyString m3=m1+m2;
   cout<<m3.c_ret()<<endl;
   MyString m4("hahahaha");
   m4=m3;
   cout<<"test"<<m4.c_ret()<<endl;
   MyString m5("hahah");
   if(m4==m5)
       cout<<"yes"<<endl;
   else if(m4>m5)
       cout<<"大於"<<endl;
   else if(m4<m5)
       cout<<"小於"<<endl;
   cout<<m4[1]<<endl;

    return 0;
}



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