(转)一个有用的ANSI字符串类String的实现及使用演示

下面的程序分成两个部分:

(1)String类:类头String.h和类实现String.cpp

(2)String类使用演示程序Main.cpp

 

类头文件String.h代码如下:

 

#ifndef STRING_H
#define STRING_H
#include <iostream>
using namespace std;

class String
{
    friend ostream & operator<<(ostream & output, const String & s);
    friend istream & operator>>(istream & input, String & s);

public:
    String(const char* = "");
    String(const String &);
    ~String();
    const String & operator=(const String &);   //赋值
    String & operator+=(const String &);        //字符串连接
    int operator!() const;                      //String为空?
    int operator==(const String &) const;       //测试s1==s2
    int operator!=(const String &) const;       //测试s1!=s2
    int operator<(const String &) const;        //测试s1<s2
    int operator>(const String &) const;        //测试s1>s2
    int operator<=(const String &) const;       //测试s1<=s2
    int operator>=(const String &) const;       //测试s1>=s2
    char & operator[](int);                     //返回对字符的引用
    String & operator()(int, int);              //返回一个子字符串
    int GetLength() const;                      //返回字符串的长度,不包括结尾的/0
private:
    char* sPtr;                                 //指向字符串起始位置的指针
    int length;                                 //字符串的长度
};

#endif
类实现文件String.cpp代码如下:

 

#include <iostream>
#include <string.h>
#include <assert.h>
#include "String.h"

ostream & operator<<(ostream & output, const String & s)
{
    output<<s.sPtr;
    return output;
}

istream & operator>>(istream & input, String & s)
{
    static char temp[100];
    input>>temp;
    s = temp;
    return input;
}

String::String(const char * s)
{
    cout<<"Convention constructor:"<<s<<endl;
    length = strlen(s);
    sPtr = new char[length+1];
    assert(sPtr!=0);
    strcpy(sPtr, s);
}

String::String(const String & copy)
{
    cout<<"Copy constructor:"<<copy.sPtr<<endl;
    length = copy.length;
    sPtr = new char[length+1];
    assert(sPtr != 0);
    strcpy(sPtr, copy.sPtr);
}

String::~String()
{
    cout<<"Destructor:"<<sPtr<<endl;
    delete[] sPtr;
}

const String & String::operator=(const String & right)
{
    cout<<"operator = called"<<endl;
    if(&right != this)   //避免自我赋值
    {
        delete[] sPtr;
        length = right.length;
        sPtr = new char[length+1];
        assert(sPtr!=0);
        strcpy(sPtr, right.sPtr);
    }
    else
    {
        cout<<"Attempt assignment of a String to itself/n";
    }
    return *this;
}

String & String::operator+=(const String & right)
{
    char* tempPtr = sPtr;
    length += right.length;
    sPtr = new char[length+1];
    assert(sPtr!=0);
    strcpy(sPtr, tempPtr);
    strcat(sPtr, right.sPtr);
    delete[] tempPtr;
    return *this;
}

//字符串为空?
int String::operator !() const
{
    return length == 0;
}

int String::operator==(const String & right) const
{
    return strcmp(sPtr, right.sPtr)==0;
}

int String::operator!=(const String & right) const
{
    return strcmp(sPtr, right.sPtr)!=0;
}

int String::operator<(const String & right) const
{
    return strcmp(sPtr, right.sPtr)<0;
}

int String::operator>(const String & right) const
{
    return strcmp(sPtr, right.sPtr)>0;
}

 

int String::operator<=(const String & right) const

{
    return strcmp(sPtr, right.sPtr)<=0;
}

int String::operator>=(const String & right) const
{
    return strcmp(sPtr, right.sPtr)>=0;
}

//返回对String中某个字符的引用
char & String::operator [](int subscript)
{
    //首先测试下标是否越界
    assert(subscript>=0 && subscript<length);
    return sPtr[subscript];
}

String & String::operator()(int index, int sublength)
{
    //首先测试下标是否越界
    assert(index>=0 && index<length && sublength >= 0);

    String * subPtr = new String;
    assert(subPtr!=0);

    //计算子字符串长度
    int size = 0;
    if(sublength == 0 || (index+sublength>length))
    {
        size = length - index + 1;
    }
    else
    {
        size = sublength + 1;
    }
    //分配子字符串内存
    delete subPtr->sPtr;
    subPtr->length = size;
    subPtr->sPtr = new char[size];
    assert(subPtr->sPtr!=0);

    int i = index, j=0;
    //把子字符串拷贝到新的对象中
    for( ; i<index+size-1; i++, j++)
    {
        subPtr->sPtr[j] = sPtr[i];
    }

    subPtr->sPtr[j] = '/0';
    return *subPtr;
}

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

你可能感兴趣的:(String,测试,delete,iostream,Constructor,destructor)