C++ 重载String字符串类

myString.h

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

class myString
{
public:
	myString();
	myString(int len);
	myString(const myString& Str);
	myString(const char* str);
	~myString();
	//char& getString();//多余
	int getLen();
	myString& operator+(myString& str);
	myString& operator=(myString& str);
	char& operator[](int index);
	bool operator==(const myString& str)const;//const修饰this指针
	bool operator!=(const myString& str)const;
	bool operator>(const myString& str)const;
	bool operator<(const myString& str)const;

private:
	char* m_p;
	int m_len;
	friend istream& operator>>(istream& in, myString& str);
	friend ostream& operator<<(ostream& out, myString& str);
};

myString.cpp

#define _CRT_SECURE_NO_WARNINGS
#include "myString.h"

myString& myString::operator=(myString& str)
{
	if (this->m_p != NULL)
	{
		delete[] m_p;
		m_p = NULL;
		m_len = 0;
	}
	this->m_len = str.m_len;
	m_p = new char[this->m_len + 1];
	for (int i = 0; i < m_len; i++)
	{
		this->m_p[i] = str.m_p[i];
	}
	return *this;
}

char& myString::operator[](int index)
{
	return m_p[index];
}

bool myString::operator==(const myString& str) const
{
	if (this->m_len != str.m_len)
		return false;
	else
	{
		for (int i = 0; i < m_len; i++)
		{
			if (this->m_p[i] != str.m_p[i])
				return false;
		}
		return true;
	}
}

bool myString::operator!=(const myString& str) const
{
	return !(*this == str);
}

bool myString::operator>(const myString& str) const
{
	if (strcmp(this->m_p, str.m_p) > 0)
		return true;
	else
	{
		return false;
	}
}

bool myString::operator<(const myString& str) const
{
	if (strcmp(this->m_p, str.m_p) < 0)
		return true;
	else
	{
		return false;
	}
}

istream& operator>>(istream& in, myString& str)
{
	char buf[1024] = { 0 };
	char* tmp = buf;
	cout << "请输入1个字符串:";
	in >> tmp;
	str.m_len = strlen(tmp);
	str.m_p = new char[str.m_len + 1];
	strcpy(str.m_p, buf);
	return in;
}

ostream& operator<<(ostream& out, myString& str)
{
	for (int i = 0; i < str.m_len; i++)
	{
		out << str.m_p[i];
	}
	out << endl;
	// TODO: 在此处插入 return 语句
	return out;
}

myString::myString()
{
	this->m_len = 0;
	this->m_p = NULL;
}

myString::myString(int len)
{
	this->m_len = len;
	m_p = new char[m_len + 1];
}

myString::myString(const myString& Str)
{
	this->m_len = Str.m_len;
	m_p = new char[m_len + 1];
	strcpy(m_p, Str.m_p);
}

myString::myString(const char* str)
{
	this->m_len = strlen(str);
	this->m_p = new char[this->m_len + 1];
	for (int i = 0; i < m_len; i++)
	{
		this->m_p[i] = str[i];
	}
}

myString::~myString()
{
	if (this->m_p != NULL)
	{
		delete[] m_p;
		m_p = NULL;
		m_len = 0;
	}
}

int myString::getLen()
{
	return this->m_len;
}

myString& myString::operator+(myString& str)
{
	//int len = m_len + str.m_len;
	strcat(this->m_p, str.m_p);
	return *this;
}

myStringMain.cpp

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include "myString.h"
using namespace std;

int main()
{
	myString s1(5), s2(5);
	cin >> s1;
	cout << s1 << endl;
	cin >> s2;
	cout << s2 << endl;
	
	myString s3(10);
	
	cout << s1[2] << endl;

	if (s1 == s2)
		cout << "===" << endl;
	if (s1 > s2)
		cout << "s1 > s2" << endl;
	if (s1 < s2)
		cout << "s1 < s2" << endl;

	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

banjitino

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

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

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

打赏作者

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

抵扣说明:

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

余额充值