数组(三)

该博客主要展示了C++中自定义字符串类的实现。定义了字符串类String,包含构造函数、析构函数,对操作符进行了重载,如赋值、索引、加法等操作符。最后通过main函数进行了测试,展示了字符串类的使用方法和操作符重载后的效果。
字符串类

None.gif#include <iostream>
None.gif
using namespace std;
None.gif
None.gif
class String
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif
public:
InBlock.gif    
// constructors
InBlock.gif
    String();
InBlock.gif    String(
const char* const);
InBlock.gif    String(
const String&);
InBlock.gif
InBlock.gif    
~String();
InBlock.gif
InBlock.gif    
// overloaded operators
InBlock.gif
    char& operator [](unsigned short offset);
InBlock.gif    
char operator [](unsigned short offset) const;
InBlock.gif    String 
operator +(const String&);
InBlock.gif    
void operator += (const String&);
InBlock.gif    String
& operator = (const String&);
InBlock.gif
InBlock.gif    
// general accessors
ExpandedSubBlockStart.gifContractedSubBlock.gif
    unsigned short GetLen() const dot.gifreturn itsLen; }
ExpandedSubBlockStart.gifContractedSubBlock.gif    
const char* GetString() const dot.gifreturn itsString; }
InBlock.gif
InBlock.gif
private:
InBlock.gif    String(unsigned 
short);  // private constructor
InBlock.gif
    char* itsString;
InBlock.gif    unsigned 
short itsLen;
ExpandedBlockEnd.gif}
;
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**/////////////////////////////////////////////////////////////////////
None.gif// 构造函数
None.gif

ExpandedBlockStart.gifContractedBlock.gifString::String() 
dot.gif{
InBlock.gif    itsString 
= new char[1];
InBlock.gif    itsString[
0= '\0';
InBlock.gif    itsLen 
= 0;
ExpandedBlockEnd.gif}

None.gif
ExpandedBlockStart.gifContractedBlock.gifString::String(unsigned 
short len) dot.gif{
InBlock.gif    itsString 
= new char[len + 1];
InBlock.gif
InBlock.gif    
for (unsigned short i = 0; i <= len; i++)
InBlock.gif        itsString[i] 
= '\0';
InBlock.gif    itsLen 
= len;
ExpandedBlockEnd.gif}

None.gif
ExpandedBlockStart.gifContractedBlock.gifString::String(
const char* const cString) dot.gif{
InBlock.gif    itsLen 
= strlen(cString);    
InBlock.gif    itsString 
= new char[itsLen + 1];
InBlock.gif
InBlock.gif    
for (unsigned short i = 0; i < itsLen; i++)
InBlock.gif        itsString[i] 
= cString[i];
InBlock.gif    itsString[itsLen] 
= '\0';
ExpandedBlockEnd.gif}

None.gif
ExpandedBlockStart.gifContractedBlock.gifString::String(
const String& rhs) dot.gif{
InBlock.gif    itsLen 
= rhs.GetLen();
InBlock.gif    itsString 
= new char[itsLen + 1];
InBlock.gif
InBlock.gif    
for (unsigned short i = 0; i < itsLen; i++)
InBlock.gif        itsString[i] 
= rhs[i]; // 这里的访问操作符在后面要实现之。
InBlock.gif
    itsString[itsLen] = '\0';
ExpandedBlockEnd.gif}

None.gif
ExpandedBlockStart.gifContractedBlock.gifString::
~String() dot.gif{
InBlock.gif    delete [] itsString;
InBlock.gif    itsLen 
= 0;
ExpandedBlockEnd.gif}

None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**////////////////////////////////////////////////////////////////////////
None.gif// 以下实现自定义的操作符重载
None.gif

ExpandedBlockStart.gifContractedBlock.gifString
& String::operator = (const String& rhs) dot.gif{
InBlock.gif    
// 重要的判断!
InBlock.gif
    if (this == &rhs)
InBlock.gif        
return *this;
InBlock.gif    delete [] itsString;
InBlock.gif    itsLen 
= rhs.GetLen();
InBlock.gif    itsString 
= new char[itsLen + 1];
InBlock.gif
InBlock.gif    
for (unsigned short i = 0; i < itsLen; i++)
InBlock.gif        itsString[i] 
= rhs[i];
InBlock.gif    itsString[itsLen] 
= '\0';
InBlock.gif
InBlock.gif    
return *this;
ExpandedBlockEnd.gif}

None.gif
ExpandedBlockStart.gifContractedBlock.gif
char& String::operator [](unsigned short offset) dot.gif{
InBlock.gif    
if (offset > itsLen)
InBlock.gif        
return itsString[itsLen - 1];
InBlock.gif    
else
InBlock.gif        
return itsString[offset];
ExpandedBlockEnd.gif}

None.gif
ExpandedBlockStart.gifContractedBlock.gif
char String::operator [](unsigned short offset) const dot.gif{
InBlock.gif    
if (offset > itsLen)
InBlock.gif        
return itsString[itsLen - 1];
InBlock.gif    
else
InBlock.gif        
return itsString[offset];
ExpandedBlockEnd.gif}

None.gif
ExpandedBlockStart.gifContractedBlock.gifString String::
operator +(const String& rhs) dot.gif{
InBlock.gif    unsigned 
short totalLen = itsLen + rhs.GetLen();
InBlock.gif    String temp 
= String(totalLen);
InBlock.gif
InBlock.gif    unsigned 
short i, j;
InBlock.gif    
for (i = 0; i < itsLen; i++)
InBlock.gif        temp[i] 
= itsString[i];
InBlock.gif    
for (j = 0; j < rhs.GetLen(); i++, j++)
InBlock.gif        temp[i] 
= rhs[j];
InBlock.gif    temp[totalLen] 
= '\0';
InBlock.gif
InBlock.gif    
return temp;
ExpandedBlockEnd.gif}

None.gif
ExpandedBlockStart.gifContractedBlock.gif
void String::operator += (const String& rhs) dot.gif{
InBlock.gif    unsigned 
short rhsLen = rhs.GetLen();
InBlock.gif    unsigned 
short totalLen = itsLen + rhsLen;
InBlock.gif    String temp(totalLen);
InBlock.gif
InBlock.gif    unsigned 
short i, j;
InBlock.gif    
for (i = 0; i < itsLen; i++)
InBlock.gif        temp[i] 
= itsString[i];
InBlock.gif    
for (j = 0; j < rhsLen; i++, j++)
InBlock.gif        temp[i] 
= rhs[j];
InBlock.gif    temp[totalLen] 
= '\0';
InBlock.gif
InBlock.gif    
*this = temp;    
ExpandedBlockEnd.gif}

None.gif
None.gif
int main(int argc, char *argv[])
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    String s1(
"initial test");
InBlock.gif    cout 
<< "s1: \t" << s1.GetString() << endl;
InBlock.gif
InBlock.gif    
char* temp = "Hello World";
InBlock.gif    s1 
= temp;
InBlock.gif    cout 
<< "s1: \t" << s1.GetString() << endl;
InBlock.gif
InBlock.gif    
char tempTwo[20];
InBlock.gif    strcpy(tempTwo, 
"; nice to be here!");
InBlock.gif    s1 
+= tempTwo;
InBlock.gif    cout 
<< "tempTwo: \t" << tempTwo << endl;
InBlock.gif    cout 
<< "s1: \t" << s1.GetString() << endl;
InBlock.gif
InBlock.gif    cout 
<< "s1[4]: \t" << s1[4<< endl;
InBlock.gif    s1[
4= 'x';
InBlock.gif    cout 
<< "s1: \t" << s1.GetString() << endl;
InBlock.gif
InBlock.gif    cout 
<< "s1[999]: \t" << s1[999<< endl;
InBlock.gif
InBlock.gif    String s2(
"Another string");
InBlock.gif    String s3;
InBlock.gif    s3 
= s1 + s2;
InBlock.gif    cout 
<< "s3: \t" << s3.GetString() << endl;
InBlock.gif
InBlock.gif    String s4;
InBlock.gif    s4 
= "Why does this work?";
InBlock.gif    cout 
<< "s4: \t" << s4.GetString() << endl;
InBlock.gif     
InBlock.gif    
return 0;
ExpandedBlockEnd.gif}

None.gif
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值