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类里面有很多的成员函数,而我只实现了其中的一小部分常见的功能函数,如果有兴趣可以自己试着扩展更多的功能。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值