String类c++实现

5 篇文章 0 订阅
2 篇文章 0 订阅

1>.h  文件

#define _CRT_SECURE_NO_WARNINGS
#pragma once
#include <iostream>
#include <string>
using namespace std;

class myString
{
public:
    myString();
    //myString(int len);
    myString(const char *str);
    myString(const myString &another);
    ~myString();
    //重载[]
    char & operator[](int index);
    //重载<<
    friend ostream &operator<<(ostream &os, myString&s);
    //重载>>
    friend istream& operator>>(istream&is, myString &s);
    //重载=
    myString &operator=(myString &s);
    myString &operator=(char* c);
    //重载+
    myString operator+ (myString &s );
    //重载==
    bool operator==(myString &s);
    //重载!=
    bool operator!=(myString &s);
    
private:
    int len;
    char *str;
};

2>.cpp文件

#include "myString.h"

myString::myString()
{
    this->len = 0;
    this->str = NULL;
}
myString::myString(const char *str)
{
    if (str == NULL)
    {
        this->len = 0;
        this->str = new char[0 + 1];
        strcpy(this->str, "");
    }
    else
    {
        this->len = strlen(str);
        this->str = new char[this->len + 1];
        strcpy(this->str, str);
    }
    
}
myString::myString(const myString &another)
{
    this->len = another.len;
    this->str = new char[this->len + 1];
    strcpy(this->str, another.str);
}
myString::~myString()
{
    if (this->str != NULL)
    {
        cout << "~mystr()....." << endl;
        delete this->str;
        this->str = NULL;
        this->len = 0;
    }
}
 char & myString::operator[](int index)
 {
     return this->str[index];
 }
 ostream & operator<<(ostream &os, myString&s)
 {
     os << s.str;
     return os;
 }
 istream& operator>>(istream&is, myString &s)
 {
     if (s.str != NULL)
     {
         delete[]s.str;
         s.str = NULL;
         s.len = 0;
     }
     char str_temp[4096] = { 0 };
     is >> str_temp;
     s.len = strlen(str_temp);
     s.str = new char[s.len + 1];
     strcpy(s.str, str_temp);
     return is;
 }
 myString &myString::operator=(myString &str)
 {
     if (this == &str)
     {
         return *this;
     }
     //回收
     if (this->str != NULL) {
         delete[] this->str;
         this->str = NULL;
         this->len = 0;
     }
     // 深拷贝
     this->len = str.len;
     this->str = new char[this->len + 1];
     strcpy(this->str, str.str);

     return *this;
 }
 myString &myString::operator=(char* p)
 {
     if (this->str != NULL)
     {
         delete[]this->str;
         this->len = 0;
     }
     this->str = new char[strlen(p) + 1];
     strcpy(this->str, p);
     return *this;
 }
 myString myString::operator+ (myString &another)
 {
     myString temp;
     temp.len = this->len + another.len;
     temp.str = new char[temp.len + 1];
     //清空拼接区
     memset(temp.str, 0, temp.len + 1);
     strcat(temp.str, this->str);
     strcat(temp.str, another.str);
    /*
       写法2
             strcpy(temp.str,this->str);
             strcat(temp.str,another.str);
    用strcpy清除开辟区,并且搞出来/0;
    */
    return temp;
 }
 bool myString::operator==(myString &s)
 {
     if (this == &s)
     {
         return true;
     }
     if (this->len == s.len && 0 == strcmp(this->str, s.str))
     {
         return true;
     }
     return false;
 }
 bool myString::operator!=(myString &s)
 {
     if (this == &s)
     {
         return false;
     }
     if (this->len != s.len ||0 != strcmp(this->str, s.str))
     {
         return true;
     }
     else
     {
         return false;

     }
 }

4>text.cpp文件

#include "myString.h"


int main()
{

    cout << "~~~~~~~~~~~~~~~~~~" << endl;
    myString s1("abc");
    myString s3(s1);
    myString s2 ="123";
    


    cout << s1<< endl;
    cout << s2 << endl;
    s1[1] = 'x';
    cout << s1 << endl;
    s1 = s3;
    cout << s1 << endl;

    myString s4("abs");
    cin >> s4;
    cout << s4 << endl;

    cout << s1 << endl;
    cout << s2 << endl;
    cout << "~~~~~~~~~~~~~~~~~~~~~~" << endl;
    cout << s1 + s2 << endl;
    cout << s1 << endl;
    cout << s2 << endl;

    if (s1 == s2)
    {
        cout << "哈哈" << endl;
    }
    cout << "~~~~~~~~~~~~~~~~~~" << endl;
    s2 = s1;
    if (s1 == s2)
    {
        cout << "哈哈" << endl;
    }
    cout << "~~~~~~~~~~~~~~~~~~" << endl;
    if (s1 != s2)
    {
        cout << "xixi" << endl;
}
    cout << "~~~~~~~~~~~~~~~~~~" << endl;
    s2 = "123";
    if (s1 != s2)
    {
        cout << "xixi" << endl;
    }
    cout << "~~~~~~~~~~~~~~~~~~" << endl;
    return 0;
    
}


 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值