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;
};

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;
}

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");

}

   其实MyString类是模范C++的string类写的,string类里面有很多的成员函数,而我只实现了其中的一小部分常见的功能函数,如果有兴趣可以自己试着扩展更多的功能。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值