C++类的封装之 字符串MyString类

包括了运算符重载<< >> = + == [ ]
mystring.h

#ifndef MYSTRING_H
#define MYSTRING_H
#include<iostream>
using namespace std;


class MyString
{
    friend ostream& operator<<(ostream& out,MyString& ob);
    friend istream& operator>>(istream& in,MyString& ob);
private:
    char *str;
    int size;
public:
    MyString();
    MyString(const char *str);
    MyString(const MyString& ob);
    ~MyString();
    int Size(void);
    char& operator[](int index);
    MyString& operator=(const MyString &ob);
    MyString& operator=(const char *str);
    MyString& operator+(const MyString &ob);
    MyString& operator+(const char *str);
    bool operator==(const MyString &ob);
    bool operator==(const char *str);
};

#endif // MYSTRING_H

mystring.cpp

#include "mystring.h"
#include <cstring>
#include <iostream>
using namespace std;
MyString::MyString()
{
    this->str=NULL;
    cout<<"无参构造"<<endl;
}

MyString::MyString(const char *str)
{
    cout<<"char *有参构造函数"<<endl;
    this->str=new char[strlen(str)+1];
    strcpy(this->str,str);
    this->size=strlen(str);
}

MyString::MyString(const MyString &ob)
{
    cout<<"拷贝构造函数"<<endl;
    this->str=new char[strlen(ob.str)+1];
    strcpy(this->str,ob.str);
    this->size=ob.size;
}

MyString::~MyString()
{
    cout<<"析构函数"<<endl;
    if(this->str!=NULL)
    {
        delete[] this->str;
        this->str=NULL;
    }
}

int MyString::Size()
{
    return this->size;
}

char& MyString::operator[](int index)
{
    if(index>=0&&index<this->size)
    {
        return this->str[index];
    }
    else
        cout<<"index无效"<<endl;
}

MyString &MyString::operator=(const MyString &ob)
{
    if(this->str!=NULL)
    {
        delete[] this->str;
        this->str=NULL;
    }
    this->str=new char[ob.size+1];
    strcpy(this->str,ob.str);
    this->size=ob.size;
    return *this;
}

MyString &MyString::operator=(const char *str)
{
    if(this->str!=NULL)
    {
        delete[] this->str;
        this->str=NULL;
    }
    this->str=new char[strlen(str)+1];
    strcpy(this->str,str);
    this->size=strlen(str);
    return *this;
}

MyString& MyString::operator+(const MyString &ob)
{
    int newsize=this->size+ob.size+1;//两个字符串的总长度
    char *tmp_str=new char[newsize];
    memset(tmp_str,0,newsize);
    strcpy(tmp_str,this->str);
    strcat(tmp_str,ob.str);
    static MyString newstring(tmp_str);
    if(tmp_str!=NULL)
    {
        delete[] tmp_str;
        tmp_str=NULL;
    }
    return newstring;
}

MyString &MyString::operator+(const char *str)
{
    int newsize=this->size+strlen(str)+1;//两个字符串的总长度
    char *tmp_str=new char[newsize];
    memset(tmp_str,0,newsize);
    strcpy(tmp_str,this->str);
    strcat(tmp_str,str);
    static MyString newstring(tmp_str);
    if(tmp_str!=NULL)
    {
        delete[] tmp_str;
        tmp_str=NULL;
    }
    return newstring;
}

bool MyString::operator==(const MyString &ob)
{
    if((strcmp(this->str,ob.str)==0)&&(this->size==ob.size))
    {
        return true;
    }
    return false;
}

bool MyString::operator==(const char *str)
{
    if((strcmp(this->str,str)==0)&&(this->size==strlen(str)))
    {
        return true;
    }
    return false;
}
ostream& operator<<(ostream& out,MyString& ob)
{
    out<<ob.str;
    return out;
}
istream& operator>>(istream& in,MyString& ob)
{
    if(ob.str!=NULL)
    {
        delete[] ob.str;
        ob.str=NULL;
    }
    char buf[1024]="";
    cout<<"输入一个字符串: ";
    in>>buf;
    ob.str=new char[strlen(buf)+1];
    strcpy(ob.str,buf);
    ob.size=strlen(buf);
    return in;
}

main.cpp

#include <iostream>
#include"mystring.h"
using namespace std;

int main(int argc, char *argv[])
{
    MyString str1("hahaha");
    cout<<str1<<endl;
    cout<<"size="<<str1.Size()<<endl;

    cin>>str1;
    cout<<str1<<endl;
    cout<<"size="<<str1.Size()<<endl;

    MyString str2("hello class");
    cout<<str2<<endl;
    cout<<str2[1]<<endl;
    str2[0]='H';
    cout<<str2<<endl;
    cout<<"size="<<str2.Size()<<endl;

    MyString str3("hello str3");
    cout <<str3<<endl;
    str3=str2;
    cout <<str3<<endl;

    MyString str4("hello str4");
    cout <<"str4="<<str4<<endl;
    str4="hello string";
    cout <<"str4="<<str4<<endl;

    MyString str5("我爱");
    MyString str6("物联网");
    cout<<(str5+str6)<<endl;;

    MyString str7("我爱学习,");
    cout<<str7+"学习爱我"<<endl;

    MyString str8("hehe");
    MyString str9("haha");
    if(str8 == str9)
    {
        cout<<"相等"<<endl;
    }
    else
    {
        cout<<"不相等"<<endl;
    }

    if(str8 == "hehe")
    {
        cout<<"相等"<<endl;
        }
    else
    {
        cout<<"不相等"<<endl;
    }
    return 0;
}

发布了24 篇原创文章 · 获赞 13 · 访问量 3096

猜你喜欢

转载自blog.csdn.net/Evan_work/article/details/105084591