栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++ MyString 的友元,各种重载

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++ MyString 的友元,各种重载

#pragma once
#define _CRT_SECURE_NO_WARNINGS
#include 
using namespace std;

//我的字符串类
class MyString
{
	friend ostream& operator<<(ostream& out, const MyString& str);
	friend istream& operator>>(istream& in,  MyString& str);
public:
	MyString();
	MyString(const char* str); //MyString str="abc";
	MyString(const MyString& str);
	~MyString();
	MyString& operator=(const MyString& str);
	MyString& operator=(const char  *str);
	char& operator[](int pos);
	MyString operator+(const char* str);
	MyString operator+(const char c);
	MyString operator+(const MyString &str);
	MyString operator+(int n);
	bool operator==(const char* str);
	bool operator==(const MyString& str);
private:
	char* pString;//维护底层堆区开辟的字符数组
	int m_Size;//长度
};
#include "mystring.h"

//重载 左移
ostream& operator<<(ostream& out, const MyString& str)
{
	out << str.pString;
	return out;
}
//右移
istream& operator>>(istream& in, MyString& str)
{
	if (str.pString)
	{
		delete str.pString;
		str.pString = NULL;
		str.m_Size = 0;
	}

	char buf[1024] = {0};
	in >> buf;
	str.m_Size = strlen(buf);
	str.pString = new char[str.m_Size + 1];
	strcpy(str.pString, buf);
	return in;
}
MyString::MyString()
{
	m_Size = 0;
	pString = NULL;
}
MyString::MyString(const char* str)
{
	m_Size = strlen(str);
	pString = new char[m_Size + 1];
	strcpy(pString, str);
}

MyString::MyString(const MyString& str)
{	
	m_Size = strlen(str.pString);
	pString = new char[m_Size + 1];
	strcpy(pString, str.pString);
}

MyString::~MyString()
{
	if (pString)
	{
		delete pString;
		pString == NULL;
	}
}

MyString& MyString::operator=(const MyString& str)
{
	if (pString)
	{
		delete pString;
		pString == NULL;
	}

	m_Size = strlen(str.pString);
	pString = new char[m_Size + 1];
	strcpy(pString, str.pString);
	return *this;
}

MyString& MyString::operator=(const char* str)
{
	if (pString)
	{
		delete pString;
		pString == NULL;
	}

	m_Size = strlen(str);
	pString = new char[m_Size + 1];
	strcpy(pString, str);
	return *this;
}

char& MyString::operator[](int pos)
{
	return pString[pos];
}

MyString MyString::operator+(const char c)
{
	int newSize = m_Size + 1 + 1;
	char str[2] = {0};
	str[0] = c;	
	return *this+str;	
}

MyString MyString::operator+(const char* str)
{
	int newSize = m_Size + strlen(str) + 1;
	char *temp=new char[newSize];
	memset(temp, 0, newSize);
	strcat(temp, pString);
	strcat(temp, str);
	MyString newString(temp);
	delete[] temp;
	return newString;
}

MyString MyString::operator+(const MyString& str)
{
	int newSize = m_Size + strlen(str.pString) + 1;
	char* temp = new char[newSize];
	memset(temp, 0, newSize);
	strcat(temp, pString);
	strcat(temp, str.pString);
	MyString newString(temp);
	delete[] temp;
	return newString;
}
MyString MyString::operator+(int n)
{
	char buff[1024] = {0};
	int i=0;
	while (n)	
		buff[i++] = n % 10 + '0', n /= 10;	
	
	for (int j = 0; j < i / 2; j++)
	{
		char tmp = buff[j]; 
		buff[j] = buff[i - j - 1]; 
		buff[i - j - 1] = tmp;
	}
	
	return *this+buff;
}
bool MyString::operator==(const char* str)
{
	if (strcmp(pString, str) == 0)
		return true;
	return false;
}

bool MyString::operator==(const MyString& str)
{
	if (strcmp(pString, str.pString) == 0)
		return true;
	return false;
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1011917.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号