C++ 自定义字符串类型

C++ 自定义字符串类型

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"

//1. 重载左移运算符
ostream& operator<<(ostream & cout , MyString & str)
{
	cout << str.pString;
	return cout;
}

//2. 重载右移运算符
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;
}

//3. 重载=赋值运算符
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;
}

// 4. 重载 []  str[0]  按照索引位置设置获取字符
char& MyString::operator[](int index)
{
	return this->pString[index];
}

// 5. 重载 +  字符串拼接
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;
}

//6. 重载 == 对比字符串
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;
	}

}

测试

#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;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
课程内容100+集 C 语言编程实战课程,由 WangTeacher 主讲 。大神手把手教你编程,精心制作一部视频教学实战课程,细致到开发环境的搭建,都是手把手传授。并结合 15 年的编程经验,把最先进的软件开发流程和编程技巧融合到课程中,代码示例涵盖了 C 语言的每一个知识点,你值得拥有!第一章 基础技能篇:C 语言简介,基础语法和规则,数据类型,变量,运算符,流程控制,数组,字符串,函数,结构体,联合体,指针,文件操作,代码调试等知识点。第二章 实战技能篇:编程规范、内存管理、Makefile、进程内存布局、动态库、静态库、链表、排序算法、C 函数库介绍、递归函数、多进程调试等适用技能。第三章 案例分享篇:学员管理系统,从需求设计,架构设计,到模块设计,再到各个模块编码,学会从专业软件开发的角度去理解编程,他确实就是一门艺术。第四章 面试题篇:所有付出了时间和精力的学习,都是为了有回报,学习计算机技术更是如此,通过分析常见面试题,可以帮助你查漏补缺,更快、更容易实现回报。第五章 补充课程:在学员学习的过程中如果出现一个疑问,或者不够深入讲解的地方进行补充的课程。  课程特色案例:从专业程序员的角度思考问题和编写代码。实用:课程中融合了多年总结的编程技巧。快速:最快掌握编程的方法就是模仿和刻意练习。方式:脱离实践的传道都是扯蛋,能用代码演示的地方,绝不废话,每课都是编码,以最接近专业程序员日常的方式,手把手带你入行,和大神结对编程就是高效。 适合人群小学中学生,专科本科生,就业想转行生;逻辑思维差,空间想象差,编程可以改善;平时爱唱歌,却五音不全,写程序很合适;想尽快挣钱,迎娶白富美,达到人生巅峰。 

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值