【C++】手动实现String类的封装(分文件编译)

        实现了String类的大部分封装,采用分文件编译 

//mystring.h
#ifndef MYSTRING_H
#define MYSTRING_H

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

class myString
{
private:
    char *str;    //定义一个字符串
    int size;     //记录字符串长度

public:
    //成员函数:

    //1.构造函数
    myString(const char* s);

    //2.析构函数
    ~myString();

    //3.为字符串赋值函数 "="
    myString & operator=(const myString &other);

    //4.访问指定字符,有边界检查 "at"
    char &s_at(int index);

    //5.访问指定字符 "operator[]"
    char &s_operator(int index);

    //6.返回指向字符串首字符的指针 "data"
    char *s_data();

    //7.返回字符串的不可修改的C字符数组版本 "c_str"
    const char* s_c_str()const;

    //8.检查字符串是否为空 "empty"
    bool empty();

    //9.返回字符串长度 "size"
    int s_size();

    //10.返回字符串长度 "length"
    int s_lenth();

    //11.返回当前对象分配的存储空间能保存的字符数量 "capacity"
    int s_capacity();

   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值