C++day4

仿照string类,完成myString 类

代码:

头文件

#ifndef TEST_H
#define TEST_H
#include <iostream>
#include <cstring>
using namespace std;
class myString
{
public:
    myString();    //无参构造
    myString(const char *str); //有参构造
    ~myString();   //析构
    myString(const myString &other);//拷贝构造
    myString &operator=(const myString &other);//拷贝赋值
    bool empty();   //判空
    int size_();    //size函数
    const char *c_str_();//c_str函数
    char &at_(int pos);//at函数
    const myString operator+(const myString &R)const;//加号重载
    myString &operator+=(const myString &R);//加等号重载
    char & operator[](int i);//中括号重载
    bool operator>(const myString &other)const;//关系重载
    bool operator<(const myString &other)const;//关系重载
    bool operator==(const myString &other)const;//关系重载
    void show();//输出
private:
    char *str;      //存储字符串
    int size;       //存储字符串长度
};
#endif // TEST_H

源代码

#include "test.h"
myString::myString():size(30)//无参构造
{
    str = new char[size];         //构造出一个长度为30的字符串
    strcpy(str,"");         //赋值为空串
}
myString::myString(const char *s) //有参构造
{
    size = strlen(s);
    str = new char[size+1];
    strcpy(str,s);
}
myString::~myString()   //析构
{
    delete[]str;
}
myString::myString(const myString &other):str(new char[other.size+1]),size(other.size)//拷贝构造
{
    strcpy(str,other.str);
}
myString & myString::operator=(const myString &other)//拷贝赋值
{
    if(this != &other)
    {
        this->size = other.size;
        if(this->str!=NULL)     //判断原来的指向是否为空
        {
            delete []this->str;
        }
        this->str = new char[size+1]; //重新在堆区申请空间
        strcpy(this->str,other.str);  //拷贝字符串
    }
    return *this;
}
bool myString::empty()   //判空
{
    return strlen(str) == 0;
}
int myString::size_()    //size函数
{
    return size;
}
const char *myString::c_str_()//c_str函数
{
    return str;
}
char &myString::at_(int pos)//at函数
{
    return str[pos];
}
const myString  myString::operator+(const myString &R)const//加号重载
{
    myString temp;
    delete []temp.str;
    temp.str = new char[size+R.size];
    strcpy(temp.str,this->str);//将自身给到新的地址中
    strcat(temp.str,R.str);    //将后面要追加的追加到新的地址中
    temp.size = this->size + R.size;
    return temp;
}
myString &myString::operator+=(const myString &R)//加等号重载
{
    strcat(this->str,R.str);    //将后面要追加的追加到新的地址中
    this->size += R.size;
    return *this;
}
char &myString::operator[](int i)//中括号重载
{
   if(i >= 0 && i< size)
   {
       return this->str[i];
   }
   else
   {
       cout<<"数组越界"<<endl;
   }
}
void myString::show()//输出
{
    cout<<str<<endl;
}
bool myString::operator>(const myString &other)const//关系重载
{
    if(strcmp(str,other.str)>0)
    {
        return true;
    }
    return false;
}
bool myString::operator<(const myString &other)const//关系重载
{
    if(strcmp(str,other.str)<0)
    {
        return true;
    }
    return false;
}
bool myString::operator==(const myString &other)const//关系重载
{
    if(strcmp(str,other.str)==0)
    {
        return true;
    }
    return false;
}

测试函数

#include "test.h"
int main()
{
    char arr[10]="hello";
    myString s1(arr);  //有参构造

    myString s2=s1;   //拷贝构造

    myString s3;   //无参构造
    s3=s2;    //拷贝赋值函数

    cout << "s1:" << s1.size_() << endl;
    cout << "s2:" << s2.size_() << endl;    //size
    cout << "s3:" << s3.size_() << endl;

    cout << "改变前";
    s1.show();
    s1.at_(1) = 'i';      //at()函数
    cout << "改变后";
    s1.show();

    myString s4 = s1 + "world"; //加法运算符重载
    s4.show();

    myString s5("pig");   //有参构造
    s4 += s5;      //+=运算符重载
    s4.show();
    s4.size_();

    if(s5>s1)
    {
        cout<<"s5 > s1"<<endl;
    }else
    {
        cout<<"s5 <= s1"<<endl;
    }

    cout << "s4[6] = " << s4[6] << endl;
    return 0;
}

运行结果

思维导图 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值