c++day4

写出三种构造函数,算术运算符、关系运算符、逻辑运算符重载尝

cpp 复制代码
#include <iostream>
#include <cstring>
 
using namespace std;
 
class My_string
{
    private:
        char *data;
        int size;
 
    public:
        //无参构造默认长度为15
        My_string();
 
        //有参构造
        My_string(const char *str);
 
        My_string(int n, char ch);
 
        //析构函数
        ~My_string();
 
        //拷贝构造函数
        My_string(const My_string &other);
 
        //拷贝赋值函数
        My_string & operator=(const My_string &other);
 
        //c_str函数
        const char *c_str();
 
        //size函数
        int size_t();
 
        //empty函数
        bool empty();
 
        //at函数
        char at(int i);
 
        // + 运算符
        const My_string operator+(const My_string &R)const;
        //实现+=运算符重载
        const My_string operator+=(const My_string &R)const;
        // == 运算符
        bool operator==(const My_string &R)const;
        //实现[]运算符重载
        char &operator[](int n);
 
        friend ostream &operator<<(ostream &L, const My_string &R); //输入
        friend istream &operator>>(istream &L, const My_string &R); //输出
 
};
 
//无参构造默认长度为15
My_string::My_string():size(15)
{
    data = new char[size];
    data[0] = '\0';
}
 
//有参构造
My_string::My_string(const char *str)
{
    int s = strlen(str);
    data = new char[s+1];
    data[0] = '\0';
 
    strcpy(data, str);
}
 
My_string::My_string(int n, char ch)
{
    data = new char[n+1];
    data[0] = '\0';
 
    for(int i=0; i<n; i++) {
        this->data[i] = ch;
    }
    data[n+1] = '\0';
}
 
//析构函数
My_string::~My_string()
{
    delete []data;
    data = nullptr;
    cout<<"析构函数:this = "<<this<<endl;
}
 
//拷贝构造函数
My_string::My_string(const My_string &other)
{
    int s = strlen(other.data);
    data = new char[s+1];
    data[0] = '\0';
 
    strcpy(this->data, other.data);
}
 
//拷贝赋值函数
My_string & My_string::operator=(const My_string &other)
{
    int s = strlen(other.data);
    data = new char[s+1];
    data[0] = '\0';
 
    strcpy(this->data, other.data);
    return *this;
}
 
//c_str函数
const char* My_string::c_str()
{
    return data;
}
 
//size函数
int My_string::size_t()
{
    return strlen(data);
}
 
//empty函数
bool My_string::empty()
{
    return strlen(data)==0?1:0;
}
 
//at函数
char My_string::at(int i)
{
    return data[i];
}
 
//实现+运算符重载
const My_string My_string::operator+(const My_string &R)const
{
    My_string temp;
    strcat(temp.data, this->data);
    strcat(temp.data, R.data);
    return temp;
}
 
//实现+=运算符重载
const My_string My_string::operator+=(const My_string &R)const
{
    strcat(this->data, R.data);
    return this->data;
}
 
//实现[]运算符重载
char & My_string::operator[](int n)
{
    return data[n];
}
 
//实现==运算符重载
bool My_string::operator==(const My_string &R)const
{
    return this->data == R.data;
}
 
//实现插入运算符的重载:
ostream &operator<<(ostream &L, const My_string &R)
{
    L<<R.data;
    return L;
}
 
//实现提取运算符的重载:
istream &operator>>(istream &L, const My_string &R)
{
    L>>R.data;
    return L;
}
 
int main()
{
    My_string s1;   //无参构造
 
    My_string s2 = "hello world!";  //有参构造
    cout<<"s2 = "<<s2<<endl;
 
    My_string s3(5,'A');
    cout<<"s3 = "<<s3<<endl;
 
    My_string s4 = s2;  //拷贝构造
    cout<<"s4 = "<<s4<<endl;
 
    s1 = s2;            //拷贝赋值
    cout<<"s1 = "<<s1<<endl;
 
    printf("%s\n", s3.c_str());
    //cout<<s3.c_str()<<endl;
 
    cout<<"size_t = "<<s2.size_t()<<endl;
 
    My_string ss1;
    if(ss1.empty()) {
        cout<<"空字符串!"<<endl;
    }
    else {
        cout<<"非空字符串!"<<endl;
    }
 
    cout<<"s1.at(1) = "<<s1.at(1)<<endl;
    cout<<"s1[1] = "<<s1[1]<<endl;
 
    My_string ss2 = s2 + "  zhengqi";
    cout<<"ss2 = "<<ss2<<endl;
 
    My_string ss3;
    cout<<"请输入ss3字符串>>>";
    cin>>ss3;           //输入运算符
    cout<<"ss3 = "<<ss3<<endl;
 
    My_string ss4 = "aaa";
    My_string ss5 = "aaa";
    bool bo = ss4 == ss5;
    cout<<"bool = "<<bo<<endl;
 
    s1 += s3;
    cout<<s1<<endl;
 
    return 0;
}

试实现自增、自减运算符的重载

相关推荐
思成不止于此3 分钟前
C++ STL中map与set的底层实现原理深度解析
开发语言·c++·set·map·红黑树·底层实现
艾醒3 分钟前
大模型原理剖析——从技术特性、底层架构到落地逻辑的全维度解析
算法
惺忪97985 分钟前
C++ 构造函数完全指南
开发语言·c++
小此方6 分钟前
Re:从零开始学C++(五)类和对象·第二篇:构造函数与析构函数
开发语言·c++
秦苒&7 分钟前
【C语言】详解数据类型和变量(二):三种操作符(算数、赋值、单目)及printf
c语言·开发语言·c++·c#
无限进步_7 分钟前
【C语言&数据结构】有效的括号:栈数据结构的经典应用
c语言·开发语言·数据结构·c++·git·github·visual studio
Liangwei Lin14 分钟前
洛谷 B3637 最长上升子序列
算法
是喵斯特ya16 分钟前
python开发web暴力破解工具(进阶篇 包含验证码识别和token的处理)
开发语言·python·web安全
零K沁雪18 分钟前
multipart-parser-c 使用方式
c语言·开发语言
珂朵莉MM30 分钟前
第七届全球校园人工智能算法精英大赛-算法巅峰赛产业命题赛第一赛季优化题--无人机配送
人工智能·算法·无人机