第39课 - 字符串类的创建(上)

1、字符串类的创建 

        历史遗留问题 

            - C语言不支持真正意义上的字符串 

            - C语言用字符数组—组函数实现字符串操作 

            - C语言 不支持自定义类型,因此 无法获得字符串类型 


        从C到C++的进化过程引入了自定义类型

        在C++中可以通过类完成字符串类型的定义 


        问题:

                 C++中的原生类型系统是否包含字符串类型?


      DTLib中字符串类的设计

        


        DTLib中字符串类的实现 

        


        实现时的注意事项 

            -无缝实现 String 对象与 char* 字符串的互操作 

            -操作符重载函数需要考虑是否支持 const 版本 

             - 通过C语言中的字符串函数实现  String 的成员函数 


2、编程实验 

字符串类的实现     String.h     String.cpp

DTString.h

#ifndef DTSTRING_H
#define DTSTRING_H

#include"Object.h"

namespace DTLib
{

class String : public Object
{
protected:
    char* m_str;
    int m_length;

    void init(const char* s);
public:
    String();
    String(char c);
    String(const char* s);
    String(const String& s);

    int length() const;
    const char* str() const;

    bool operator == (const String& s) const;
    bool operator == (const char* s) const;
    bool operator != (const String& s) const;
    bool operator != (const char* s) const;
    bool operator > (const String& s) const;
    bool operator > (const char* s) const;
    bool operator < (const String& s) const;
    bool operator < (const char* s) const;
    bool operator >= (const String& s) const;
    bool operator >= (const char* s) const;
    bool operator <= (const String& s) const;
    bool operator <= (const char* s) const;

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

    String& operator = (const String& s);
    String& operator = (const char* s);
    String& operator = (char c);

    ~String();
};
}

#endif // DTSTRING_H

DTString.cpp

#include<cstring>
#include<cstdlib>
#include"DTString.h"
#include"Exception.h"

using namespace std;

namespace DTLib
{

void String::init(const char *s)
{
    m_str = strdup(s);

    if(m_str)
    {
        m_length = strlen(m_str);
    }
    else
    {
        THROW_EXCEPTION(NoEnoughMemoryException,"No memory to create String object ...");
    }
}

String::String()
{
    init("");
}

String::String(const char *s)
{
    init(s ? s : "");   //NULL指针转换为空字符串
}

String::String(const String &s)
{
    init(s.m_str);
}
String::String(char c)
{
    char s[] = {c,'\0'};
    init(s);
}

int String::length() const
{
    return m_length;
}

const char* String::str() const
{
    return m_str;
}

bool String::operator == (const String& s) const
{
    return (strcmp(m_str,s.m_str) == 0);
}
bool String::operator == (const char* s) const
{
    return (strcmp(m_str,s ? s : "") == 0);
}

bool String::operator != (const String& s) const
{
    return !(*this == s);
}
bool String::operator != (const char* s) const
{
    return !(*this == s ? s : "");
}

bool String::operator > (const String& s) const
{
    return (strcmp(m_str,s.m_str) > 0);
}
bool String::operator > (const char* s) const
{
    return (strcmp(m_str,s ? s : "") > 0);
}

bool String::operator < (const String& s) const
{
    return (strcmp(m_str,s.m_str) < 0);
}
bool String::operator < (const char* s) const
{
    return (strcmp(m_str,s ? s : "") < 0);
}

bool String::operator >= (const String& s) const
{
    return (strcmp(m_str,s.m_str) >= 0);
}
bool String::operator >= (const char* s) const
{
    return (strcmp(m_str,s ? s : "") >= 0);
}

bool String::operator <= (const String& s) const
{
    return (strcmp(m_str,s.m_str) <= 0);
}
bool String::operator <= (const char* s) const
{
    return (strcmp(m_str,s ? s : "") <= 0);
}

String String::operator + (const String& s) const
{
    return (*this + s.m_str);
}
String String::operator + (const char* s) const
{
    String ret;

    int len = m_length + strlen(s ? s : "");

    char* str = reinterpret_cast<char*>(malloc(len+1));

    if(str)
    {
        strcpy(str,m_str);
        strcat(str,s ? s : "");

        free(ret.m_str);

        ret.m_str = str;
        ret.m_length = len;
    }
    else
    {
        THROW_EXCEPTION(NoEnoughMemoryException,"No memory to add String values ...");
    }

    return ret;
}

String& String::operator += (const String& s)
{
    return (*this = *this + s.m_str);
}
String& String::operator += (const char* s)
{
    return (*this = *this + s);
}

String& String::operator = (const String& s)
{
    return (*this = s.m_str);
}
String& String::operator = (const char* s)
{
    if(m_str != s )
    {
        char* str = strdup(s ? s : "");\

        if(str)
        {
            free(m_str);

            m_str = str;

            m_length = strlen(m_str);
        }
        else
        {
            THROW_EXCEPTION(NoEnoughMemoryException,"No memory to assign new String value ...");
        }
    }

    return *this;
}
String& String::operator = (char c)
{
    char s[] = {c,'\0'};

    return (*this = s);
}

String::~String()
{
    free(m_str);
}

}

main.cpp

#include <iostream>
#include"DTString.h"

using namespace std;
using namespace DTLib;

void test_1()
{
    cout<<"test_1() begin :"<<endl;

    String s;

    s = 'N';

    cout<<s.str()<<endl;
    cout<<s.length()<<endl;
    cout<<(s == "N")<<endl;
    cout<<(s > "CCC")<<endl;

    s += "yist";
    cout<<s.str()<<endl;
    cout<<s.length()<<endl;
    cout<<(s == "Nyist")<<endl;

    cout<<"test_1() end :"<<endl;
}

void test_2()
{
    cout<<"test_2() begin :"<<endl;

    String a[] = {"D","C","B","A"};

    String min = a[0];
    for(int i=0;i<4;i++)
    {
        if(min > a[i])
            min = a[i];
    }

    cout<<min.str()<<endl;

    cout<<"test_2() end :"<<endl;
}
int main()
{
    test_1();

    test_2();
    
    return 0;
}

                            


3、小结 

            C / C++语言本身不支持字符串类型 

            C语言通过字符数组和一组函数支持字符串操作 

            C++通过自定义字符串类型支持字符串操作 

            字符串类型通过C语言中的字符串函数实现 



猜你喜欢

转载自blog.csdn.net/qq_39654127/article/details/80368793