自定义string类的简单实现

大家都知道C++中有一个string类,由于正在学习类,就模仿C++中的string类写了一个自定义的string类,下面是自定义string类的简单实现(Fedora下实现)。
//MyString.h
#ifndef __MYSTRING__
#define __MYSTRING__

#include <iostream>
using namespace std;

/***********************************
 *class name:MyString
 *creat time:2011.07.23
 *last modify time:2011.07.23
 *author:hahaya
 ***********************************/

class MyString
{
    public:
	MyString();
	MyString(const char*);
	MyString(const MyString&);
	~MyString();
    public:
	MyString& operator=(const MyString&);
	MyString& operator+(const MyString&);
	MyString& operator+(const char*);
	MyString& operator+=(const MyString&);
	MyString& operator+=(const char*);
	bool operator==(const MyString&);
	bool operator==(const char*);
	bool operator!=(const MyString&);
	bool operator!=(const char*);
	char operator[](int);
	//由于输入的时候会改变MyString对象的值,则不加const限定符
	friend istream& operator>>(istream&, MyString&);
	friend ostream& operator<<(ostream&, const MyString&);
    public:
	int length();
	void UpperCase();
	void LowerCase();
    private:
	char *str;
};

#endif




//MyString.cpp
#include <iostream>
#include <cstring>
#include <cctype>
#include "MyString.h"
using namespace std;
const int MAX_STRLEN = 200;

//构造函数和析构函数
//
//无参构造函数
MyString::MyString()
{
    this->str = new char[MAX_STRLEN];
}

//带一个参数的构造函数
MyString::MyString(const char *str)
{
    this->str = new char[MAX_STRLEN];
    strcpy(this->str, str);
}

//拷贝构造函数,因为MyString类中有指针,故拷贝构造函数要重写
MyString::MyString(const MyString &mystring)
{
    this->str = new char[MAX_STRLEN];
    strcpy(this->str, mystring.str);
}

//析构函数
MyString::~MyString()
{
    delete []str;
}


//重载操作符
//
//重载=操作符
MyString& MyString::operator=(const MyString &mystring)
{
    strcpy(this->str, mystring.str);
    return *this;
}

//重载+操作符
MyString& MyString::operator+(const MyString &mystring)
{
    strcat(this->str, mystring.str);
    return *this;
}

//重载+操作符
MyString& MyString::operator+(const char *str)
{
    strcat(this->str, str);
    return *this;
}

//重载+=操作符
MyString& MyString::operator+=(const MyString &mystring)
{
    strcat(this->str, mystring.str);
    return *this;
}

//重载+=操作符
MyString& MyString::operator+=(const char *str)
{
    strcat(this->str, str);
    return *this;
}

//重载==操作符
bool MyString::operator==(const MyString &mystring)
{
    if(strcmp(this->str, mystring.str) == 0)
    {
	return true;
    }
    else
    {
	return false;
    }
}

//重载==操作符
bool MyString::operator==(const char *str)
{
    if(strcmp(this->str, str) == 0)
    {
	return true;
    }
    else
    {
	return false;
    }
}

//重载!=操作符
bool MyString::operator!=(const MyString &mystring)
{
    if(strcmp(this->str, mystring.str) == 0)
    {
	return false;
    }
    else
    {
	return true;
    }
}

//重载!=操作符
bool MyString::operator!=(const char *str)
{
    if(strcmp(this->str, str) == 0)
    {
	return false;
    }
    else
    {
	return true;
    }
}

//重载[]操作符
char MyString::operator[](int position)
{
    return *(str + position);
}

//重载>>操作符
//重载>>操作符必须用友元函数,因为istream对象会用到MyString对象的私有成员str
istream& operator>>(istream &is, MyString &mystring)
{
    is >> mystring.str;
    return is;
}

//重载<<操作符
ostream& operator<<(ostream &os, const MyString &mystring)
{
    os << mystring.str;
    return os;
}



//类方法
//
//求字符串的长度
int MyString::length()
{
    return strlen(this->str);
}

//将字符串转化成大写
void MyString::UpperCase()
{
    for(int i = 0; i < strlen(str); i++)
    {
	if(islower( *(str + i) ))
	{
	    *(str + i) = toupper( *(str + i) );
	}
    }
}

//将字符串转化成小写
void MyString::LowerCase()
{
    for(int i = 0; i < strlen(str); i++)
    {
	if(isupper( *(str + i) ))
	{
	    *(str + i) = tolower( *(str + i) );
	}
    }
}


//MyStringTest.cc
#include <iostream>
#include "MyString.h"
using namespace std;

int main()
{
    //str1为空串
    MyString str1;
    cout << str1 << endl;
 
    //str2字符串为Hello
    MyString str2("Hello");
    cout << str2 << endl;

    //str3字符串为Hello
    MyString str3 = str2;
    cout << str3 << endl;

    if(str3 == str2)
    {
	cout << "字符串str2和str3相等" << endl;
    }
    
    if(str2 != str1)
    {
	cout << "字符串str1和str2不相等" << endl;
    }


    //str4字符串为World,str3串现在为HelloWorld
    MyString str4("World");
    str3 += str4;
    cout << str3 << endl;

    //输出字符串str4的第一个字母
    cout << "str4字符串的第一个字母为:" << str4[0] << endl;

    //把字符串str4的所有字母大写
    str4.UpperCase();
    cout << "str4所有字母大写为:" << str4 << endl;

    //把字符串str4的所有字母小写
    str4.LowerCase();
    cout << "str4所有字母小写为:" << str4 << endl;
    return 0;
}



程序运行截图


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值