作业:
头文件:
#ifndef MY_STRING_H
#define MY_STRING_H
#include <cstring>
class My_string
{
private:
char *ptr; //指向字符数组的指针
int size; //字符串的最大容量
int len; //字符串当前容量
public:
//无参构造
My_string();
//有参构造
My_string(const char* src);
My_string(int num, char value);
//拷贝构造
My_string(const My_string & other);
//拷贝赋值
//析构函数
~My_string();
//判空
bool empty();
//尾插
void push_back(char value);
//尾删
void pop_back();
//at函数实现
char at(int index);
//清空函数
void clear();
//返回C风格字符串
char *data();
//返回实际长度
int get_length();
//返回当前最大容量
int get_size();
};
#endif // MY_STRING_H
源文件:
#include <iostream>
#include "MY_string.h"
using namespace std;
My_string::My_string():size(15)
{
this->ptr = new char[size];
this->ptr[0] = '\0'; //表示串为空串
this->len = 0;
}
//有参构造
My_string::My_string(const char* src)
{
size = strlen(src);
ptr = new char[size +1];
strcpy(ptr,src);
}
//拷贝构造
My_string::My_string(const My_string & other)
{
}
//拷贝赋值
//析构函数
My_string::~My_string()
{
delete ptr;
}
//判空
bool My_string::empty()
{
return this->len==0;
}
//尾插
void push_back(char value);
//尾删
void pop_back();
//at函数实现
char My_string::at(int index)
{
if(index>=0 && index<size)
{
return ptr[index];
}
else
{
cout<<"超出范围"<<endl;
exit(1);
}
}
//清空函数
void clear();
//返回C风格字符串
char *My_string::data()
{
return this->ptr;
}
//返回实际长度
int My_string::get_length()
{
return this->len;
}
//返回当前最大容量
int My_string::get_size()
{
return this->size;
}
主函数:
#include <iostream>
#include <cstring>
#include "MY_string.h"
using namespace std;
int main()
{
return 0;
}

5560

被折叠的 条评论
为什么被折叠?



