新建String类,重载运算符实现相关操作

本文分享了一名初学者如何使用C++实现字符串类的重载+运算符拼接、单目运算符转换大小写和~运算符大小写切换,以及如何重载[]运算符进行子串操作,包括实例代码和可能的问题解析。

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

 前言:刚学习c++,老师布置了两次试验任务用来测试重载运算符的学习情况,分享一下我的内容,望路过的各位不吝赐教。

一、实验基本要求:
设计字符串操作类String,
1.重载+运算符实现两个字符串拼接(支持连加)
2.重载单目运算符+/-实现返回纯大写/小写字符串
3.重载~(非符号)实现返回大小写切换后的字符串。

以下代码参考了b站一位up主的教学视频,贴上地址

C++重载运算符的运算_哔哩哔哩_bilibili

#include<iostream>
#include<cstringt.h>
#include<string.h>
using namespace std;

class CString
{
private:
    char* m_str;
public:
    //有参构造函数赋值
    CString(const char* str)
    {
        //动态内存分配
        m_str = new char[strlen(str) + 1];
        //赋值
        strcpy(m_str, str);
    }
    //析构函数
    ~CString()
        //判断是否占用内存空间
    {
        if (m_str)
        {
            //内存释放
            delete[]m_str;
        }
    }
    //拷贝构造函数
    CString(const CString& other)
    {
        if (this != &other)
        {
            m_str = new char[strlen(other.m_str)+1];
            strcpy(m_str, other.m_str);
        }
        //调用情况:
        //1.用一个对象去初始化另一个对象
        //2.函数参数是类的对象的时候,且是传值而不是引用
        //3.当函数返回值是其他对象时
    }
    //重载“+”号运算符,本质实现的是两个类对象相加
    CString operator+(const CString& other_str)
        //直接采用实参,提高效率
    {
        //判断,分动态配内存,返回
        int len1 = strlen(m_str);
        int len2 = strlen(other_str.m_str);
        char* temp = new char[len1 + len2];
        strcpy(temp, m_str);//将m_str拷贝赋值到temp中
        strcat(temp, other_str.m_str);//将other链接到temp中
        CString x(temp);//返回值类型是CString,temp表示的是字符型指针,因此进行替换返回
        return x;//3.x属于其他的对象
    }
    //获取x的值
    const char* getstr() const
    {
        return m_str;
    }
};

//沿用第一种方式,进行“++”与“--”单目运算符的重载
class NString
{
private:
    char* m1_str;
public:
    //有参构造函数赋值
    NString(const char* str)
    {
        //动态内存分配
        m1_str = new char[strlen(str)+1];
        //赋值
        strcpy(m1_str, str);
    }
    //析构函数
    ~NString()
        //判断是否占用内存空间
    {
        if (m1_str)
        {
            //内存释放
            delete[]m1_str;
        }
    }
    //拷贝构造函数
    NString(const NString& other)
    {
        if (this != &other)
        {
            m1_str = new char[strlen(other.m1_str)+1];
            strcpy(m1_str, other.m1_str);
        }
    }
    //重载“++”号运算符,实现大写转化
    NString operator++(int)
    {
        int len1 = strlen(m1_str)+1;
        char* temp = new char[len1];
        //采用循环进行遍历,实现转化功能
        for (int i = 0; i < len1; i++)
        {
            temp[i] = toupper(m1_str[i]);//toupper()函数:小写转大写
        }
        NString x(temp);
        return x;
    }
    const char* getstr1() const
    {
        return m1_str;
    }
    //重载“--”号运算符,实现小写转化
    NString operator--(int)
    {
        int len1 = strlen(m1_str)+1;
        char* temp = new char[len1];
        //采用遍历转化
        for (int i = 0; i < len1; i++)
        {
            temp[i] = tolower(m1_str[i]);//tolower()函数:大写转小写
        }
        NString x(temp);
        return x;
    }
    const char* getstr2() const
    {
        return m1_str;
    }
};

//继续沿用第一种定义方式,改变重载定义即可
class MString
{
private:
    char* m2_str;
public:
    //有参构造函数赋值
    MString(const char* str)
    {
        //动态内存分配
        m2_str = new char[strlen(str) + 1];
        //赋值
        strcpy(m2_str, str);
    }
    //析构函数
    ~MString()
        //判断是否占用内存空间
    {
        if (m2_str)
        {
            //内存释放
            delete[]m2_str;
        }
    }
    //拷贝构造函数
    MString(const MString& other)
    {
        if (this != &other)
        {
            m2_str = new char[strlen(other.m2_str) + 1];
            strcpy(m2_str, other.m2_str);
        }
    }

    //重载“~”号运算符,实现大小写相互转化
    friend MString operator~(const MString& other_str)
    {
        int len1 = strlen(other_str.m2_str) + 1;
        char* temp = new char[len1];
        strcpy(temp, other_str.m2_str);
        //采用循环进行遍历,实现转化功能
        for (int i = 0; i < len1; i++)
        {
            if (isupper(temp[i]))
            {
                temp[i] = tolower(temp[i]);
            }
            else
                temp[i] = toupper(temp[i]);
        }
        MString x(temp);
        return x;
    }
    const char* getstr3() const
    {
        return m2_str;
    }
};

int main()
{
    CString one("abc");
    CString two("def");
    CString three = one + two;
    cout << three.getstr() << endl;
    NString four("ght");
    NString five = four ++;
    NString six("ASDAS");
    NString seven =six--;
    cout << five.getstr1() << endl;
    cout << seven.getstr2() << endl;
    MString nine("abcdFDHA");
    MString ten = ~nine; 
    cout << ten.getstr3() << endl;
}

 二、重载[]运算符,因为分了两次实验,所以这个单列出来了:

针对String类,通过重载[]运算符实现求子串操作:

对于字符串对象S,String S=”abcdefg”

S[“2:6”]能得到包含下标2到5之间的子串“cdef”相对应的String对象

,S[“2:”]能得到从下标2起到最末尾的子串“cdefg”

S[“:4”]能得到从字符串开始到下标4之前的子串“abc”

代码如下: 

#include<iostream>
#include<cstringt.h>
#include<string.h>
using namespace std;

class QString
{
private:
    int x, y;
    char* m_str;
public:
    QString(const char* str)
    {
        m_str = new char[strlen(str)+1];
        strcpy(m_str, str);
    }
    ~QString()
    {
        if (m_str)
        {
            delete[]m_str;
        }
    }
    QString(const QString& other)
    {
        if (this != &other)
        {
            m_str = new char[strlen(other.m_str) + 1];
            strcpy(m_str, other.m_str);
        }
    }
    //重载“[]”号运算符,实现的截取子串
    QString operator[](const char a[4])
    {
        int len1 = strlen(m_str),len=0;
        //ASCII码转换
        int x = (int)a[0] - '0';
        if (a[0] == ' ')
            x = x + 16;
        int y = (int)a[2] - '0';
        //注意不同情况内存要变换
        if (a[2] == ' ')
        {
            len = len1-x-1;
        }
        else
        {
            len = y - x;
        }
        //分配内存
        char* temp = new char[len-1];
        char* temp1 = new char[len1+1];
        strcpy(temp1, m_str);
        //不同情况下的判断,这里用了比较简单的循环,高级的暂时...
        //("2,6")
        if (a[0] > ' ' && a[2] > ' ')
        {
            for (int i = 0, n = x; i < len; i++, n++)
            {
                temp[i] = temp1[n];
            }
        }
        //(" ,6")
        else if (a[0] <= ' ' && a[2] >= ' ')
        {
            for (int i = 0; i < len; i++)
            {
                temp[i] = temp1[i];
            }
        }
        //("2, ")
        else if (a[0] >=' '&& a[2] <= ' ')
        {
            for (int i = x,n=0; i < len1 ;n++,i++)
            {
                temp[n] = temp1[i];
            }
        }       
        QString q(temp);
        return q;
    }
    const char* getstr() const
    {
        return m_str;
    }
};

int main()
{
    QString s("sawdsawdsa");
    QString s1 = s[("2, ")];
    cout << s1.getstr() << endl;
    QString s2 = s[("2,6")];
    cout << s2.getstr() << endl;
    QString s3 = s[(" ,6")];
    cout << s3.getstr() << endl;
}

tips:作为刚学习编程的小菜鸟,以上代码仍然存在问题,好像是内存分配上有错误,导致编译出来的结果会出现有乱码的情况。如果有路过的小伙伴知道的话,可以评论留言,今天分享就到这了,谢谢观看。

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值