首页 > 其他分享 >41. 强化训练-字符串类封装

41. 强化训练-字符串类封装

时间:2024-09-02 15:26:54浏览次数:6  
标签:封装 强化训练 41 char pString str operator MyString const


  • 强化训练-字符串类封装
  • 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;
}


标签:封装,强化训练,41,char,pString,str,operator,MyString,const
From: https://blog.51cto.com/zaishu/11898406

相关文章

  • 65. 类模板应用 – 数组类封装
    类模板应用–数组类封装将类写到myArray.hpp中属性:T*pAddress;指向堆区数组指针intm_Capacity数组容量intm_Size;数组大小行为myArray(intcapacity)myArray(constMyArray&arr)operator=operator[]~myArray()getCapacitygetSizepushbackmyArray.hpp#p......
  • 周赛413场 个人总结
    第1题 代码 """根据a的ascii码值是97奇数黑色的规律是:a1是97+1=偶数b2是98+2=偶数c1是99+1=偶数d2是100+2=偶数...所以,偶数为黑色===白色的规律a2=97+2=奇数b1=98+1=奇数....所以,奇数为白色"""classSolution:defche......
  • sicp每日一题[1.41]
    Exercise1.41Defineaproceduredoublethattakesaprocedureofoneargumentasargumentandreturnsaprocedurethatappliestheoriginalproceduretwice.Forexample,ifincisaprocedurethatadds1toitsargument,then(doubleinc)shouldbeapr......
  • 041.CI4框架CodeIgniter,控制器过滤器Filter的使用
    01、我们在Filters目录,创建一个MyFilter.php文件<?phpnamespaceApp\Filters;useCodeIgniter\Filters\FilterInterface;useCodeIgniter\HTTP\RequestInterface;useCodeIgniter\HTTP\ResponseInterface;classMyFilterimplementsFilterInterface{publicfu......
  • 416. 分割等和子集(leetcode)
    https://leetcode.cn/problems/partition-equal-subset-sum/description/01背包问题,需要考虑到如何把这个问题转化成01背包问题转换成01背包问题后,如何定义f[i]状态来表示这里有两种方式:1.按照传统01背包表示,即前i个物品中选,体积小于等于j的最大价值,这里体积和价值是等价......
  • Go基于crypto库实现AES封装加密以及协同PHP8 使用openssl AES加密使用
    前言要实现Go与PHP8之间的AES加密协同工作,我们需要确保两端使用相同的加密模式、密钥长度、以及密钥和初始化向量(IV)。下面,我将提供一个详细的教程,说明如何在Go中使用crypto/aes和crypto/cipher库来实现AES加密,并在PHP8中使用OpenSSL来解密这些数据(反之亦然)。Go基于基础......
  • springboot微信点餐系统(11041)
     有需要的同学,源代码和配套文档领取,加文章最下方的名片哦一、项目演示项目演示视频二、资料介绍完整源代码(前后端源代码+SQL脚本)配套文档(LW+PPT+开题报告)远程调试控屏包运行三、技术介绍Java语言SSM框架SpringBoot框架Vue框架JSP页面Mysql数据库IDEA/Eclipse开发......
  • 关于at32f415 free rtos下使用flash储存数据flash db库在写入数据库操作时,写入次数大
    由于f415的扇区每2k是一块扇区,所以在此处.blk_size=n*1024, //Flash块/扇区大小(因为STM32F2各块大小不均匀,所以擦除粒度为最大块的大小:128K)这个代码中,需要m==2,同理,需要查看你的单片机每个扇区的大小是多少,如果一个扇区的大小是4k,则此处需要填写的是由于f415的扇区每2......
  • STL 改造红黑树 模拟封装set和map
    改造红黑树目录改造红黑树适配STL迭代器的红黑树基本结构RBTreeNode__RBTree_iteratorRBTree完整代码封装的set封装的map在初次看STL中实现红黑树的源码时有些不理解,然后自己尝试对set以RBTree<K,K>的方式封装红黑树的迭代器;实现过程发现,这样封装复用程度特别低,也特别冗余,......
  • Markdown学习20221418曾庆林
    一、我掌握的内容1.Markdown及其基本的语法(标题,有序列表,代码)2.线下工具vscode二、我没有掌握的内容1.markdown详细语法(斜体,无序列表,链接,引用,分割线,表格)2.线上工具3.插入公式,绘图,格式转换4.ChatGPT等AIGC的提示词工程中的应用三、实践斜体*列表百度![图片]()终......