C++ string类的实现


string类的简单实现


/*  
 *  String类的简单实现
 *  Build 20160701
 *  Author: LonelyEnvoy
 *  Copyright (c) 2015-2016
 *  All Rights Reserved.
 */


#define MAX_LENGTH 50
#define NULL 0

#include <iostream>
using std::ostream;
using std::istream;
using std::cout;
using std::endl;

class String
{
public:
	String(const char* s = NULL);
	String(const String &obj);
	~String();
	void set(char* s);
	char* get() const;
	void print() const;
	int size() const;

	static int strlen(const char* s);
	static char* strcat(char* s1, const char* s2);
	static char* strcpy(char* s1, const char* s2);

	String operator+(String& obj2);
	String& operator=(String& obj2);
	String& operator=(const char* s2);
	String& operator+=(String& obj2);
	char& operator[](unsigned n);
	friend ostream & operator << (ostream & os, const String & obj);
	friend istream & operator >> (istream & is, String & obj);

private:
	char* str;
};


 

String::String(const char * s)
{
	if (s == NULL)
		str = NULL;
	else
	{
		str = new char[strlen(s) + 1];
		strcpy(str, s);
	}
}

String::String(const String & obj)
{
	if (obj.str == NULL)
		str = NULL;
	else
	{
		str = new char[strlen(obj.str) + 1];
		strcpy(str, obj.str);
	}
}

String::~String()
{
	if (str == NULL)
		return;
	delete[]str;
}

void String::set(char * s)
{
	if (s == NULL)
		return;
	if (str != NULL)
		delete[]str;

	str = new char[strlen(s) + 1];
	
	int i;
	for (i = 0; i < strlen(s); i++)
		str[i] = s[i];
	s[i] = '\0';
}

char * String::get() const
{
	return str;
}

void String::print() const
{
	for (int i = 0; i < strlen(str); i++)
		cout << str[i];
	cout << endl;
}

int String::size() const
{
	return strlen(str);
}

int String::strlen(const char * s)
{
	if (s == NULL)
		return 0;
	int i = 0;
	while (s[++i] != '\0');
	return i;
}

char * String::strcat(char * s1, const char * s2)
{
	// s1 must have enough space for storage
	if (s1 == NULL || s2 == NULL)
		return NULL;
	int i, j;
	for (i = strlen(s1), j = 0; j < strlen(s2); i++, j++)
		s1[i] = s2[j];
	s1[i] = '\0';
	return s1;
}

char * String::strcpy(char * s1, const char * s2)
{
	// s1 must have enough space for storage
	if (s1 == NULL || s2 == NULL)
		return NULL;
	int i;
	for (i = 0; i < strlen(s2); i++)
		s1[i] = s2[i];
	s1[i] = '\0';
	return s1;
}

String String::operator+(String & obj2)
{
	String s;
	if (str == NULL || obj2.str == NULL)
		return String();
	else if (str == NULL)
		s = obj2;
	else if (obj2.str == NULL)
		s = *this;
	else
	{
		s.str = new char[strlen(str) + strlen(obj2.str) + 1];
		strcpy(s.str, str);
		strcat(s.str, obj2.str);
	}
	return s;
}

String & String::operator=(String & obj2)
{
	if (this != &obj2)
	{
		delete[]str;
		if (obj2.str == NULL)
			str = NULL;
		else
		{
			str = new char[strlen(obj2.str) + 1];
			strcpy(str, obj2.str);
		}
	}
	return obj2;
}

String & String::operator=(const char * s2)
{
	String temp(s2);
	operator=(temp);
	return temp;
}

String & String::operator+=(String & obj2)
{
	operator=(operator+(obj2));
	return obj2;
}

char & String::operator[](unsigned n)
{
	if (n >= 0 && n < (unsigned)size())
		return str[n];
	throw 1; // 越界抛出异常
}

ostream & operator<<(ostream & os, const String & obj)
{
	os << obj.str;
	return os;
}

istream & operator>>(istream & is, String & obj)
{
	char temp[MAX_LENGTH];
	is >> temp;
	obj = temp;
	return is;
}




 

转载于:https://www.cnblogs.com/LonelyEnvoy/p/5850680.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值