实现了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();