c++的封装

实现my_string中可能实现的功能

cpp 复制代码
#include <iostream>
#include<cstring>
using namespace std;
class my_string
{
    char *str;    //记录c风格的字符串
    int size;     //记录字符串的实际长度
    int capacit;//记录最大容量

public:
    //无参的构造函数
    my_string():size(0),capacit(15){
       str = new char[capacit];
    }
    // 带参数的构造函数
     my_string(const char *s):capacit(15) {
         size = strlen(s) ;
         if(size>capacit){
             extend();
         }
         str = new char[capacit];
         strcpy(str, s);
     }
     //拷贝构造函数
     my_string(const my_string &other):str(new char[other.capacit]),size(other.size),capacit(other.capacit){
         strcpy(str,other.str);
         cout<<"拷贝构造函数"<<endl;
     }
     //移动构造函数
     my_string(const my_string &&other):str(new char[other.capacit]),size(other.size){
         strcpy(str,other.str);
         cout<<"拷贝构造函数"<<endl;
     }
     //拷贝赋值函数
     my_string &operator=(const my_string&other){
         if(this!=&other){
             size=other.size;
             capacit=other.capacit;
             str=new char[capacit];
             strcpy(str,other.str);
         }
         cout<<"拷贝赋值函数"<<endl;
         return *this;
     }
     //移动赋值函数
     my_string &operator=(const my_string &&other){
         if(this!=&other){
             size=other.size;
             capacit=other.capacit;
             str=new char[capacit];
             strcpy(str,other.str);
         }
         cout<<"拷贝赋值函数"<<endl;
         return *this;
     }
     ~my_string(){
         delete []str;
     }
     friend bool operator<=(const my_string &L,const my_string &R);
     friend bool operator>=(const my_string &L,const my_string &R);
     friend istream &operator>>(istream &L,my_string &R);
     friend ostream &operator<<(ostream &L,const my_string &R);
     friend bool operator>(const my_string &L,const my_string &R);
     friend bool operator<(const my_string &L,const my_string &R);
     friend bool operator!=(const my_string &L,const my_string &R);
     friend bool operator==(const my_string &L,const my_string &R);
     friend const my_string operator+(const my_string &L,const my_string &R);
     bool my_string_empty();//判空函数
     int str_size();     //字符串长度
     char * my_c_ctl(){//将c++的字符串转化为c风格的字符串
         return str;
     }
     char &at(int pos){//仿at功能

         return str[pos];
     }
     void extend();//二倍扩容
     void show();
     int capacity(){
         return this->capacit;
     }
     //清空字符串
     void clear(){
         str[0]='\0';
     }
     //push_back
     void push_back(char a){
         if(size>=capacit){
             extend();
         }
         str[this->size]=a;
         size++;
     }
     //pop_back
     bool pop_back(){
         if(size==0){ return false;}
         str[size-1]='\0';
         size--;
         return true;
     }
     //运算符+=重载函数
     void operator+=(char a);
};
void my_string::operator+=(char a){
    if(size>=capacit){
        extend();
    }
    str[this->size]=a;
    size++;
}
void my_string::extend(){
    char *arg;
    arg=new char[capacit*2+1];//在堆区申请一个两倍capacity大小的空间
    strcpy(arg,str);//将原先str字符串复制到arg中;
    delete []str;//删除原先的在堆区的str
    str=arg;
}
void my_string::show(){
   cout<<str<<endl;
}
bool my_string::my_string_empty(){//判空函数
    if(strlen(str)==0){
        return true;
    }
    else
        return false;
}
int my_string::str_size(){     //字符串长度
    return strlen(str);
}
//运算符+重载函数
const my_string operator+(const my_string &L,const my_string &R){
    my_string temp;
    temp.size = L.size + R.size;
    temp.capacit=L.capacit;
    if(temp.size>=temp.capacit){
        temp.extend();
    }
    temp.str = new char[temp.capacit];
    strcpy(temp.str, L.str);
    strcat(temp.str, R.str);
    return temp;
}


//运算符==重载函数
bool operator==(const my_string &L,const my_string &R){
    return strcmp(L.str,R.str)==0;
}
//运算符!=重载函数
bool operator!=(const my_string &L,const my_string &R){
    return strcmp(L.str,R.str)!=0;
}
//运算符>重载函数
bool operator>(const my_string &L,const my_string &R){
    return strcmp(L.str,R.str)>0;
}
//运算符>=重载函数
bool operator>=(const my_string &L,const my_string &R){
    return strcmp(L.str,R.str)>=0;
}
//运算符<=重载函数
bool operator<=(const my_string &L,const my_string &R){
    return strcmp(L.str,R.str)<=0;
}
//运算符<重载函数
bool operator<(const my_string &L,const my_string &R){
    return strcmp(L.str,R.str)<0;
}
//<<重载函数
ostream &operator<<(ostream &L,const my_string &R){
    L<<R.str<<endl;
    return L;
}
//>>重载函数
istream &operator>>(istream &L,my_string &R){
    L>>R.str;
    R.size=strlen(R.str);
    return L;
}
int main()
{
    my_string s1("aaa");
    my_string s2(s1);
    s2.show();
    cout<<"********************************"<<endl;
    my_string s3;
    s3=s2;
    s3.show();
    cout<<"********************************"<<endl;
    my_string s4=s1+s2;
    s4.show();
    cout<<"********************************"<<endl;
    if(s4!=s1){
        cout<<"yes"<<endl;
    }
    else{
        cout<<"no"<<endl;
    }
    cout<<"********************************"<<endl;
    my_string s5="abcd";
    cout<<s5;
    cout<<"********************************"<<endl;
    my_string s6;
    cin>>s6;
    cout<<s6;
    cout<<"********************************"<<endl;
    s6.push_back('a');
    cout<<s6;
    cout<<"********************************"<<endl;
    s6.clear();
    cout<<s6;
    cout<<"********************************"<<endl;
    cout<<s5.capacity();
    return 0;
}
相关推荐
Mr_Xuhhh20 分钟前
深入理解二叉树:从数据结构到算法实战
数据结构·算法
xuhaoyu_cpp_java22 分钟前
Boyer-Moore 投票算法
java·经验分享·笔记·学习·算法
武藤一雄31 分钟前
WPF中ViewModel之间的5种通讯方式
开发语言·前端·microsoft·c#·wpf
XiYang-DING32 分钟前
【Java】堆
java·开发语言
Lyyaoo.37 分钟前
【Java基础面经】Java 反射机制
java·开发语言·python
kobesdu1 小时前
FAST-LIO2 + 蓝海M300激光雷达:从建图到实时栅格图的完整流程
算法·机器人·ros·slam·fast lio
x_xbx1 小时前
LeetCode:438. 找到字符串中所有字母异位词
算法·leetcode·职场和发展
MThinker1 小时前
K230+canMV+micropython实现低成本MLX90640红外热成像测温模块(续)
算法·智能硬件·micropython·canmv·k230
m0_694845571 小时前
UVdesk部署教程:企业级帮助台系统实践
服务器·开发语言·后端·golang·github
泉飒1 小时前
C2001: 常量中有换行符-QT解决办法-逆向思路
开发语言·qt