MyString类的C++实现

#pragma once
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<string>
#include<string.h>
using namespace std;

class MyString
{
	friend istream& operator >> (istream& in, MyString& str);
	friend ostream& operator<<(ostream& os, MyString& str);

public:
	MyString(const char* s);
	MyString(const MyString& s);
	
	MyString& operator=(const MyString& str);
	MyString& operator=(const char* s);

	char& operator[](int index);
	
	MyString operator+(const MyString& str);
	MyString operator+(const char* s);

	const char* c_str();

	bool operator==(const MyString& str);
	bool operator==(const char* s);
	
	MyString& operator+=(const MyString& str);
	MyString& operator+=(const char* s);
	~MyString();

private:
	char* pAddr;
	int mSize;
};`在这里插入代码片

#include "MyString.h"

istream& operator >> (istream& in, MyString& str) 
{
	cout << ">> operator\n";
	char buf[256] = { 0 };
	in >> buf;
	if (str.pAddr){
		delete[] str.pAddr;
		str.pAddr = NULL;
	}

	str.pAddr = new char[strlen(buf) + 1];
	strcpy(str.pAddr, buf);
	str.mSize = strlen(buf);
	return in;
}
ostream& operator<<(ostream& os, MyString& str) 
{
	os << str.pAddr;
	return os;
} 

MyString::MyString(const char* s)
{
	cout << "char* construct" << endl;

	this->pAddr = new char[strlen(s) + 1];
	strcpy(this->pAddr, s);
	this->mSize = strlen(s);
}
MyString::MyString(const MyString& s)
{
	cout << "copy construct" << endl;

	//指针需要手动控制拷贝过程。默认拷贝只是值拷贝,浅拷贝
	this->pAddr = new char[strlen(s.pAddr) + 1];
	strcpy(this->pAddr, s.pAddr);
	this->mSize = s.mSize;
}

MyString& MyString::operator=(const MyString& str)
{
	cout << "string operator=" << endl;

	if (this->pAddr) {
		delete[] this->pAddr;
		this->pAddr = NULL;
	}
	this->pAddr = new char[strlen(str.pAddr) + 1];
	strcpy(this->pAddr, str.pAddr);
	this->mSize = str.mSize;

	return *this;
}
MyString& MyString::operator=(const char* s)
{
	cout << "char* operator=" << endl;
	if (this->pAddr) {
		delete[] pAddr;
		pAddr = NULL;
		}
	pAddr = new char[strlen(s) + 1];
	strcpy(pAddr, s);
	mSize = strlen(s);

	return *this;
}

char& MyString::operator[](int index)
{
	return pAddr[index];
}

MyString MyString::operator+(const MyString& str)
{
	cout << "string operator+" << endl;

	int newsize = this->mSize + str.mSize + 1;
	char* tmp = new char[newsize];
	memset(tmp, 0, newsize);
	strcat(tmp, this->pAddr);
	strcat(tmp, str.pAddr);

	MyString newstr(tmp);

	delete[] tmp;

	return newstr;

}
MyString MyString::operator+(const char* s)
{
	cout << "char* operator+" << endl;

	int newsize = this->mSize + strlen(s) + 1;

	char* tmp = new char[newsize];
	memset(tmp, 0, newsize);
	strcat(tmp, this->pAddr);
	strcat(tmp, s);

	MyString newstr(tmp);
	delete[] tmp;

	return newstr;
}

const char* MyString::c_str()
{
	return this->pAddr;
}
bool MyString::operator==(const MyString& str)
{
	cout << "string operator==" << endl;

	if (strcmp(this->pAddr, str.pAddr) == 0 && this->mSize == str.mSize)
	{
		return true;
	}
	return false;
}
bool MyString::operator==(const char* s)
{
	cout << "char* operator==" << endl;

	if (strcmp(this->pAddr, s) && this->mSize == strlen(s)) {
		return true;
	}
	return false;
}

MyString& MyString::operator+=(const MyString& str)
{
	cout << "string operator+=" << endl;

	MyString newstr(this->pAddr);
	
	int newsize = this->mSize + str.mSize + 1;

	if (this->pAddr) {
		delete[] this->pAddr;
		this->pAddr = NULL;
	}

	this->pAddr = new char[newsize];
	memset(this->pAddr, 0,newsize);
	strcat(this->pAddr, newstr.pAddr);
	strcat(this->pAddr, str.pAddr);
	this->mSize = newsize;

	return *this;
}
MyString& MyString::operator+=(const char* s)
{
	cout << "char* operator+=" << endl;

	MyString newstr(this->pAddr);

	int newsize = this->mSize + strlen(s) + 1;
	if (this->pAddr) {
		delete[] this->pAddr;
		this->pAddr = NULL;
	}

	this->pAddr = new char[newsize];
	memset(this->pAddr, 0, newsize);
	strcat(this->pAddr, newstr.pAddr);
	strcat(this->pAddr, s);
	this->mSize = newsize;

	return *this;
}
MyString::~MyString()
{
	cout << "destruct\n";
	if (this->pAddr)
	{
		delete[] pAddr;
		this->pAddr = NULL;
	}
}

```cpp
#include "MyString.h"

void test02()
{
	MyString s1 = "aaa";
	MyString s2 = s1;
	
	cout << "s1:" << s1 << endl;

	cout << "s2:" << s2 << endl;

	const char* s3 = "ccc";
	s1 = s3;

	cout << "s1:" << s1 << endl;
	
	s1 = s2;
	cout << "s1:" << s1 << endl;

	s1 += "bbb";
	cout << "s1:" << s1 << endl;
	s1 += s3;
	cout << "s1:" << s1 << endl;

	cout << "s1[2]: " << s1[2] << endl;

	s1[2] = '_';
	cout << "s1:" << s1 << endl;
}
void test021() {
	MyString s1("hello");
	MyString s2(s1);
	MyString s3 = "world";

	if (s1 == s2) {
		cout << "s1 == s2" << endl;
	}
	else
	{
		cout << "s1 != s2" << endl;
	}
	if (s1 == s3) {
		cout << "s1 == s3" << endl;
	}
	else
	{
		cout << "s1 != s3" << endl;
	}


	cout << "enter s3\n";
	cin >> s3;
	cout << "s3:" << s3 << endl;
}
int main()
{
	test021();


	cout << "hello world ! ... \n";
	system("pause");
	return EXIT_SUCCESS;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值