字符串類的封裝
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(const char * str);
MyString(const MyString & str);
~MyString();
//重載=運算符
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; // 字符串大小
};
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)
{
//cout << "有參構造調用" << endl;
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()
{
//cout << "析構函數調用" << endl;
if (this->pString != NULL)
{
delete[] this->pString;
this->pString = NULL;
}
}
char& MyString::operator[](int index)
{
return this->pString[index];
}
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);
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);
return *this;
}
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;
}
return false;
}
bool MyString::operator==(const MyString & str)
{
if (strcmp(this->pString, str.pString) == 0 && this->m_Size == strlen(str.pString))
{
return true;
}
return false;
}
測試
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include "MyString.h"
using namespace std;
//測試 MyString
void test01()
{
MyString str = "abc";
cout << str << endl;
/*cout << "請輸入str新的內容:" << endl;
cin >> str;
cout << "新內容爲:" << str << endl;*/
MyString str2(str);
MyString str3 = "aaaaaa";
str3 = str2;
str3 = "aaaa";
cout << "str3 = " << str3 << endl;
str3[0] = 'w';
cout << "str3 第一個位置爲 = " << str3[0] << endl;
MyString str4 = "";
str4 = str2 + str3; //字符串拼接
str4 = str2 + "kkk"; //字符串拼接
cout << "str4 爲 " << str4 << endl;
if (str3 == str4)
{
cout << "str3 與 str4相等" << endl;
}
else
{
cout << "str3 與 str4不相等" << endl;
}
/*int a = 10;
cin >> a;
cout << "a = " << a << endl;*/
}
int main(){
test01();
system("pause");
return EXIT_SUCCESS;
}
運行
abc
str3 = aaaa
str3 第一個位置爲 = w
str4 爲 abckkk
str3 與 str4不相等