- 强化训练-字符串类封装
- myString类 实现自定义的字符串类
- 属性
- char * pString; 维护 在堆区真实开辟的字符数组
- int m_Size; 字符串长度
- 行为
- 有参构造 MyString(char * str)
- 拷贝构造 MyString(const MyString & str);
- 析构 ~MyString();
- 重载<< 运算符
- 重载 >> 运算符
- 重载 = 赋值运算
- 重载 [] str[0] 按照索引位置设置获取字符
- 重载 + 字符串拼接
- 重载 == 对比字符串
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; //字符串长度 不统计\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)
{
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 << "拷贝构造函数调用" << endl;
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)
{
//本身 abc 传入 def
//计算开辟内存大小
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;
}
}
强化训练-字符串类封装.cpp
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
//#include <string>
#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(){
//test01();
test02();
//int a = 10;
//cin >> a;
//cout << "a = " << a << endl;
system("pause");
return EXIT_SUCCESS;
}