《CPlusPlusPrimer》第三章编程源码——String类的简单实现

本文介绍了一个简单的自定义String类的设计与实现,包括构造函数、析构函数、重载运算符等关键部分,并通过示例展示了如何使用这个类。

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

String.h

#ifndef _CPlusPlusPrimer_chapter3_string
#define _CPlusPlusPrimer_chapter3_string

#include <iostream>

using namespace std;



namespace Icanth_CPlusPlusPrimer
{

	class String; 
	// 输入输出操作作为非成员函数实现的
	istream& operator>> ( istream&, String &);
	ostream& operator<< ( ostream&, String &);

	class String
	{
	public:
		// 构造函数
		String();
		String(const String &); // 串拷贝函数
		String(const char*);
		
		// 析构函数
		~String();
		
		// 重载下标操作符
		char &operator[](int );

		bool operator==(const String &);
		bool operator==(const char*);
		
		bool operator!=(const String &);
		bool operator!=(const char*);

		String &operator=(const String &);
		String &operator=(const char *);

		// 成员函数定义
		int size() { return _size; }
		char * c_str() { return _string; }

	private:
		int _size;
		char* _string;
	};
}

#endif



String.cpp

#include "string.h"
#include <stdlib.h>
#include <string.h>
#include <iomanip> // setw

using namespace Icanth_CPlusPlusPrimer;

inline
String::String()
{
	_size = 0;
	_string = 0; 
}

inline
String::String(const char* str)
{
	if(!str)
	{
		_size = 0;
		_string = 0;
	}
	else
	{
		_size = strlen(str);
		_string = new char[_size+1]; // 用于存最后一个字符串'/0'结束符
		strcpy(_string, str);
	}
}

inline
String::String(const String &rhs)
{
	_size = rhs._size;
	if(!rhs._size)
	{
		_string = 0;
	}
	else
	{
		_string = new char[_size + 1];
		strcpy(_string, rhs._string);
	}
}

inline 
String::~String()
{
	_size = 0;
	delete[] _string;
}

inline String&
String::operator=(const char *s)
{
	if( ! s )
	{
		_size = 0;
		delete [] _string;
		_string = 0;
	}
	else
	{
		_size = strlen(s);
		delete[] _string;
		_string = new char[ _size + 1 ];
		strcpy(_string, s);
	}
	
	return *this;
}

inline istream& 
Icanth_CPlusPlusPrimer::operator>>( istream &io, String &s)
{
	// 人工限制输入最大4096字符
	const int limit_string_size = 4096;
	char inBuf [ limit_string_size ];
	
	io >> setw(limit_string_size) >> inBuf;
	s = inBuf; // String::operator=(const char&)

	return io;
}

inline ostream&
Icanth_CPlusPlusPrimer::operator<< ( ostream& io,String & s)
{
	const char* str = s.c_str();
	io << str;

	return io;
}

inline bool
String::operator==(const String &s)
{
	if(_size != s._size)
		return false;
	
	if(0 == strcmp(_string, s._string))
	{
		return true;
	}
	else
	{
		return false;
	}
}

inline bool
String::operator==(const char* s)
{
	if( _size != strlen(s) )
	{
		return false;
	}
	else
	{
		if(0 == strcmp(_string, s))
		{
			return true;
		}
		else
		{
			return false;
		}
	}
}

inline bool
String::operator!=(const String &s)
{
	return  !((*this) == s);
}

inline bool 
String::operator!=(const char* s)
{
	return !((*this) == s);
}

inline char &
String::operator[](int index)
{
	return _string[index];
}


test.cpp

#include "string.cpp"
#include <iostream>

namespace  ICP = Icanth_CPlusPlusPrimer;
using namespace std;

int main()
{
	char *str = "This is a test string stored in char*";

	using ICP::String;
	String str1;
	String str2(str);
	String str3(str2);

	cout << "Testing str2 is equal to str3 with [bool operator==(String &)]: \n" << (str2 == str3) << endl;

	cout << "Testing str3 is equal to str with [bool operator==(char *)]: \n" << (str3 == str) << endl;

	cin >> str1;
	cout << "The str1 that u just input is :\n" << str1 << endl; 

	cout << "Testing str1 is not equal to str with [bool operator!=(char *)]: \n" << (str3 != str1) << endl;
	
	cout << "The str3[2] is:" << str3[2] << endl;

	return 0;
}



运行截图:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值