强化练习-string类的实现

本文详细介绍了使用C++实现自定义字符串类的过程,包括构造函数、赋值操作符、比较运算符、拼接运算符等核心成员函数的设计与实现细节。通过深入探讨内存管理、深拷贝与浅拷贝的区别,以及如何正确处理字符串的复制和销毁,为读者提供了全面的C++字符串类编程指导。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

MyString.h

#pragma once
#include <iostream>
using namespace std;


class Mystring
{
public:
	Mystring();
	Mystring(const char* str);
	Mystring(const Mystring& another);
	~Mystring();


	char& operator[](int index);
	friend ostream& operator<<(ostream& os, const Mystring& str);
	friend istream& operator>>(istream& is,  Mystring& str);
	Mystring& operator=(const char* str);
	Mystring& operator=(const Mystring& str);
	bool operator==(const Mystring& another);
	Mystring operator+(const Mystring& another);


private:
	int m_length;
	char* m_ptr;
};


MyString.cpp

#include "stdafx.h"
#include "MyString.h"

Mystring::Mystring()
{
	m_length = 0;
	m_ptr = NULL;
}


Mystring::Mystring(const char* str)
{
	int len = strlen(str);
	if (len > 0)
	{
		m_length = len;
		m_ptr = new char[len+1];
		m_ptr[len] = 0;
		memcpy(m_ptr, str, len);
	}

}


Mystring::Mystring(const Mystring& another)
{
	int len = another.m_length;
	if (len > 0)
	{
		m_length = len;
		m_ptr = new char[len + 1];
		m_ptr[len] = 0;
		memcpy(m_ptr, another.m_ptr, len);
	}


}


Mystring::~Mystring()
{
	if (m_ptr != NULL)
	{
		delete m_ptr;
		m_ptr = NULL;
		m_length = 0;
	}
}


char& Mystring::operator[](int index)
{
	return m_ptr[index];
}


ostream& operator<<(ostream& os, const Mystring& str)
{
	os << str.m_ptr;

	return os;
}


istream& operator>>(istream& is,  Mystring& str)
{
	if (str.m_length == 0&& str.m_ptr==NULL)
	{
		char string_tmp[100] = { 0 };
		cin >> string_tmp;
		int len = strlen(string_tmp);
		str.m_length = len;
		str.m_ptr = new char[len + 1];
		str.m_ptr[len] = 0;
		memcpy(str.m_ptr, string_tmp, len);
	}
	return is;
}



Mystring& Mystring::operator=(const char* str)
{
	int len = strlen(str);
	if (len > 0)
	{
		delete[] m_ptr; //把以前释放
		m_length = len;
		m_ptr = new char[len + 1];
		m_ptr[len] = 0;
		memcpy(m_ptr, str, len);
		return *this;
	}
}


Mystring& Mystring::operator=(const Mystring& str)
{
	if (this == &str) return *this;
	int len = str.m_length;
	if (len > 0)
	{
		delete[] m_ptr; //把以前释放
		m_length = len;
		m_ptr = new char[len + 1];
		m_ptr[len] = 0;
		memcpy(m_ptr, str.m_ptr, len);
		return *this;
	}
}




bool Mystring::operator==(const Mystring& another)
{
	if (this == &another)return true;
	int len = another.m_length;
	if (m_length == len && len > 0)
	{
		for (int i = 0; i < len; i++)
		{
			if (m_ptr[i] != another.m_ptr[i])
				return false;

		}
		return true;
	}
	else
		return false;
}




Mystring Mystring::operator+(const Mystring& another)
{
	Mystring tmp;
	if (another.m_length == 0)
		return *this;
	else
	{
		int all_len = m_length + another.m_length;
		char* new_ptr = new char [all_len + 1];
		new_ptr[all_len] = 0;
		memcpy(new_ptr, m_ptr, m_length);
		memcpy(new_ptr + m_length, another.m_ptr, another.m_length);
		
		tmp.m_ptr = new_ptr;
		tmp.m_length = all_len;
		return tmp;
	}

}

user.cpp

#include "stdafx.h"
#include "MyString.h"

int main()
{

	Mystring str1("julian");

	Mystring str2;
	cin >> str2;
	cout << str1 << " " << str2 << endl;

	str2 = str1;
	cout << str2 << endl;
	str2 = "fuck";
	cout << str2 << endl;

	Mystring str3(str1);
	Mystring str4 = "mark";
	if (str3 == str4)
		cout << "same" << endl;
	else
		cout << "no same" << endl;

	
	if (str3 == str1)
		cout << "same" << endl;
	else
		cout << "no same" << endl;

	str4[0] = 'a';
	cout << str4 << endl;

	Mystring str5 = str1 + str4;
	
	str5[0] = 'w';

	cout << str5 << endl;
	cout << str1 << endl;
	cout << str5 + str1 + str2 << endl;



    return 0;
}

结果输入:kerr
输出:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值