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;
    }
}
相关推荐
J心流1 小时前
Vscode中使用C++代码进行debug
c++·ide·vscode
猎嘤一号1 小时前
Windows11桌面解锁守护脚本
开发语言·python·opencv
chilavert3182 小时前
技术演进中的开发沉思-31 MFC系列:类层次结构
c++·windows
一只鱼^_2 小时前
牛客周赛 Round 99
java·数据结构·c++·算法·贪心算法·动态规划·近邻算法
蓝婷儿2 小时前
Python 数据建模与分析项目实战预备 Day 2 - 数据构建与字段解析(模拟简历结构化数据)
开发语言·python·机器学习
青衫客362 小时前
浅谈 Python 中的 yield——yield的返回值与send()的关系
开发语言·python
玩代码3 小时前
CompletableFuture 详解
java·开发语言·高并发·线程
hz_zhangrl3 小时前
CCF-GESP 等级考试 2025年6月认证C++三级真题解析
开发语言·c++·青少年编程·gesp·gesp2025年6月·c++三级
人生在勤,不索何获-白大侠4 小时前
day21——特殊文件:XML、Properties、以及日志框架
xml·java·开发语言
dongzhenmao6 小时前
P1484 种树,特殊情形下的 WQS 二分转化。
数据结构·c++·windows·线性代数·算法·数学建模·动态规划