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;
}
相关推荐
郝学胜-神的一滴24 分钟前
Linux 进程控制块(PCB)解析:深入理解进程管理机制
linux·服务器·开发语言
后端小张26 分钟前
【鸿蒙开发手册】重生之我要学习鸿蒙HarmonyOS开发
开发语言·学习·华为·架构·harmonyos·鸿蒙·鸿蒙系统
胖咕噜的稞达鸭28 分钟前
AVL树手撕,超详细图文详解
c语言·开发语言·数据结构·c++·算法·visual studio
CSCN新手听安30 分钟前
【linux】多线程(六)生产者消费者模型,queue模拟阻塞队列的生产消费模型
linux·运维·服务器·c++
-SGlow-38 分钟前
Linux相关概念和易错知识点(48)(epoll的底层原理、epoll的工作模式、反应堆模式)
linux·服务器·c语言·网络·c++
007php00744 分钟前
百度面试题解析:synchronized、volatile、JMM内存模型、JVM运行时区域及堆和方法区(三)
java·开发语言·jvm·缓存·面试·golang·php
csdn_aspnet1 小时前
C++ 圆台体积和表面积计算程序(Program for Volume and Surface area of Frustum of Cone)
c++
芒果量化1 小时前
Optuna - 自动调参利器&python实例
开发语言·python·算法·机器学习
foundbug9991 小时前
基于CSMA-CA协议的V2X通信MATLAB仿真
开发语言·网络·matlab
WangMing_X2 小时前
C#上位机软件:2.5 体验CLR实现多语言混合编程
java·开发语言·c#