目录
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=(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; //字符串长度 不统计\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.pString = new char[strlen(buf) + 1];
strcpy(str.pString, buf);
str.m_size = strlen(buf);
return cin;
}
//有参构造
MyString::MyString(char* str)
{
//cout << "MyString有参构造函数调用" << endl;
this->pString = new char[strlen(str) + 1];
strcpy(this->pString , str);
this->m_size = strlen(str);
}
//拷贝构造
MyString::MyString(const MyString& str)
{
//cout << "MyString拷贝构造函数调用" << endl;
this->pString = new char[strlen(str.pString) + 1];
strcpy(this->pString, str.pString);
this->m_size = str.m_size;
}
//重载 = 运算符
MyString& MyString::operator=(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 = str.m_size;
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)
{
//cout << "析构调用" << endl;
delete [] this->pString;
this->pString = NULL;
}
}
main.cpp
#include<iostream>
#define _CRT_SECURE_NO_WARNINGS
using namespace std;
#include "myString.h"
int main()
{
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;
}
system("pause");
return EXIT_SUCCESS;
}