字符串封装,mystring类,用C++实现

封装了多次mystring,但是为了防止遗忘,特写篇文章备查

包括三个文件,mystring.h,mytring.cpp,测试文件:main.cpp
测试平台,vs2013

mystring.h

#ifndef __MYSTRING_H__
#define __MYSTRING_H__

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;

class mystring
{
public:
    /*带默认参数的构造函数*/
    mystring(const char *str = NULL);
    /*拷贝构造函数*/
    mystring(const mystring &other);
    /*虚析构函数*/
    virtual ~mystring();
    /*赋值运算符重载*/
    mystring & operator=(const mystring &other);
    /*获得C风格字符串*/
    char *getCstring();

    /*运算符重载*/
    char operator[](int i);
    bool operator<(const mystring &other);
    bool operator>(const mystring &other);
    bool operator==(const mystring &other);
    /*字符串连接符*/
    mystring & operator+=(const mystring &other);

    /*字符串重载输入输出运算符*/
    friend ostream &operator<<(ostream &out, mystring &str);
    friend istream &operator>>(istream &in, mystring &str);

private:
    char *m_pstr;

};


#endif

mytring.cpp

#include"mystring.h"

/*带默认参数的构造函数*/
mystring::mystring(const char *str)
{
    if (str == NULL)
    {
        m_pstr = new char[1];
        *m_pstr = '\0';
    }
    else
    {
        int len = strlen(str);
        m_pstr = new char[len + 1];
        /*strcpy会连带字符串结束符一起拷贝*/
        strcpy(m_pstr, str);
    }
}

/*拷贝构造函数*/
mystring::mystring(const mystring &other)
{
    int len = strlen(other.m_pstr);
    m_pstr = new char[len + 1];
    strcpy(m_pstr, other.m_pstr);
}


/*虚析构函数*/
mystring::~mystring()
{
    delete[]m_pstr;
}

/*赋值运算符重载,返回引用的目的&是为了连等式*/
mystring & mystring::operator=(const mystring &other)
{
    if (this == &other)
        return *this;
    delete[]m_pstr;
    int len = strlen(other.m_pstr);
    m_pstr = new char[len + 1];
    strcpy(m_pstr, other.m_pstr);
    return *this;

}

/*获得C风格字符串*/
char *mystring::getCstring()
{
    /*只能通过成员函数来操作私有成员*/
    return m_pstr;
}

/*重载运算符*/
char mystring::operator[](int i)
{
    if (i>=0&&i<=strlen(m_pstr))
    {
        return m_pstr[i];
    }
    
}

bool mystring::operator<(const mystring &other)
{
    return (strcmp(this->m_pstr, other.m_pstr)<0 ? true : false);
}

bool mystring::operator>(const mystring &other)
{
    return (strcmp(this->m_pstr, other.m_pstr)>0 ? true : false);
}

bool mystring::operator==(const mystring &other)
{
    return (strcmp(this->m_pstr, other.m_pstr) == 0 ? true : false);
}

/*字符串连接符*/
mystring & mystring::operator+=(const mystring &other)
{
    int len = strlen(m_pstr)+strlen(other.m_pstr);
    char *temp = new char[len + 1];
    /*先拷贝*/
    strcpy(temp, m_pstr);
    /*再连接*/
    strcat(temp, other.m_pstr);
    delete[]m_pstr;
    m_pstr = temp;
    return *this;
}

/*字符串重载输入输出运算符*/
ostream &operator<<(ostream &out, mystring &str)
{
    return out << str.m_pstr;
}

istream &operator>>(istream &in, mystring &str)
{
    char buf[256];
    in >> buf;
    /*调用C风格参数构造函数创建对象*/
    str=mystring(buf);
    return in;
}

main.cpp

#include "mystring.h"

int main()
{
    /*测试空串*/
    mystring str1;
    cout << "str1=" << str1 << endl;;

    /*测试用C风格字符串来构造*/
    mystring str2("hello world");
    cout << "str2="<<str2 << endl;

    /*测试拷贝构造函数*/
    mystring str3(str2);
    cout << "str3=" << str3 << endl;

    /*测试赋值运算符重载*/
    str1 = str3;
    cout << "赋值之后,str1=" << str1 << endl;

    /*测试getCstring()*/
    cout << "str3的C风格字符串: "<<str3.getCstring()<<" 长度为: "<<strlen(str3.getCstring()) << endl;

    mystring str4 = "i love you";
    cout << str4 << endl;

    /*测试str[i]*/
    cout << str4[3] << endl;

    /*测试> < == +=*/
    cout << "str4 > str3 "<< (str4 > str3) << endl;
    cout << "str4 < str3 " << (str4 < str3) << endl;
    cout << "str4 == str3 " << (str4 == str3) << endl;
    cout << "str2 == str3 " << (str2 == str3) << endl;
    cout << "str4 += str3 " << (str4 += str3) << endl;

    /*测试cin,cout*/
    mystring str5;
    cin >> str5;
    cout << str5 << endl;

    system("pause");
    return 0;
}

 

posted on 2014-10-27 14:34  你好中国  阅读(980)  评论(0编辑  收藏  举报