c++&qt day3

头文件

cpp 复制代码
#ifndef MYSTRING_H
#define MYSTRING_H

#include <iostream>
#include <cstring>

using namespace std;


class myString
{
private:
    char* str;
    int size;
public:
    //无参构造
    myString();
    //有参构造
    myString(const char* s);
    //拷贝构造
    myString(const myString& other);
    //析构函数
    ~myString();
    //拷贝赋值函数
    myString& operator=(const myString& other);
    //判空
    bool empty()const;
    //size函数
    int getSize()const;
    //c_str函数
    const char* c_str()const;
    //at函数
    char &at(int pos);
    //加号运算符重载
    myString operator+(const myString& other)const;
    //加等于运算符重载
    myString& operator+=(const myString& other);
    //关系运算符>重载
    bool operator>(const myString& other)const;
    //中括号运算符重载
    char& operator[](int pos);
};

#endif // MYSTRING_H

功能文件

cpp 复制代码
#include "head.h"

//无参构造
myString::myString() :size(10)
{
    str = new char[10];
    strcpy(str, "");
}

//有参构造
myString::myString(const char* s)
{
    size = strlen(s);
    str = new char[size + 1];
    strcpy(str, s);
}


//拷贝构造
myString::myString(const myString& other)
{
    size = other.size;
    str = new char[size + 1];
    strcpy(str, other.str);
}

//析构函数
myString::~myString()
{
    delete[] str;
}

//拷贝赋值
myString& myString::operator=(const myString& other)
{
    if (this != &other)
    {
        delete[] str;
        size = other.size;
        str = new char[size + 1];
        strcpy(str, other.str);
    }
     return *this;
}

//判空
bool myString::empty() const
{

    return size==0;
}

//size函数
int myString::getSize() const
{
    return size;
}

//c_str函数
const char* myString::c_str() const
{
    return str;
}

//at函数
char& myString::at(int pos)
{
    if (pos >= 0 && pos < size)
    {
        return str[pos];
    }
    else
    {
        cout << "所给位置不合法" << endl;
        return str[0];
    }
}

//加号运算符重载
myString myString::operator+(const myString& other) const
{
    myString temp;
    temp.size = this->size + other.size;
    temp.str = new char[temp.size + 1];
    strcpy(temp.str, this->str);
    strcat(temp.str, other.str);
    return temp;
}

//加等于运算符重载
myString& myString::operator+=(const myString& other)
{
    char* temp = new char[size + other.size + 1];
    strcpy(temp, str);
    strcat(temp, str);
    delete[] str;
    str = temp;
    size += other.size;
}

//关系运算符>重载
bool myString::operator>(const myString& other) const
{
    return strcmp(str, other.str)>0;
}

//中括号运算符重载
char& myString::operator[](int pos)
{
    return at(pos);
}

测试文件

cpp 复制代码
#include "head.h"
int main()
{
    myString s1("Hello");
    myString s2("World");

    // 使用各种函数和运算符
    myString s3 = s1 + " " + s2;
    cout << s3.c_str() << endl;

    s1 += " C++";
    cout << s1.c_str() << endl;

    if (s1 > s2)
    {
       cout << s1.c_str() << " is greater than " << s2.c_str() << endl;
    return 0;
    }
}
相关推荐
闻缺陷则喜何志丹12 分钟前
【贪心 字典序 回文 最长公共前缀】LeetCode3734. 大于目标字符串的最小字典序回文排列|分数未知
c++·算法·力扣·贪心·字典序·回文·最长公共前缀
tung tung tung sahur20 分钟前
领略 Rust 抽象之美:自定义迭代器实现全解析
开发语言·后端·rust
ftpeak26 分钟前
《Rust MP4视频技术开发》第八章:生成MP4
开发语言·rust·音视频·mp4
好学且牛逼的马1 小时前
【SSM框架 | day25 spring IOC 与 DI 注解开发】
java·开发语言
_OP_CHEN1 小时前
C++进阶:(四)set系列容器的全面指南
开发语言·c++·stl·set·multiset·关联式容器·setoj题
不惑_1 小时前
Java 使用 FileOutputStream 写 Excel 文件不落盘?
开发语言·python
十五年专注C++开发1 小时前
Qt-VLC: 一个集成VLC的开源跨平台媒体播放库
开发语言·qt·媒体·libvlc·vlc-qt
郝学胜-神的一滴2 小时前
128天写作之旅:记录与成长的点滴
开发语言·程序人生
superman超哥2 小时前
仓颉语言中流式I/O的设计模式深度剖析
开发语言·后端·设计模式·仓颉
豆浆whisky2 小时前
Go内存管理最佳实践:提升性能的Do‘s与Don‘ts|Go语言进阶(17)
开发语言·后端·golang