2022.9.28

#include <iostream>
#include<cstring>
using namespace std;

class my_string
{
private:
    char *str;
    int len;
public:

    my_string()//无参构造
    {
    }
    my_string(const char *s)//有参构造
    {
        str=new char[128];
        memset(str,0,128);
        strcpy(str,s);
    }
    my_string(int n,char ch)
    {
        int i;
        str=new char[128];
        memset(str,0,128);
        for(i=0;i<n;i++)
        {
            str[i]=ch;
        }
        str[i]='\0';
    }
    my_string(const my_string &other)//拷贝构造
    {
        this->str=new char[128];
        memset(str,0,128);
        strcpy(this->str,other.str);
        this->len=other.len;
    }
    my_string& operator=(const my_string &other)//拷贝赋值
    {
        this->str=new char[128];
        memset(this->str,0,128);
        strcpy(this->str,other.str);
        return *this;
    }
    ~my_string()//析构函数
    {
        delete str;
    }
    bool my_empty()//判空
    {
        if(strlen(str)==0)
        {
            return 1;
        }else{
            return 0;
        }
    }
    int my_size()//求长度
    {
        int size=static_cast<int>(strlen(str));
        return size;
    }
    char& at(int pos)//at()
    {
        char *btr=new char[128];
        memset(btr,0,128);
        strcpy(btr,str);
        return btr[pos];
    }
    char* my_str()//转换C风格字符串
    {
        return str;
    }
};


int main()
{
    my_string s1("oh my girl");
    cout<<s1.my_str()<<endl;
    my_string s2(3,'-');
    cout<<s2.my_str()<<endl;
    my_string s3=s1;
    cout<<s3.my_str()<<endl;
    my_string s4;
    s4=s3;
    my_string s5("");
    cout<<s5.my_empty()<<endl;
    cout<<s4.my_empty()<<endl;
    cout<<s1.at(3)<<endl;
    cout<<s1.my_size()<<endl;




    return 0;
}

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值