C++(day4)

思维导图

封装Mystring

cpp 复制代码
#include <iostream>
#include<cstring>

using namespace std;

class Mystring{
public:
    //无参构造函数
    Mystring():size(10){
        str=new char[size];
        strcpy(str,"");
        cout<<"无参构造函数"<<endl;
    }
    //有参构造函数
    Mystring(const char *s){
        size=strlen(s);
        str=new char[size+1];
        strcpy(str,s);
        cout<<"有参构造函数"<<endl;
    }
    //拷贝构造函数
    Mystring(const Mystring &other){
        this->size=other.size;
        this->str=new char[this->size];
        strcpy(this->str,other.str);
        cout<<"拷贝构造函数"<<endl;
    }
    //析构函数
    ~Mystring(){
        delete []str;
        cout<<"析构函数"<<endl;
    }
    //拷贝赋值函数
    Mystring &operator=(const Mystring &other){
        if(this!=&other){
            this->size=other.size;
            strcpy(this->str,other.str);
        }
        cout<<"拷贝赋值函数"<<endl;
        return *this;
    }
    //判空函数
    bool empty()const{
        return !strlen(this->str);
    }
    //size函数
    int strsize()const{
        return strlen(this->str);
    }
    //c_str函数
    char *c_str(){
        return this->str;
    }
    //at函数
    char &at(int pos){
        return *(this->str+pos-1);
    }
    //加号运算符重载
    Mystring operator+(const Mystring &R)const{
        Mystring temp;
        strcat(temp.str,this->str);
        strcat(temp.str,R.str);
        return temp;
    }
    //加等于运算符重载
    Mystring &operator+=(const Mystring &R){
        strcat(this->str,R.str);
        return *this;
    }
    //关系运算符重载(>)
    bool operator>(const Mystring &R)const{
        if(strcmp(this->str,R.str)>0){
            return true;
        }
        else{
            return false;
        }
    }
    //中括号运算符重载
    char &operator[](int pos)const{
        return *(this->str+pos-1);
    }
    //展示函数
    void show(){
        cout<<str<<endl;
    }
private:
    char *str;  //字符串首地址
    int size;   //字符串大小
};

int main()
{
    Mystring str1("hello");
    str1.show();
    Mystring str2("world");
    str2.show();
    Mystring str3;
    if(str3.empty()){
        cout<<"str3现在为空,字符串长度为"<<str3.strsize()<<endl;
    }
    str3=str1;
    str3.show();
    if(!str3.empty()){
        cout<<"str3现在不为空,字符串长度为"<<str3.strsize()<<endl;

    }
    Mystring str4=str2;
    str4.show();
    str4+=str3;
    str4.show();
    cout<<"str4字符串第7位是"<<str4.at(7)<<",str4字符串第13位是"<<str4[13]<<endl;
    cout<<str4.c_str()<<endl;
    if(str3>str2){
        cout<<"str3>str2"<<endl;
    }
    else{
        cout<<"str3<str2"<<endl;
    }

    return 0;
}
相关推荐
艾莉丝努力练剑41 分钟前
【LeetCode&数据结构】单链表的应用——反转链表问题、链表的中间节点问题详解
c语言·开发语言·数据结构·学习·算法·leetcode·链表
还债大湿兄2 小时前
《C++内存泄漏8大战场:Qt/MFC实战详解 + 面试高频陷阱破解》
c++·qt·mfc
倔强青铜35 小时前
苦练Python第18天:Python异常处理锦囊
开发语言·python
u_topian5 小时前
【个人笔记】Qt使用的一些易错问题
开发语言·笔记·qt
珊瑚里的鱼6 小时前
LeetCode 692题解 | 前K个高频单词
开发语言·c++·算法·leetcode·职场和发展·学习方法
AI+程序员在路上6 小时前
QTextCodec的功能及其在Qt5及Qt6中的演变
开发语言·c++·qt
xingshanchang6 小时前
Matlab的命令行窗口内容的记录-利用diary记录日志/保存命令窗口输出
开发语言·matlab
Risehuxyc6 小时前
C++卸载了会影响电脑正常使用吗?解析C++运行库的作用与卸载后果
开发语言·c++
AI视觉网奇6 小时前
git 访问 github
运维·开发语言·docker
不知道叫什么呀6 小时前
【C】vector和array的区别
java·c语言·开发语言·aigc