实现string类

GWString.h

#pragma once
#include 
using namespace std;

class GWString
{
    friend ostream& operator<<(ostream& lhs, const GWString& str);
    friend istream& operator>>(istream& in, const GWString& str);
public:
    GWString();
    GWString(char *str);//构造函数
    GWString(const GWString& str);
    GWString& operator=(const GWString&);
    GWString& operator+(const GWString&);
    bool operator==(const GWString&);
    char operator[](size_t index);
    int& GWlength();
    void show();
    ~GWString();
private:
    char *m_str;
};


GWString.cpp

#include "stdafx.h"
#include "GWString.h"
#include 
#include 
#include 
using namespace std;


ostream& operator<<(ostream& lhs, const GWString& str)
{
    /*char *temp = str.m_str;
    while ((*temp)!='\0'){
        lhs << *temp;
        temp++;
    }*/
    lhs << str.m_str;
    return lhs;
}

istream& operator>>(istream& in, const GWString& str){
    cout << "请输入..." << endl;
    in >> str.m_str;
    return in;
    
}

GWString& GWString::operator+(const GWString& that){
    int len = strlen(m_str);
    char* temp = new char[len + 1];
    strcpy_s(temp,len + 1,m_str);
    if (m_str)//释放旧资源
    {
        delete m_str;
        m_str = NULL;
    }
    m_str = new char[len + strlen(that.m_str) + 1];  //分配新资源
    memset(m_str, 0, len + strlen(that.m_str) + 1);

    strcat_s(this->m_str, len + strlen(that.m_str) + 1, temp);
    strcat_s(this->m_str, len + strlen(that.m_str) + 1, that.m_str);
    delete[] temp;
    temp = NULL;
    return *this;
}

bool GWString::operator==(const GWString& str){
    char *temp = this->m_str;
    char *temp2 = str.m_str;
    if (strlen(this->m_str) != strlen(str.m_str))
    {
        return false;
    }
    while ( (*temp) != '\0' && (*temp2) != '\0' )
    {
        if ( (*temp) != (*temp2) )
        {
            return false;
        }
        temp++;
        temp2++;
    }
    return true;
}

GWString::GWString()
{
}

GWString::GWString(const GWString& str){
    if (str.m_str != NULL)
    {
        m_str = new char[strlen(str.m_str) + 1];
        sprintf_s(m_str, strlen(str.m_str) + 1, str.m_str);
    }
    else {
        m_str = new char[1];
        m_str = '\0';
    }
}

GWString::GWString(char *str){
    if (str != NULL)
    {
        m_str = new char[strlen(str) + 1];
        //sprintf(m_str, strlen(str) + 1, str);
        strcpy_s(m_str,strlen(str) + 1, str);
    }
    else {
        m_str = new char[1];
        m_str = '\0';
    }
}

GWString& GWString::operator=(const GWString &that){
    if (&that != this)//避免自赋值
    {
        if (m_str)//释放旧资源
        {
            delete m_str;
            m_str = NULL;
        }
        m_str = new char[strlen(that.m_str)+1];  //分配新资源
        //拷贝新内容
        memcpy(m_str, that.m_str, strlen(that.m_str) + 1);
    }
    //返回自引用
    return *this;
}

char GWString::operator[](size_t index){
    int tempIndex = 0;
    char *temp = this->m_str;
    while ((*temp)!= NULL)
    {
        if (index == tempIndex)
        {
            return *temp;
        }
        temp++;
        tempIndex++;
    }
    return *temp;
}

int& GWString::GWlength(){
    int temp = 0;
    char *pTemp = m_str;
    while ((*pTemp) != '\0')
    {
        pTemp++;
        temp++;
    }
    return temp;
}

void GWString::show(){
    
}

GWString::~GWString()
{
    if (m_str)
    {
        delete m_str;
        m_str = NULL;
    }
}

main.m

// 操作符重载2-23期-92-子羽.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include "GWString.h"
#include 
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    //堆区创建对象
    GWString *str1 = new GWString("hello world");
    int length = str1->GWlength();
    cout << "字符串长度为:" << length << endl;

    //栈区创建对象
    GWString str2 = "hello GW";
    GWString str3 = "hello GW";
    int length2 = str2.GWlength();
    cout << "字符串长度为:" << length2 << endl;
    
    //字符串比较
    //if (*str1 == str2)
    if (str3 == str2)
    {
        cout << "string is equal" << endl;
    } else {
        cout << "string is not equal" << endl;
    }

    //字符串输入
    cin >> str2;

    //字符串输出:
    cout << "字符串输出:" << str2 << endl;


    //字符串拼接:
    str3 = str2 + str3;
    //str3 = *str1 + str2;
    cout << "字符串拼接之后打印:" << str3 << endl;


    //字符串中某个字符
    cout << str2[0] << endl;
    return 0;
}


TIM图片20180613172657.png

你可能感兴趣的:(实现string类)