用C++实现String类的四种构造和运算符重载

本文详细介绍了如何使用C++实现一个基本的String类,包括四种构造函数(默认、从字符指针、复制构造和参数构造),以及常见的运算符重载如赋值、比较和索引等。通过示例代码展示了类的实现过程和主要功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

(教学版本,main函数可以自己写)

MyString.h

 

#ifndef __MYSTRING_H__

#define __MYSTRING_H__

 

#include <iostream>

using namespace std;

 

class MyString

{

    friend ostream & operator<<(ostream& out, const MyString& str);

    friend istream & operator>>(istream& in, const MyString& str);

public:

    MyString(int len=0);

    MyString(const char *p);

    MyString(const MyString & str);

    ~MyString();

 

public:

    MyString& operator=(const char *p);

    MyString& operator=(const MyString &str);

 

    char& operator[](const int index);

 

    bool operator==(const char *p) const;

    bool operator==(const MyString& str) const;

    bool operator!=(const char *p) const;

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

 

    bool operator<(const char *p) const;

    bool operator<(const MyString& str) const;

    bool operator>(const char *p) const;

    bool operator>(const MyString& str) const;

 

public:

    char *c_str()

    {

        return m_p;

    }

    const char *c_str2()

    {

        return m_p;

    }

 

    int length()

    {

        return m_len;

    }

 

private:

    int m_len;

    char *m_p;

};

 

#endif

 

 

 

 

 

MyString.cpp

#include "MyString.h"

#include <string.h>

 

ostream & operator<<(ostream& out, const MyString& str)

{

    out << str.m_p;

    return out;

}

 

istream & operator>>(istream& in, const MyString& str)

{

    in >> str.m_p;

    return in;

}

 

MyString::MyString(int len)

{

    if (len == 0)

    {

        m_len = 0;

        m_p = new char[m_len + 1];

        strcpy(m_p, "");

    }

    else

    {

        m_len = len;

        m_p = new char[m_len + 1];

        memset(m_p, 0, m_len+1);

    }

}

 

MyString::MyString(const char *p)

{

    if (p == NULL)

    {

        m_len = 0;

        m_p = new char[m_len + 1];

        strcpy(m_p, "");

    }

    else

    {

        m_len = strlen(p);

        m_p = new char[m_len + 1];

        strcpy(m_p, p);

    }

 

}

 

MyString::MyString(const MyString &str)

{

    m_len = str.m_len;

    m_p = new char[m_len + 1];

    strcpy(m_p, str.m_p);

}

 

MyString::~MyString()

{

    if (m_p != NULL)

    {

        delete [] m_p;

        m_p = NULL;

        m_len = 0;

    }

}

 

MyString& MyString::operator=(const char *p)

{

    if (m_p != NULL)

    {

        delete [] m_p;

    }

    if (p == NULL)

    {

        m_len = 0;

        m_p = new char[m_len + 1];

        strcpy(m_p, "");

    }

    else

    {

        m_len = strlen(p);

        m_p = new char[m_len + 1];

        strcpy(m_p, p);

    }

 

    return *this;

}

 

 

MyString& MyString::operator=(const MyString &str)

{

    if (m_p != NULL)

    {

        delete [] m_p;

    }

 

    m_len = str.m_len;

    m_p = new char[m_len + 1];

    strcpy(m_p, str.m_p);

 

    return *this;

}

 

char& MyString::operator[](const int index)

{

    return m_p[index];

}

 

bool MyString::operator==(const char *p) const

{

    if (p == NULL)

    {

        if (m_len == 0)

            return true;

        else

            return false;

    }

    else

    {

        return !strcmp(m_p, p);

    }

}

 

bool MyString::operator==(const MyString& str) const

{

    return !strcmp(m_p, str.m_p);

}

 

bool MyString::operator!=(const char *p) const

{

    return !(*this == p);

}

 

bool MyString::operator!=(const MyString& str) const

{

    return !(*this == str);

}

 

bool MyString::operator<(const char *p) const

{

    return (strcmp(m_p, p) < 0);

}

 

bool MyString::operator<(const MyString& str) const

{

    return (strcmp(m_p, str.m_p) < 0);

}

 

bool MyString::operator>(const char *p) const

{

    return (strcmp(m_p, p) > 0);;

}

 

bool MyString::operator>(const MyString& str) const

{

    return (strcmp(m_p, str.m_p) > 0);

}

 

 

 

Main.cpp

int main1()

{

    MyString s1;

    MyString s2("s2");

    MyString s2_2 = NULL;

    MyString s3 = s2;

 

    MyString s4 = "s42323233";

    s4 = s2;

    s4 = "headad";

 

    cout << s4[2] << endl;

 

    cout << s4 << endl;

 

    return 0;

}

 

int main2()

{

    MyString s1;

    MyString s2("s2");

    MyString s3("s223123");

 

    if (s2 == NULL)

    {

        cout << "相等" << endl;

    }

    else

    {

        cout << "不相等" << endl;

    }

 

    if (s2 == s3)

    {

        cout << "相等" << endl;

    }

    else

    {

        cout << "不相等" << endl;

    }

 

 

    return 0;

}

 

int main3()

{

    MyString s1;

    MyString s2("s2");

    MyString s3("s3");

 

    if (s3 < "bbbb")

    {

        cout << "s3 < bbbb" << endl;

    }

    else

    {

        cout << "s3 > bbbb" << endl;

    }

 

    if (s3 > s2 )

    {

        cout << "s3 > s2" << endl;

    }

    else

    {

        cout << "s3 < s2" << endl;

    }

 

    return 0;

}

 

int main()

{

    MyString s1(128);

    cin >> s1;

 

    cout << s1 << endl;

 

    return 0;

}

 

 

 

 

我的版本(较粗糙)

String.h

#ifndef _STRING_H_

#define _STRING_H_

 

#include <iostream>

 

using namespace std;

class String

{

private:

        char *m_data;

        int m_len;

 

public:

        String();

        String(const String &s);

        String(char *data);

        String(int n,char a);

        ~String();

        //void print();

        friend ostream &operator <<(ostream &out,const String &s);

        friend istream &operator >>(istream &in, String &s);

 

        String &operator =(const String &s);

        String &operator =( char *s);

 

        String &operator +=(const String &s);

 

        char &operator [](int index);

 

        bool operator ==(const String &s);

 

};

 

#endif

 

 

 

 String.cpp

#include <iostream>

#include <string.h>

#include "String.h"

 

using namespace std;

 

String ::String()

{

    m_data = new char[1];

        //m_data[0] = '0';

        m_len = 1;

}

 

String ::String(char *data)

{

 

        m_len = strlen(data);

        m_data = new char[m_len +1];

        strcpy(m_data,data);

 

}

 

String ::String(int n,char a)

{

        int i;

        char b[n];

 

        m_len = n;

        m_data = new char[m_len + 1];

 

        for(i=0;i<n;i++)

        {

                b[i] = a;

        }

        for(i=0;i<n;i++)

        {

                m_data[i] = b[i];

        }

        m_data[n] = '\0';

}

 

String ::String(const String &s)

{

        m_len = s.m_len;

        m_data = new char[m_len + 1];

        strcpy(m_data,s.m_data);

}

 

/*void String::print()

{

        cout << m_data << endl;

        cout << m_len << endl;

}*/

 

String::~String()

{

        if(m_data != NULL)

        {

                delete[] m_data;

                m_data = NULL;

                m_len = 0;

        }

}

 

ostream &operator <<(ostream &out,const String &s)

{

        out << s.m_data;

        return out;

}

 

istream &operator >>(istream &in,String &s)

{

        char tmp[1024] = {0};

 

        in >> tmp;

 

        if(s.m_data != NULL)

        {

                delete s.m_data;

        }

        s.m_len = strlen(tmp);

        s.m_data = new char[s.m_len +1];

        strcpy(s.m_data,tmp);

 

        return in;

}

 

String &String::operator =(const String &s)

{

        if(*this == s)

        {

                return *this;

        }

        m_len = s.m_len;

        if(m_data != NULL)

        {

                delete m_data;

        }

        m_data = new char[m_len + 1];

        strcpy(m_data,s.m_data);

}

 

String &String::operator =( char *s)

{

    if(*this == s)

        {

                return *this;

        }

        m_len = strlen(s);

        if(m_data != NULL)

        {

                delete m_data;

        }

        m_data = new char[m_len +1];

        strcpy(m_data,s);

}

 

bool String::operator ==(const String &s)

{

        if(this->m_data == s.m_data)

        {

                return true;

        }

        else

        {

                return false;

        }

}

 

String & String::operator +=(const String &s)

{

        m_len = m_len + s.m_len;

 

        char *tmp = new char[m_len + 1];

        strcpy(tmp,m_data);

        strcat(tmp,s.m_data);

 

        delete m_data;

        m_data = tmp;

 

        return *this;

}

 

char &String::operator [](int index)

{

        return m_data[index];

}

 

 

main.cpp

#include <iostream>

#include "String.h"

using namespace std;

 

int main()

{

        String s3(10,'b');

        String s2("aba");

        String s1;

        String s4(s2);

        String s5;

        String s6;

 

 

        cout << s1 << endl;

        cout << s2 << endl;

        cout << s3 << endl;

        cout << s4 << endl;

 

        cin >> s1;

        cout << s1 << endl;

 

        cout << (s2 += s3) << endl;

        cout << s2[1] << endl;

 

        s5 = s3;

        s6 = "hello";

        cout << s5 << endl;

        cout << s6 << endl;

return 0;

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值