mystring.h
#define _CRT_SECURE_NO_WARNINGS
#pragma once
#include <iostream>
using namespace std;
class mystring
{
friend ostream& operator<<(ostream& cout, mystring &str);
friend istream& operator>>(istream& cin, mystring &str);
public:
mystring(const mystring & str);
mystring(const char *str);
~mystring();
//str=str1=str2 因为这种连环赋值所以要返回本身用引用
mystring& operator=(const char *str);
mystring& 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;//字符串大小 不算\0;
};
mystring.cpp
#include "mystring.h"
ostream& operator<<(ostream& cout, mystring &str)
{
cout << str.pstring;
return cout;
}
istream& operator>>(istream& cin, mystring &str)
{
//先判断 原始是否有内容 如果有清空
if (str.pstring != NULL)
{
delete[] str.pstring;
str.pstring = NULL;
}
//让用户输入内容
char buf[1024];
cin >> buf;
//把用户输入的字符赋值给 str;
str.pstring = new char[strlen(buf) + 1];
strcpy(str.pstring, buf);
str.m_size = strlen(buf);
return cin;
}
mystring::mystring(const char *str)
{
this->pstring = new char[strlen(str) + 1];
strcpy(this->pstring, str);
this->m_size = strlen(str);
}
mystring::mystring(const mystring & str)
{
this->pstring = new char[strlen(str.pstring) + 1];
//this->pstring = new char[str.m_size + 1];
strcpy(this->pstring, str.pstring);
this->m_size = str.m_size;
}
mystring::~mystring()
{
if (this->pstring != NULL)
{
delete[] this->pstring;
this->pstring = NULL;
}
}
mystring& 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);
return *this;
}
mystring& mystring::operator=(const mystring &str)
{
if (this->pstring != NULL)
{
delete[] this->pstring;
this->pstring = NULL;
}
this->pstring = new char[strlen(str.pstring) + 1];
strcpy(this->pstring, str.pstring);
this->m_size = strlen(str.pstring);
return *this;
}
char& mystring::operator[](int index)
{
return this->pstring[index];
}
mystring mystring::operator+(const char *str)
{
int newsize = this->m_size + strlen(str) + 1;
char *tmp = new char[newsize];
memset(tmp, 0, newsize);
strcat(tmp, this->pstring);
strcat(tmp, str);
mystring newstr(tmp);
delete[] tmp;
return newstr;
}
mystring mystring::operator+(const mystring &str)
{
int newsize = this->m_size + strlen(str.pstring) + 1;
char *tmp = new char[newsize];
memset(tmp, 0, newsize);
strcat(tmp, this->pstring);
strcat(tmp, str.pstring);
mystring newstr(tmp);
delete[] tmp;
return newstr;
}
bool mystring::operator==(const char * str)
{
if (strcmp(this->pstring, str) == 0 && this->m_size == strlen(str))
{
return true;
}
else
{
return false;
}
}
bool mystring::operator==(const mystring &str)
{
if (strcmp(this->pstring, str.pstring) == 0 && this->m_size == strlen(str.pstring))
{
return true;
}
else
{
return false;
}
}
test.cpp
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
#include "mystring.h"
void test()
{
mystring str = "abc";
cout << str << endl;
cout << "请输入str新内容" << endl;
cin >> str;
cout << "新内容:" << str << endl;
cout << str << endl;
mystring str3 = "aaa";
cout << str3 << endl;
str3 = str = "aaaa";
cout << str3 << endl;
cout << str << endl;
cout << str3[1] << endl;
mystring str4 = "a";
str4 =str3+str;
cout << "str4=" << str4 << endl;
if (str3 == str)
{
cout << "==" << endl;
}
else
{
cout << "!=" << endl;
}
}
int main()
{
test();
system("pause");
return EXIT_SUCCESS;
}