C++:封装一个字符串

封装一个字符串
MyString.h

#ifndef __MYSTRING_H__
#define __MYSTRING_H__

#include <iostream>

class MyString
{
    // 重载 << 操作符
    friend std::ostream& operator<<(std::ostream &out, MyString &str);

    // 重载 >> 操作符
    friend std::istream& operator>>(std::istream& in, MyString &str);
public:
    MyString();                   // 无参构造
    MyString(const char *s);      // 有参构造
    MyString(int len, char data = 0); // 有参构造
    MyString(const MyString &s);  // 拷贝构造

    ~MyString();                  // 析构函数

// 重载=、[]操作符
public:
    MyString& operator=(const char *s);       // 普通字符串赋值
    MyString& operator=(const MyString &s);   // 类对象之间赋值
    char & operator[](int index);

// 重载 + 运算符
public:
    MyString operator+(const char *str);
    MyString operator+(const MyString &s);

    MyString& operator+=(const char *str);
    MyString& operator+=(const MyString &s);

// 重载 == !=
public:
    bool operator==(const char *str) const;
    bool operator==(const MyString &str) const;

    bool operator!=(const char *str) const;
    bool operator!=(const MyString &str) const;

// 重载 < >
public:
    bool operator>(const char *str) const;
    bool operator>(const MyString &str) const;

    bool operator<(const char *str) const;
    bool operator<(const MyString &str) const;

public:
    const char *c_str()
    {
        return m_p;
    }

    char *c_str2()
    {
        return m_p;
    }

    int leng()
    {
        return m_len;
    }
private:
    int m_len;    // 字符串长度
    char *m_p;    // 字符串数据
}; 
#endif // __MYSTRING_H__

MyString.cpp

#include "MyString.h"
#include <cstring>
MyString::MyString()
{
    m_len = 0;
    m_p = nullptr;
}
MyString::MyString(const char *s)
{
    if (NULL == s)
    {
        m_len = 0;
        m_p = nullptr;
    }
    else
    {
        m_len = strlen(s);
        m_p = new char[m_len+1];
        strcpy(m_p, s);
    }
}

MyString::MyString(int len, char data)
{
    if (len <= 0)
    {
        m_p = nullptr;
        m_len = 0;
    }
    else
    {
        m_len = len;
        m_p = new char[m_len+1];
        for (int i = 0;i < len; i++)
        {
            m_p[i] = data;
        }
        m_p[len] = '\0';
    }
}

MyString::MyString(const MyString &s)
{
    if (nullptr == s.m_p)
    {
        m_len = 0;
        m_p = nullptr;
    }
    else
    {
        m_len = s.m_len;
        m_p = new char[m_len+1];
        strcpy(m_p, s.m_p);
    }
}

MyString::~MyString()
{
    if (m_p != nullptr)
        delete[] m_p;
    m_p = nullptr;
    m_len = 0;
}


MyString& MyString::operator=(const char *s)
{
    MyString str = s;
    *this = str;  // 赋值

    return *this;
}

MyString& MyString::operator=(const MyString &s)
{
    if (this != &s)
    {
        MyString tmp = s;

        char *p = tmp.m_p;
        tmp.m_p   = m_p;
        m_p       = p;

        m_len = s.m_len;
    }

    return *this;
}

MyString MyString::operator+(const char *str)
{
    int len = m_len + strlen(str);

    MyString tmp(len);
    strcpy(tmp.m_p, m_p);
    strcat(tmp.m_p, str);

    return tmp;
}
MyString MyString::operator+(const MyString &s)
{
    int len = m_len + s.m_len;

    MyString tmp(len);
    strcpy(tmp.m_p, m_p);
    strcat(tmp.m_p, s.m_p);

    return tmp;
}

MyString& MyString::operator+=(const char *str)
{
    // *this = *this + str;

    int len = m_len + strlen(str);
    char *tmp = m_p;
    m_p = new char[len+1];
    strcpy(m_p, tmp);
    strcat(m_p, str);


    m_len = len;
    delete[] tmp;

    return *this;
}

MyString& MyString::operator+=(const MyString &s)
{
    // *this = *this + s;
    int len = m_len + s.m_len;
    char *tmp = m_p;
    m_p = new char[len+1];
    strcpy(m_p, tmp);

    if (this == &s)
        strcat(m_p, tmp);
    else
        strcat(m_p, s.m_p);

    m_len = len;
    delete[] tmp;

    return *this;
}
    
char & MyString::operator[](int index)
{
    return m_p[index];
}
   
bool MyString::operator==(const char *str) const
{
//    if (strcmp(m_p, str) == 0)
//        return true;
//    else
//        return false;

    return (strcmp(m_p, str) == 0);
}
bool MyString::operator==(const MyString &str) const
{
    return (strcmp(m_p, str.m_p) == 0);
}

bool MyString::operator!=(const char *str) const
{
    return (strcmp(m_p, str) != 0);
}
bool MyString::operator!=(const MyString &str) const
{
    return (strcmp(m_p, str.m_p) == 0);
}

bool MyString::operator>(const char *str) const
{
//    if (strcmp(m_p, str) == 0)
//        return true;
//    else
//        return false;

    return (strcmp(m_p, str) > 0);
}
bool MyString::operator>(const MyString &str) const
{
    return (strcmp(m_p, str.m_p) > 0);
}

bool MyString::operator<(const char *str) const
{
    return (strcmp(m_p, str) < 0);
}
bool MyString::operator<(const MyString &str) const
{

    return (strcmp(m_p, str.m_p) < 0);
}
 
std::ostream& operator<<(std::ostream &out, MyString &str)
{
    out << str.m_p;
    return out;
}

std::istream& operator>>(std::istream& in, MyString &str)
{
    in >> str.m_p;
    return in;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值