封装字符串类

封装CString字符串类,并实现如下功能:
1.实现含参构造函数,将字符串作为参数,可以构造一个包含该字符串的字符串对象。
2.实现重载拷贝构造函数,实现深拷贝。
3.实现无参构造函数。
4.重载 [ ] 符号,实现返回字符串对象中指定位置字符的功能。如n[4],返回对象n字符串的第4个字符。
5.实现成员函数,调用时返回对象代表的字符串的长度。
6.重载 ( ) 符号,实现截取字符串的功能,如m(2,8),返回m对象代表的字符串中的第2位到第8位。

#include<iostream>
using namespace std;
class CString {
private:
    char* str;
    int length;
public:
    CString():str(NULL),length(0){}//无参构造函数
    CString(const char*input);//含参构造函数
    CString(const CString&temp);//拷贝构造函数
    ~CString() { delete[]str; length = 0; }//析构函数
    int len() { return length; }//返回长度函数
    char* getString() { return str; }//返回字符串函数
    char operator[](int);//取特定位字符
    CString operator()(int, int);//截取字符串
    friend CString operator+(CString& a, CString& b);//连接字符串
    CString& operator=(CString& temp);//重载等于号
};
using namespace std;
CString::CString(const char*input)//字符串构造函数
{
    int count = 0, i;
    while (input[count] != '\0')//结束后,count在数值上等于字符串的长度
    {
        count++;
    }
    str = new char[count + 1];//申请字符串长度加1的内存空间
    for (i = 0; i<count; i++)
    {
        str[i] = input[i];//依次对字符赋值
    }
    str[i] = '\0';
    length = count;
}
CString::CString(const CString& temp)//拷贝构造函数
{
    int i;
    length = temp.length;
    str = new char[temp.length + 1];//多申请一位,最后一位放'\0'
    for (i = 0; i < temp.length; i++)
    {
        str[i] = temp.str[i];
    }
    str[i] = '\0';
}
char CString::operator[](int temp)//返回字符值
{
    if (temp <= length + 1 && temp >= 0)
    {
        return str[temp - 1];
    }
    else
    {
        cout << "取值超出范围!" << endl;
        return '\0';
    }
}
CString CString::operator()(int l, int r)//截取字符串
{
    CString temp;
    if (l > r || r > this->length || l<1) 
    {
        cout << "输入范围有误!" << endl;
        return temp;
    }
    int i, j;
    temp.length = r - l + 1;
    temp.str = new char[temp.length+1];
    for (i = l - 1, j = 0; i < r; i++, j++)
    {
        temp.str[j] = this->str[i];
    }
    temp.str[j] = '\0';
    return temp;
}
CString operator+(CString& a, CString& b)//字符串连接
{
    int i,j;
    CString temp;
    temp.length = a.length + b.length;
    temp.str = new char[temp.length + 1];
    for (i = 0; i<a.length; i++)
    {
        temp.str[i] = a.str[i];
    }
    for (i = a.length, j = 0; j<b.length; i++, j++)
    {
        temp.str[i] = b.str[j];
    }
    temp.str[a.length + b.length] = '\0';
    return temp;
}
CString& CString::operator=(CString& temp) 
{
    int i;
    this->length = temp.length;
    delete[]this->str;
    this->str = new char[temp.length + 1];
    for (i = 0; i < temp.length; i++)
    {
        this->str[i] = temp.str[i];
    }
    this->str[i] = '\0';
    return *this;
}
    int main()
{
    CString m("software");//含参构造函数
    CString n(m);//拷贝构造函数
    CString o;//无参构造函数
    //调用getString()函数显示结果
    cout << "含参构造函数结果:" << m.getString() << endl;
    cout << "拷贝构造函数结果:" << n.getString() << endl;

    o = m;//使用拷贝构造函数
    cout << "使用等号调用拷贝构造函数结果:" << o.getString() << endl;

    cout << "重载中括号结果:" << n[4] << endl;//重载中括号
    cout << "返回n的字符串长度:" << n.len() << endl;//成员函数返回字符串长度

    o = m + n;//重载+号,连接字符串
    cout << "连接字符串结果:" << o.getString() << endl;

    o = m(2, 8);//重载(),截取字符串
    cout << "截取字符串结果:" << o.getString() << endl;

    system("pause");
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值