5.11home

header.h

#ifndef HEADER_H
#define HEADER_H

#include <iostream>
#include <cstring>

using namespace std;


class myString
{

public:
    //无参构造
    myString();
    //有参构造
    myString(const char *s);
    //拷贝构造
    myString(const myString & other);
    //析构函数
    ~myString();
    //判空函数
    bool isEmpty();
    //size函数
    int mySize();
    //c_str函数
    void c_str();
    //at函数
    char &at(int pos);
    //拷贝赋值
    myString operator=(const myString &other);
    // << 重载
    friend ostream &operator<<(ostream &l ,const myString &r);
    // >> 重载
    friend istream &operator>>(istream &l , myString &r);
    // + 重载
    myString operator+(const myString &other);
    // == 重载
    bool operator==(const myString &other);
    // != 重载
    bool operator!=(const myString &other);
    // < 重载
    bool operator<(const myString &other);
    // > 重载
    bool operator>(const myString &other);
    // <= 重载
    bool operator<=(const myString &other);
    // >= 重载
    bool operator>=(const myString &other);
    // [] 重载
    char &operator[](int pos);

private:
    char *str; //字符串
    int size;  //字符串长度
};



#endif // HEADER_H

test.cpp

#include "header.h"
#include <cstring>

//无参构造
myString::myString() : size(10)
{
    str = new char[size];
    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);
}

//判空
bool myString::isEmpty()
{
    return (size == 0) ? true : false;
}

//获取长度
int myString::mySize()
{
    return size;
}

//c_str
void myString::c_str()
{
    cout<< str << endl;
}

//at
char &myString::at(int pos)
{
    char &ch = str[pos];
    return ch;
}

//拷贝赋值
myString  myString::operator=(const myString &other)
{
    this->size = other.size;
    strcpy(this->str,other.str);
    return *this;
}

// << 重载
ostream &operator<<(ostream &l ,const myString &r)
{
    l<<r.str;
    return  l;
}

//  >> 重载
istream &operator>>(istream &l , myString &r)
{
    l>>r.str;
    return  l;
}

// + 重载
myString myString::operator+(const myString &other)
{
    strcat(this->str,other.str);
    return *this;
}

// == 重载
bool myString::operator==(const myString &other)
{
    if(strcmp(this->str,other.str) == 0)
    {
        return 1;
    }
    return  0;
}

// != 重载
bool myString::operator!=(const myString &other)
{
    if(strcmp(this->str,other.str) != 0)
    {
        return 1;
    }
    return  0;
}


//  < 运算符重载
bool myString::operator<(const myString &other)
{
    if(strcmp(this->str,other.str) < 0)
    {
        return 1;
    }
    return  0;
}
 
//  > 运算符重载
bool myString::operator>(const myString &other)
{
    if(strcmp(this->str,other.str) > 0)
    {
        return 1;
    }
    return  0;
}

//  < 运算符重载
bool myString::operator<=(const myString &other)
{
    if(strcmp(this->str,other.str) <= 0)
    {
        return 1;
    }
    return  0;
}
 
//  > 运算符重载
bool myString::operator>=(const myString &other)
{
    if(strcmp(this->str,other.str) >= 0)
    {
        return 1;
    }
    return  0;
}

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

main.cpp

#include<header.h>

using namespace std;

int main()
{
    //无参
    myString s1;
    //判空
    s1.isEmpty();
    //有参
    myString s2("aaaAAA");
    //判空
    s2.isEmpty();
    //c_str
    s2.c_str();
    //at
    cout << "s2.at(1): " << s2.at(1) << endl;

    myString s3("mmmMMMM");
    //at
    cout << "s3.at(3): " << s3.at(3) << endl;
    //拷贝构造
    myString s4 = s3;
    //c_str
    s4.c_str();
    //at
    cout << "s4.at(1): " << s4.at(3) << endl;
    // ==
    cout << "(s1 == s2): " << (s1 == s2) << endl;
    // +
    myString s5 = s3 + s1;
    cout << "s5: " << endl;
    s5.c_str();
    // >
    cout << "(s4 > s5): " << (s4 > s5) << endl;
    // <=
    cout << "(s3 <= s4): " << (s3 <= s4) << endl;
    //[]
    cout << "s3[2]: " << s3[2] << endl;

    return 0;
}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值