C++实现一个String类(包括 构造函数,析构函数,拷贝构造函数,运算符重载应用)

C++实现一个String类

/**<
    实现一个MyString 类:
   包括 构造函数,析构函数,拷贝构造函数,运算符重载应用
 */
#include<iostream>
#include<cstring>

using namespace std;
class MyString
{

public:
    char* getP();                            //获得指针
    const char*getPconst();                  //常量指针,不能通过指针修改所指向的内存的值
public:
    MyString(const char *str = nullptr);       //构造函数--字符串构造
    MyString(int len);                         //构造函数--长度构造

    ~MyString();                               // 析构函数

    MyString(const MyString &str);               //拷贝构造函数

    MyString& operator = (const MyString &str);  //赋值构造函数 str1 = str2
    MyString& operator = (const char* str);    //赋值构造函数 str = "abc"

    char& operator[](int index);             //重载[](下标)运算符 str[0] = '1';

    bool  operator==(const char* str);       //重载 ==  str = "abc";
    bool  operator==(const MyString& str);     //重载 ==  str1 = str2;

    bool  operator!=(const char* str);       //重载 !=  str = "abc";
    bool  operator!=(const MyString& str);     //重载 !=  str1 = str2;

    int   operator>(const char* str);        //重载  >  str >"abc";
    int   operator>(const MyString& str);      //重载  >  str1 = str2;

    int   operator<(const char* str);        //重载  <  str >"abc";
    int   operator<(const MyString& str);      //重载  <  str1 = str2;

private:
    friend ostream& operator<<(ostream&out,MyString& str);   //重载>>运算符
    friend istream& operator>>(istream&in, MyString& str);   //重载<<运算符
    char *m_data;
    int   m_size;
};

/***************************************************************************/
char* MyString::getP()   //获得指针
{
     return m_data;
}
const char* MyString::getPconst()  //常量指针不能通过指针修改指向的内存的值
{
    return m_data;
}
/***************************************************************************/
//构造函数--字符串构造
MyString::MyString(const char *str)
{
    if(str == nullptr) //加分点:对m_data判断
    {

        m_data = new char[1];//加分点:对空字符串自动申请结束标志位
        m_data[0] = '\0';
        m_size = 0;
    }
    else
        {
            m_size = strlen(str);
            m_data = new char[m_size + 1];//m_size+1为了存储最后一个空字符
            strcpy(m_data,str);
        }

}
/***************************************************************************/
//构造函数--长度构造
MyString::MyString(int len=0)
{
    if (len=0)
    {
        m_size = 0;//m_size+1为了存储最后一个空字符
        m_data = new char[m_size + 1];
        strcpy(m_data,  "");
    }
    else
    {
        m_size = len;
        m_data = new char[m_size + 1];//number+1为了存储最后一个空字符
        memset(m_data,0,m_size); //调用memset函数分配空间
    }
}
/***************************************************************************/
//拷贝构造函数
MyString::MyString(const MyString &str)  //得分点:输入为const型
{
    m_size = str.m_size;
    m_data = new char[m_size +1];
    strcpy(m_data,str.m_data);
}
/***************************************************************************/
//析构函数
MyString::~MyString()
{
    delete[] m_data;
}
/***************************************************************************/

//赋值函数  str1 = str2;
MyString& MyString::operator=(const MyString &str)
{
    if(this == &str) //自赋值判断,非常重要
    return *this;

    delete[] m_data; //释放原有内存资源

    m_size = strlen(str.m_data);
    m_data = new char[m_size+1];
    strcpy(m_data, str.m_data);
    return *this;   //得分点:返回本对象的引用
}
/***************************************************************************/
//赋值函数  str = “abc”;
MyString& MyString::operator=(const char* str)
{
    //释放以前的内存
    if (m_data != NULL)
    {
        delete[]m_data;
        m_data = NULL;
    }
    //根据str的长度开辟新的内存
    m_size = strlen(str);
    m_data = new char[m_size + 1];
    //复制值
    strcpy(m_data, str);
    return *this;
}
/***************************************************************************/
//重载[](下标)运算符 str[0] = '1';
char& MyString::operator[](int index)
{
    return m_data[index];
}
/***************************************************************************/
//重载== (str=="abc")
bool MyString::operator==(const char* str)
{
    //先判断字符数量
    if (m_size!=strlen(str))
    {
        return false;
    }
    return !strcmp(m_data,str);
}

//重载== (str1==str2)
bool MyString::operator==(const MyString& str)
{
    //判断字符数量
    if (m_size != strlen(str.m_data))
    {
        return false;
    }
    return !strcmp(m_data, str.m_data);
}
/***************************************************************************/
//重载!= (str=="abc")
bool MyString::operator!=(const char* str)
{
    return !operator==(str);
}

//重载!= (str1!=str2)
bool MyString::operator!=(const MyString& str)
{
    return !operator==(str);
}
/***************************************************************************/
//重载> (str1>str2)
int MyString::operator>(const MyString& str)
{
    //-1表示str1<str2  1表示str1>str2  0表示=
    return strcmp(m_data,str.m_data);
}

//重载> (str>"abc")
int MyString::operator>(const char* str)
{
    //-1表示str1<str2  1表示str1>str2  0表示=
    return strcmp(m_data, str);
}
/***************************************************************************/
//重载< (str1<str2)
int MyString::operator<(const MyString& str)
{
    return strcmp(str.m_data,m_data);
}

//重载< (str<"abc")
int MyString::operator<(const char* str)
{
    return strcmp(str, m_data);
}
/***************************************************************************/

//重载<<运算符
ostream& operator<<(ostream&out, MyString& str)
{
    out << str.m_data ;
    return out;
}
//重载>>运算符
istream& operator>>(istream&in, MyString& str)
{
    in >> str.m_data;
    return in;
}

/***************************************************************************/
void Test()
{
    cout<<"/****************1.创建空的字符串***********************************/"<<endl;
    //1.创建空的字符串
    MyString atr(0);
    cout<<"/****************2.有参数构造函数***********************************/"<<endl;
    //2.有参数构造函数
    MyString btr("abc");
    MyString ctr = NULL;
    cout<<"/****************3.拷贝构造函数***********************************/"<<endl;
    //3.拷贝构造函数
    MyString dtr = btr;
    cout<<"/****************4.重载<<运算符***********************************/"<<endl;
    //4.重载<<运算符
    MyString etr("efgh");
    cout << etr<<endl;
    cout<<"/****************5.重载=运算符***********************************/"<<endl;
    //5.重载=运算符
    MyString gtr(0);
    gtr = "abc";
    cout << gtr << endl;
    gtr = etr;
    cout << gtr << endl;
    cout<<"/****************6.重载[](下标)运算符***********************************/"<<endl;
    //6.重载[](下标)运算符
    MyString str("234");
    str[0] = '1';
    cout << str << endl;
    cout<<"/****************7.重载==***********************************/"<<endl;
    //7.重载==
    if (btr=="efgh")
    {
        cout << "相等" << endl;
    }
    else
    {
        cout << "不相等" << endl;
    }

    if (btr == str)
    {
        cout << "相等" << endl;
    }
    else
    {
        cout << "不相等" << endl;
    }
    cout<<"/****************8.重载!=***********************************/"<<endl;
    //8.重载!=
    if (btr != "efgh")
    {
        cout << "不相等" << endl;
    }
    else
    {
        cout << "相等" << endl;
    }
    if (btr != str)
    {
        cout << "不相等" << endl;
    }
    else
    {
        cout << "相等" << endl;
    }
    cout<<"/****************9. 重载>***********************************/"<<endl;
    //9. 重载>
    MyString a("abc");
    MyString b("adcs");
    int x = a > b;
    cout << x << endl;
    int x1 = a > "a";
    cout << x1 << endl;
    cout<<"/****************10. 重载<***********************************/"<<endl;
    //10. 重载<
    int y = a < b;
    cout << y << endl;
    int y1 = a < "a";
    cout << y1 << endl;
    cout<<"/****************11. 默认参数 重载>>运算符***********************************/"<<endl;
    //11. 默认参数 重载>>运算符
    MyString d(64);
    cout<<"put_str:";
    cin >> d;
    cout << d;
}
int main()
{
    Test();
    return 1;
}




运行环境:Code::Blocks 17.12

  • 3
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值