MyString类的实现--C++ primer plus 读书笔记

<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">本文实现一个简单的string类,参考自c++ primer plus这本书,源码如下:</span>

//======================================================
// FileName: MyString.h
// Author: sjhuangx
// Date: 2016-03-03
// Desc: a single string class
//=======================================================

#ifndef MYSTRING_H_INCLUDED
#define MYSTRING_H_INCLUDED

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using std::ostream;
using std::istream;


class MyString
{
private:
	char					*str;							// pointer to string
	int					len;							// the length of string
	static int			num_strings;			// the number of objects
	static const int CINLIM = 80;		// the limit of cin number	
	
public:
	MyString(const char* st);				// constructor from C string
	MyString();										// default construtor
	MyString(const MyString& st);		// copy constructor
	~MyString();										// destructor
	int Length() const { return len; }	// return the length of string

	// overload operator methods
	MyString& operator=(const MyString& st);
	MyString& operator=(const char* s);
	char& operator[](int i);
	const char& operator[](int i) const;

	// overload friend operator
	friend bool operator<(const MyString& str1, const MyString& str2);
	friend bool operator>(const MyString& str1, const MyString& str2);
	friend bool operator==(const MyString& str1, const MyString& str2);
	friend ostream& operator<<(ostream& os, const MyString& st);
	friend istream& operator>>(istream& is, MyString& st);

	// static function
	int HowMany();
};

#endif // MYSTRING_H_INCLUDED

以下是类的实现部分:

//======================================================
// FileName: MyString.h
// Author: sjhuangx
// Date: 2016-03-03
// Desc: a single string class
//=======================================================


#include "MyString.h"

// static member
int MyString::num_strings = 0;

// static function
int MyString::HowMany()
{
	return num_strings;
}

// class methods
MyString::MyString(const char * s) // construct string from C string
{
	if (s == NULL)
	{
		len = 0;							// set size
		str = new char[1];			// allot storage
		str[0] = '\0';					// initialize pointer
	}
	else
	{
		len = std::strlen(s);				// set size
		str = new char[len + 1];		// allot storage
		std::strcpy(str, s);					// initialize pointer				
	}
	num_strings++;	// set object count
}

// default constructor
MyString::MyString()
{
	len = 0;
	str = new char[1];
	str[0] = '\0';					// default string
	num_strings++;
}

// necessary destructor
MyString::~MyString()
{
	--num_strings;	// update static member
	delete[] str;		// delete the space
	len = 0;				// reset len
}

// copy constructor
MyString::MyString(const MyString& st)
{
	len = st.len;							// same length
	str = new char[len + 1];		// allot space
	std::strcpy(str, st.str);			// copy date to new location
	++num_strings;					// handle static member update
}

//============ overload operator methods =============

// assign String to String
MyString& MyString::operator=(const MyString& st)
{
	if (this == &st)
	{
		return *this;
	}

	delete[] str;
	len = st.len;
	str = new char[len + 1];
	std::strcpy(str, st.str);
	return *this;
}

// assign a C string to MyString
MyString& MyString::operator=(const char* st)
{
	if (st == NULL)
	{
		return *this;
	}
	delete[] str;
	len = std::strlen(st);
	str = new char[len + 1];
	std::strcpy(str, st);
	return *this;
}

// read-write char access for non-const Strings
char& MyString::operator[](int i)
{
	if (i > len)	// if i large than length of string, then return the last of string
	{
		i = len;
	}
	if (i < 0)	// if i less then 0, than return the first of string
	{
		i = 0;
	}
	return str[i];
}

// read-only char access for const String
const char& MyString::operator[](int i) const
{
	if (i > len)	// if i large than length of string, then return the last of string
	{
		i = len;
	}
	if (i < 0)	// if i less then 0, than return the first of string
	{
		i = 0;
	}
	return str[i];
}

//============== overload friend operators ===============

// overload less operator
bool operator<(const MyString& str1, const MyString& str2)
{
	return (std::strcmp(str1.str, str2.str) < 0);
}

bool operator>(const MyString& str1, const MyString& str2)
{
	return (std::strcmp(str1.str, str2.str) > 0);
}

bool operator==(const MyString& str1, const MyString& str2)
{
	return (std::strcmp(str1.str, str2.str) == 0);
}

// simple string outpout
ostream& operator<<(ostream& os, const MyString& st)
{
	os << st.str;
	return os;
}

// simple string input
istream& operator>>(istream& is, MyString& st)
{
	char temp[MyString::CINLIM];
	is.get(temp, MyString::CINLIM);
	if (is)
	{
		st = temp;
	}
	while (is && is.get() != '\n')
	{
		continue;
	}
	return is;
}

简单测试如下:

#include "MyString.h"
using namespace std;

int main(int argc, char** argv)
{
	MyString mystr;
	mystr = "hello world";
	cout << mystr << endl;

	system("pause");
	return 0;
}





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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值