C++封装My_string

My_string类中的所有能重载的运算符全部进行重载

头文件
#ifndef SEQLIST_H
#define SEQLIST_H


#include <iostream>
#include <cstring>
using namespace std;
using datatype = char;

class My_string
{
private:
    datatype *ptr; //顺序表字符数组
    int size = 15;     //数组的最大
    int len;  //数组的实际长度

public:
    My_string();    //无参构造

    //有参构造
    My_string(const char* src);

    My_string(int num,char value);
    //拷贝构造
    My_string(const My_string &other);
    //拷贝赋值
    My_string &operator = (const My_string &other);
    //析构函数
    ~My_string();
    bool empty();      //判空

    void push_back(datatype value); //尾插

    void pop_back(); //尾删

    int listsize();  //求长度

    datatype & at(int inex);  //获取任意位置元素

    void clear();//清空

    char *data();//返回C风格字符串

    int get_length();//返回当前最大容器
    void show();         //展示
    void append(const char *ptr);//扩容
    const My_string operator+(const My_string &other)const;//+运算符重载
    char &operator[](int n);//[]运算符重载
    bool operator>(const My_string &R)const;//>重载
    bool operator<(const My_string &R)const;//<重载
    bool operator==(const My_string &R)const;//==重载
    bool operator>=(const My_string &R)const;//>=重载
    bool operator<=(const My_string &R)const;//<=重载
    bool operator!=(const My_string &R)const;//!=重载
    My_string &operator +=(const My_string &R);//+=重载

    friend ostream &operator<<(ostream &L,const My_string &R);
    friend istream &operator>>(istream &L, My_string &R);
};

#endif // SEQLIST_H
源文件
#include "seqlist.h"

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)
{
    len = strlen(src)+1;
    size = len >size?size*2:size;
    this->ptr = new char[size];
    strcpy(ptr,src);
}

My_string::My_string(int num,char value)
{
    size = num >size?size*2:size;
    ptr = new char[size];
    for(int i = 0;i<num;i++)
    {
        this->ptr[i] = value;
    }
    this->len = num;
}
//拷贝构造
My_string::My_string(const My_string &other):ptr(new char[other.size]),size(other.size),len(other.len)
{
    strcpy(this->ptr,other.ptr);
    this->size = other.size;
    this->len = other.len;
}
//拷贝赋值
My_string &My_string::operator = (const My_string &other)
{
    if(this != &other)
    {
        delete []ptr;
        size = other.size;
        ptr = new char[size + 1];
        strcpy(ptr,other.ptr);
    }
    return *this;
}
//析构函数
My_string::~My_string()
{
    delete []ptr;
}
char *My_string::data()//返回C风格字符串
{
    return ptr;
}
//判空
bool My_string::empty()
{
    return ptr[0] == 0;
}

//尾插
void My_string::push_back(datatype value)
{
    this->ptr[len++] = value;
}

//尾删
void My_string::pop_back()
{
    if(this->empty())
    {
        cout<<"表为空无删除对象"<<endl;
        return;
    }
    this->len--;
}
//求长度
int My_string::listsize()
{
    return this->len;
}
//获取任意位置元素
datatype & My_string::at(int index)
{
    if(this->empty())
    {
        throw std::out_of_range("表为空无对象");
    }
    if(index>this->len||index<=0)
    {
        throw std::out_of_range("位置错误");
    }
    return this->ptr[index-1];
}

//展示
void My_string::show()
{
    if(this->empty())
    {
        cout<<"表为空无对象"<<endl;
        return;
    }
    cout<<"当前顺序表中的元素是:";

    cout<<"ptr = "<<ptr<<" ";
    len = strlen(ptr);
    cout<<"len = "<<len<<endl;
}
int My_string::get_length()//返回当前最大容器
{
    return this->size;
}
void My_string::clear()//清空
{
    delete []ptr;
    ptr = new char[1];
    ptr[0] = '\0';
    len = 0;
}

void My_string::append(const char *src)
{
    int src_len = strlen(src);
    while(len+src_len >= size)
    {
        size *= 2;
        char *new_ptr = new char[size];
        strcpy(new_ptr,ptr);
        delete []ptr;
        ptr = new_ptr;
    }
    strcat(ptr,src);
    len += src_len;
}
const My_string My_string::operator+(const My_string &other)const//+运算符重载
{
    My_string temp;
    delete []temp.ptr;
    temp.len =this->len+other.len;
    temp.ptr = new char(temp.len+1);
    strcpy(temp.ptr,this->ptr);
    strcat(temp.ptr,other.ptr);

    return temp;

}
char &My_string::operator[](int n)//[]运算符重载
{
    if(n<0||n>len)
    {
        cout<<"数组越界"<<endl;
    }
    else
    {
        return ptr[n-1];
    }
}
bool My_string::operator>(const My_string &R)const//>重载
{
    return strcmp(this->ptr,R.ptr)>0;
}
bool My_string::operator<(const My_string &R)const//<重载
{
    return strcmp(this->ptr,R.ptr)<0;
}
bool My_string::operator==(const My_string &R)const//==重载
{
    return strcmp(this->ptr,R.ptr)==0;
}
bool My_string::operator>=(const My_string &R)const//>=重载
{
    return strcmp(this->ptr,R.ptr)>=0;
}
bool My_string::operator<=(const My_string &R)const//<=重载
{
    return strcmp(this->ptr,R.ptr)<=0;
}
bool My_string::operator!=(const My_string &R)const//!=重载
{
    return strcmp(this->ptr,R.ptr)!=0;
}
My_string &My_string::operator +=(const My_string &R)//+=重载
{
    My_string temp;
    temp.ptr = new char(len+1);
    strcpy(temp.ptr,this->ptr);
    temp.len = this->len;
    delete [] ptr;
    this->ptr = new char(temp.len+R.len+1);
    strcpy(ptr,temp.ptr);
    strcat(ptr,R.ptr);
    return  *this;
}
ostream &operator<<(ostream &L,const My_string &R)
{
    L<<R.ptr<<endl;
    return L;
}
istream &operator>>(istream &L, My_string &R)
{
    L>>R.ptr;
    return L;
}
主程序
#include "seqlist.h"
int main()
{
    My_string s1("Hello");
    //拷贝构造
    My_string s2 = s1;
    s2.show();

    //无参构造
    My_string s3;
    //拷贝赋值
    s3 = s1;
    s3.show();
    s3.push_back('a');
    //C字符风格
    cout<<s3.data()<<endl;
    s3.pop_back();

    s3.append(" world ,good");
    s3.show();
    //求实际长度
    cout<<"s3容器的实际长度 = "<<s3.listsize()<<endl;
    cout<<"s3容器的最大长度 = "<<s3.get_length()<<endl;

    cout<<"s3容器2号元素= "<<s3.at(2)<<endl;
    //clear
    s3 = s2+s1;
    s3.show();
    cout<<"s3[2]= "<<s3[2]<<endl;
    My_string s4("good");
    s4+=s3;
    cout<<s4;
    return 0;
}
思维导图

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值