注意:分文件编写
MyStrin.h
#pragma once
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
class MyString
{
friend ostream& operator<<(ostream &cout,MyString& str);
friend istream& operator>>(istream &cout,MyString& str);
public:
MyString(const char * str);
MyString(const MyString &str);
~MyString();
//重载=运算符
void operator=(const char *str);
void operator=(const MyString& str);
//【】重载
char& operator[](int index);
//+重载
MyString operator+(const char * str);
MyString operator+(const MyString& str);
//==重载
bool operator==(const char * str);
bool operator==(const MyString&str);
private:
char * pString;
int m_Size;
};
ostream& operator<<(ostream &cout,MyString& str);
istream& operator>>(istream &cout,MyString& str);
MyString.cpp
#include "MyString.h"
//构造
MyString::MyString(const char * str)
{
cout<<"有参构造"<<endl;
this->pString=new char[strlen(str)+1];
strcpy(this->pString,str);
this->m_Size=strlen(str);
}
//拷贝构造
MyString::MyString(const MyString &str)
{
cout<<"拷贝构造调用"<<endl;
this->pString = new char[str.m_Size+1];
strcpy(this->pString,str.pString);
this->m_Size=str.m_Size;
}
//析构
MyString::~MyString()
{
cout<<"析构调用"<<endl;
if(this->pString!=NULL)
{
delete [] this->pString;
this->pString=NULL;
}
}
//重载=运算符
void MyString::operator=(const char *str)
{
if(this->pString!=NULL)
{
delete[] this->pString;
this->pString=NULL;
}
this->pString=new char[strlen(str)+1];
strcpy(this->pString,str);
this->m_Size=strlen(str);
}
void MyString::operator=(const MyString& str)
{
if(this->pString!=NULL)
{
delete[] this->pString;
this->pString=NULL;
}
this->pString=new char[str.m_Size+1];
strcpy(this->pString,str.pString);
this->m_Size=str.m_Size;
}
//重载【】运算符
char& MyString::operator[](int index)
{
return this->pString[index];
}
//重载+运算符
MyString MyString::operator+(const char * str)
{
char* tmp= new char[this->m_Size+strlen(str)+1];
memset(tmp,0,this->m_Size+strlen(str)+1);
strcat(tmp,this->pString);
strcat(tmp,str);
MyString str1(tmp);
delete [] tmp;
return str1;
}
MyString MyString::operator+(const MyString& str)
{
char* tmp= new char[this->m_Size+str.m_Size+1];
memset(tmp,0,this->m_Size+str.m_Size+1);
strcat(tmp,this->pString);//strcat链接的顺序 tmp->Pstring->str.Pstring.
strcat(tmp,str.pString);
MyString str1(tmp);//做成一个对象
delete [] tmp;
return str1;
}
//重载==运算符
bool MyString::operator==(const char * str)
{
//return this->pString==str;//错误,比较的是指针而不是字符串
return !strcmp(this->pString,str);//返回的是相减的值,为0说明相等,返回1
}
bool MyString::operator==(const MyString& str)
{
return !strcmp(this->pString,str.pString);
}
ostream& operator<<(ostream &cout,MyString& str)
{
cout<<str.pString;
return cout;
}
istream& operator>>(istream &cout,MyString& str)
{
//先释放str原有的空间
if(str.pString!=NULL)
{
delete [] str.pString;
str.pString=NULL;
}
//输入
char buff[64];
cin>>buff;
//写入str中
str.pString=new char[strlen(buff)+1];
strcpy(str.pString,buff);
str.m_Size=strlen(buff);
return cin;
}
main.c
#include<iostream>
#include"MyString.h"
void test01()
{
MyString str("hello");
cout <<str<<endl;
MyString str1("哈哈哈");
cout<<str1<<endl;
//cout<<"请输入新内容:";
//cin>>str1;
//cout<<"输入后的新内容为:"<<str1<<endl;
//str1="hello";
//cout<<str1<<endl;
//cout<<str[0]<<endl;
/* MyString str2=" ";
str2=str+str1;
cout<<str2<<endl*/;
str1=str;
cout <<str<<endl;
cout<<str1<<endl;
if(str1 == str)
{
cout<<"str1==str"<<endl;
}
else
{
cout<<"str1!=str"<<endl;
}
}
int main()
{
test01();
return 0;
}