C++ 自定义字符串类型
myString.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 & cin, MyString & str);
public:
MyString(char * str);
MyString(const MyString & str);
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);
~MyString();
private:
char * pString;
int m_Size;
};
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)
{
delete[] str.pString;
str.pString = NULL;
}
char buf[1024];
cin >> buf;
str.pString = new char[strlen(buf) + 1];
strcpy(str.pString, buf);
str.m_Size = strlen(buf);
return cin;
}
MyString::MyString(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];
strcpy(this->pString, str.pString);
this->m_Size = str.m_Size;
}
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 * temp = new char[newSize];
memset(temp, 0, newSize);
strcat(temp, this->pString);
strcat(temp, str);
MyString newString = temp;
delete[] temp;
return newString;
}
MyString MyString::operator+(const MyString&str)
{
int newSize = this->m_Size + strlen(str.pString) + 1;
char * temp = new char[newSize];
memset(temp, 0, newSize);
strcat(temp, this->pString);
strcat(temp, str.pString);
MyString newString = temp;
delete[] temp;
return newString;
}
bool MyString::operator==(const char *str)
{
if ( strcmp( this->pString , str) == 0 )
{
return true;
}
return false;
}
bool MyString::operator==(const MyString &str)
{
if (strcmp(this->pString, str.pString) == 0)
{
return true;
}
return false;
}
MyString::~MyString()
{
if (this->pString != NULL)
{
delete[] this->pString;
this->pString = NULL;
}
}
测试
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
#include "myString.h"
void test01()
{
MyString str = "abc";
cout << str << endl;
cout << "请重新给str赋值:" << endl;
cin >> str;
cout << "str 新的值为: " << str << endl;
MyString str2 = str;
cout << "str2 = " << str2 << endl;
}
void test02()
{
MyString str = "abcd";
MyString str2 = "aaa";
str2 = str;
cout << "str2 = " << str2 << endl;
cout << "str2[0] = " << str2[0] << endl;
str2[0] = 'z';
cout << "str2[0]改为z后输出: " << str2 << endl;
MyString str3 = "abc";
MyString str4 = "def";
MyString str5 = str3 + str4;
MyString str6 = str5 + "ghe";
cout << "str5 = " << str5 << endl;
cout << "str6 = " << str6 << endl;
if (str5 == str6)
{
cout << "str5 == str6" << endl;
}
else
{
cout << "str5 != str6" << endl;
}
if ( str6 == "abcdefghe")
{
cout << "str6 = abcdefghe" << endl;
}
else
{
cout << "str6 != abcdefghe" << endl;
}
}
int main(){
test02();
system("pause");
return EXIT_SUCCESS;
}
|