C++之實現MyString類


1.MyString.h

#pragma once
#include<iostream>
using namespace std;
class MyString
{
public:
    MyString(const char *str = NULL);
    MyString(const MyString&another);
    MyString&operator=(const MyString&another);
    char* c_str();
    MyString operator+(const MyString&another);
    bool operator>(const MyString&another);
    bool operator<(const MyString&another);
    bool operator==(const MyString&another);
    char &operator[](int idx);
    char at(int idx);
    friend istream& operator>>(istream& in,MyString & str);
    friend ostream& operator<<(ostream& out,const MyString & str);
    ~MyString(void);
private:
    char *_str;
};
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

2.MyString.cpp

#include "MyString.h"

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

}
//拷貝構造函數
MyString::MyString (const MyString&another)
{
    int len = strlen(another._str);
    this->_str = new char[len+1];
    strcpy(this->_str,another._str);
}
//運算符=重載
MyString&MyString::operator=(const MyString&another)
{
    if(this == &another)
    {
        return *this;
    }
    else{
        delete []this->_str;
        int len = strlen(another._str);
        this->_str = new char[len+1];
        strcpy(this->_str,another._str);
        return *this;
    }
}
//獲取字符
char* MyString::c_str()
{
    return this->_str;
}
//運算符+重載
MyString MyString::operator + (const MyString& another)
{
    MyString mystr;
    int len = strlen(this->_str)+strlen(another._str);
    delete mystr._str;
    mystr._str = new char[len+1];
    memset(mystr._str,0,len+1);
    strcat(mystr._str,this->_str);
    strcat(mystr._str,another._str);
    return mystr;
}
//運算符>重載
bool  MyString::operator>(const MyString& another)
{
    if(strcmp(this->_str,another._str)>0)
        return true;
    else return false;
}
//運算符<重載
bool  MyString::operator<(const MyString& another)
{
    if(strcmp(this->_str,another._str)<0)
        return true;
    else return false;
}
//運算符==重載
bool  MyString::operator==(const MyString& another)
{
    if(strcmp(this->_str,another._str)==0)
        return true;
    else return false;
}
//下標取值
char & MyString::operator[](int idx)
{
    return this->_str[idx];
}
//函數取值
char  MyString::at(int idx)
{
    return this->_str[idx];
}

//輸入運算符重載
istream& operator>>(istream& in,MyString & str)
{
    delete []str._str;
    char buf[1024];

    scanf("%s",buf);
    int len = strlen(buf);

    str._str = new char[len+1];
    strcpy(str._str,buf);
    return in;
}
//輸出運算符重載
ostream& operator<<(ostream& out,const MyString & str)
{
    out<<str._str;
    return out;
}
//析構函數
MyString::~MyString(void)
{
    delete []this->_str;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113

3.main.cpp

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

using namespace std;

int main()
{
    MyString s1;
    cin>>s1;
    cout<<s1<<endl;
    cout<<s1.c_str()<<endl;
    MyString s2("hello");
    cout<<"s2="<<s2.c_str()<<endl;
    MyString s3(s2);
    cout<<s3.c_str()<<endl;
    MyString s4 = " world";
    cout<<"s4="<<s4.c_str()<<endl;
    MyString s5 = s3 + s4;
    cout<<s5.c_str()<<endl;
    if(s2<s4)
       cout<<"s2<s4"<<endl;
    else if(s2>s4)
        cout<<"s2>s4"<<endl;
    else
        cout<<"s2==s4"<<endl;
    system("pause");

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

   其實MyString類是模範C++的string類寫的,string類裏面有很多的成員函數,而我只實現了其中的一小部分常見的功能函數,如果有興趣可以自己試着擴展更多的功能。

發佈了11 篇原創文章 · 獲贊 21 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章