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;
    }
}
相关推荐
StudyWinter20 分钟前
【C++】仿函数和回调函数
开发语言·c++·回调函数·仿函数
C4程序员43 分钟前
北京JAVA基础面试30天打卡14
java·开发语言·面试
黑客影儿1 小时前
Go特有的安全漏洞及渗透测试利用方法(通俗易懂)
开发语言·后端·安全·web安全·网络安全·golang·系统安全
君鼎2 小时前
Effective C++ 条款55:熟悉Boost库
c++
你好,我叫C小白2 小时前
C语言 常量,数据类型
c语言·开发语言·数据类型·常量
小红帽2.02 小时前
从ioutil到os:Golang在线客服聊天系统文件读取的迁移实践
服务器·开发语言·golang
Zafir20243 小时前
Qt实现TabWidget通过addTab函数添加的页,页内控件自适应窗口大小
开发语言·c++·qt·ui
阿巴~阿巴~3 小时前
深入解析C++非类型模板参数
开发语言·c++
多吃蔬菜!!!3 小时前
vscode 搭建C/C++开发环境搭建(linux)
linux·c语言·c++
小指纹5 小时前
河南萌新联赛2025第(六)场:郑州大学
java·开发语言·数据结构·c++·算法