c++ 自己写一个 String 类
Main.cpp :
#include "iostream"
#include "string.h"
using namespace std;
#include "MyString.h"
int main(void){
MyString s1;
MyString s2("s2");
MyString s3 = s2;
MyString s4 = "s4"; //MyString& operator=(const char *p);
s4 = "s44";
s4 = s2; //MyString& operator=(const MyString &s);
s4[1] = '9';//char& operator[](int index);
cout<<s4[1]<<endl;
cout<<s4<<endl;//ostream& operator<<(ostream &out, MyString &s);
cout << "hello..." << endl;
// ==
MyString s5("s5");
if(s5=="s5"){//bool operator==(const char *p);
printf("==\n");
}else{printf("!=\n");}
if(s5!="s5"){//bool operator!=(const char *p);
printf("==\n");
}else{printf("!=\n");}
MyString s6 = s2;
if(s5==s6){//bool operator==(const MyString& s);
printf("==\n");
}else{printf("!=\n");}
if(s5!=s6){//bool operator!=(const MyString& s);
printf("==\n");
}else{printf("!=\n");}
// <
MyString s7("s7");
MyString s8 = "aaa";
int tag = s8<"bbbb";
if(tag>0){//int operator<(const char *p);
printf("s8小于bbbb\n");
}else{
printf("s8大于bbbb\n");
}
tag = s8<s7;
if(tag>0){//int operator<(const MyString &s);
printf("s8小于s7\n");
}else{
printf("s8大于s7\n");
}
//把类私有的指针 露出来
MyString s9 = "s9999";
strcpy(s9.c_str(),"s999");//不能超过原来的长度,不然释放有问题
cout<<s9<<endl;
// >> istream& operator>>(istream &in, MyString &s)
MyString s10(128);
cout<<"\n请输入字符串(回车结束)"<<endl;
cin>>s10;//输入不能超过初始化长度,不然释放有问题
cout<<s10<<endl;
cout<<"=============="<<endl;
// char* a = "ccccc";
// char* b = "aaaaaa";
// cout <<strcmp(a,b)<<endl;
// getchar();
system("pause");
return 0;
}
MyString.h :
#include "iostream"
using namespace std;
class MyString{
friend ostream& operator<<(ostream &out, MyString &s);
friend istream& operator>>(istream &in, MyString &s);
public:
MyString(int len = 0);
MyString(const char *p);
MyString(const MyString& s);
~MyString();
public:
MyString& operator=(const char *p);
MyString& operator=(const MyString &s);
char& operator[](int index);
public:
bool operator==(const char *p) const;
bool operator==(const MyString& s) const;
bool operator!=(const char *p) const;
bool operator!=(const MyString& s) const;
public:
int operator<(const char *p);
int operator<(const MyString &s);
int operator>(const char *p);
int operator>(const MyString &s);
public://把类私有的指针 露出来
char *c_str(){
return m_p;
}
const char *c_str2(){
return m_p;
}
int length(){
return m_len;
}
private:
int m_len;
char *m_p;
};
MyString.cpp :
#include "iostream"
#include <string.h>
using namespace std;
#include "MyString.h"
ostream& operator<<(ostream &out, MyString &s){
out<<s.m_p;
return out;
}
istream& operator>>(istream &in, MyString &s){
in>>s.m_p;
return in;
}
MyString::MyString(int len){
if(len==0){
m_len = 0;
m_p = new char[m_len+1];
strcpy(m_p,"");
}else{
m_len = len;
m_p = new char[m_len+1];
memset(m_p,0,m_len);//init NULL;
}
}
MyString::MyString(const char *p){
if(p==NULL){//NULL
m_len = 0;
m_p = new char[m_len+1];
strcpy(m_p,"");
}else{
m_len = strlen(p);
m_p = new char[m_len+1];
strcpy(m_p,p);
}
}
//s3 = s2
MyString::MyString(const MyString& s){
m_len = s.m_len;
m_p = new char[m_len+1];
strcpy(m_p,s.m_p);
}
MyString::~MyString(){
if(m_p !=NULL){
delete [] m_p;
m_p = NULL;
m_len = 0;
}
}
//s4 = "s4"
MyString& MyString::operator=(const char *p){
if(m_p!=NULL){
delete [] m_p;
m_len = 0;
}
if(p==NULL){//NULL
m_len = 0;
m_p = new char[m_len+1];
strcpy(m_p,"");
}else{
m_len = strlen(p);
m_p = new char[m_len+1];
strcpy(m_p,p);
}
return *this;
}
MyString& MyString::operator=(const MyString &s){
if(m_p!=NULL){
delete [] m_p;
m_len = 0;
}
m_len = s.m_len;
m_p = new char[m_len+1];
strcpy(m_p,s.m_p);
return *this;
}
char& MyString::operator[](int index){
return m_p[index];
}
bool MyString::operator==(const char *p) const{
if(p == NULL){
if(m_len==0){
return true;
}
return false;
}else{
if(m_len == strlen(p)){
return !strcmp(m_p,p);
}
return false;
}
}
bool MyString::operator==(const MyString& s) const{
if(m_len!=s.m_len){
return false;
}
return !strcmp(m_p,s.m_p);
}
bool MyString::operator!=(const char *p) const{
return !(*this == p);
}
bool MyString::operator!=(const MyString& s) const{
return !(*this==s);
}
int MyString::operator<(const char *p){
return strcmp(this->m_p,p);
}
int MyString::operator<(const MyString &s){
return strcmp(this->m_p,s.m_p);
}
int MyString::operator>(const char *p){
return strcmp(p,this->m_p);
}
int MyString::operator>(const MyString &s){
return strcmp(s.m_p,this->m_p);
}