学习笔记:C++字符串类的封装

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 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;
	}
}

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;
}

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;
	}
	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;
}
C++强化训练-字符串类的封装.cpp
    
#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 = "";

	str3 = str2;
	str3 = "aaaaa";
	cout << str3 << endl;
	str3[0] = 'w';
	cout << "str3第一个位置为 = " << str3[0] << endl;

	MyString str4 = " ";
	str4 =(str2 + str3); //字符串的拼接
	cout << str4 << endl;

	if (str3 == str4)
	{
		cout << "str3与 str4相等" << endl;
	}
	else
	{
		cout << "str3与 str4不相等" << endl;
	}

}
int main()
{
	test01();
	system("pause");
	return EXIT_SUCCESS;
}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序猿小张的日常笔记

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值