| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 962 人关注过本帖
标题:[原创]操作符重载:实现部分string类
只看楼主 加入收藏
中学者
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:20
帖 子:3554
专家分:80
注 册:2007-9-14
结帖率:33.33%
收藏
 问题点数:0 回复次数:2 
[原创]操作符重载:实现部分string类

*/ --------------------------------------------------------------------------------------
*/ 出自: 编程中国 http://www.bc-cn.net
*/ 作者: 中学者
*/ 时间: 2007-11-18 编程论坛首发
*/ 声明: 尊重作者劳动,转载请保留本段文字
*/ --------------------------------------------------------------------------------------


最近学了操作符重载,自己写了一个很小很小很小的部分功能的string类,希望大家给点意见~


/***------------------string.h---------------------------------------****/
class string{
public:
string(); //创建一个空字符串
string(const char*); //创建一个字符串
string(const string&); //拷贝字符串
~string();
const string& operator= (const string&); //赋值给字符串
const string& operator= (const char*);
const string& operator= (char);
string& operator+ (const string&); //连接字符串
string& operator+ (const char*);
string& operator+ (char);
string& operator+= (const string&); //连接字符串,返回更新后的对象
string& operator+= (const char*);
string& operator+= (char);
friend ostream& operator<<(ostream&,const string&); //输出字符串
friend istream& operator>>(istream&,string&); //输入字符串
bool operator== (const string&); //判断字符串相等
bool operator!= (const string&); //判断字符串不相等
int length() const; //返回对象长度
int Insert(char*,int=0/*字符串长度*/,int=0/*索引*/); //添加字串到指定位置
private:
char* str;
int count;
enum{Maxsize=100};
private:
void CopyObject(const string&); //拷贝函数
};
/****--------------------------------------------------------------------------
-------------------------string.cpp---------------------------------------------
-----------------------------------------------------------------------------*/
/**------------创建空串:string::string();----------------***/
string::string():count(-1)
{
str=new char[Maxsize];
}
/*--------------创建串:string::string(const char*);------------**/
string::string(const char* str):count(strlen(str)-1)
{
this->str=new char[Maxsize];
for(int i=0;i<=count;i++)
this->str[i]=str[i];
}
/*----------拷贝构造函数:string::string(const string&);----------------**/
string::string(const string&obj):str(0)
{
CopyObject(obj);
}

void string::CopyObject(const string&obj)
{
delete [] str;
if(obj.str!=NULL)
{
str=new char[Maxsize];
count=obj.count;
for(int i=0;i<=count;i++)
str[i]=obj.str[i];
}
else
{
str=NULL;
count=-1;
}
}
/**----------------析构函数:string::~string();------------------**/
string::~string()
{
delete [] str;
count=-1;
}
/**--------------赋值操作:string& string::operator=(const string&);-------
--------------------------string& string::operator=(const char*);---------
--------------------------string& string::operator=(char);-------------***/
const string& string::operator= (const string& obj)
{
if(this!=&obj)
CopyObject(obj);
return *this;
}

const string& string::operator= (char c)
{
if(count!=-1)
{
delete [] str;
str=new char[Maxsize];
count=-1;
str[++count]=c;
}
else
{
str[++count]=c;
}
return *this;
}

const string& string::operator= (const char* str)
{
if(count==-1)
{
for(int i=0;i<static_cast<int>(strlen(str));i++)
{
this->str[++count]=str[i];
}
}
else
{
delete [] this->str;
this->str=new char[Maxsize];
count=-1;
for(int i=0;i<static_cast<int>(strlen(str));i++)
{
this->str[++count]=str[i];
}
}
return *this;
}
/***---------输入函数:istream& operator>>(istream&,string&);-----------***/
istream& operator>>(istream& in,string& obj)
{
in>>obj.str;
obj.count=strlen(obj.str)-1;
return in;
}
/**----------输出函数:ostream& operator<<(ostream&,const string&);---------***/
ostream& operator<<(ostream& out,const string&obj)
{
for(int i=0;i<=obj.count;i++)
out<<obj.str[i];
return out;
}
/***-------连接字符串: string& string::operator+( const string&);-------
-----------------------string& string::operator+(const char*);----------
-----------------------string& string::operator+(char);--------------**/
string& string::operator+ (const string& obj)
{
static string oobj;
for(int i=0;i<=count;i++)
oobj.str[++oobj.count]=str[i];
for(int j=0;j<=obj.count;j++)
oobj.str[++oobj.count]=obj.str[j];
return oobj;
}

string& string::operator+ (const char* str)
{
static string obj;
for(int i=0;i<=count;i++)
obj.str[++obj.count]=this->str[i];
for(int j=0;j<static_cast<int>(strlen(str));j++)
obj.str[++obj.count]=str[j];
return obj;
}

string& string::operator+ (char c)
{
static string obj;
for(int i=0;i<=count;i++)
obj.str[++obj.count]=this->str[i];
obj.str[++obj.count]=c;
return obj;
}
/***---------连接字符串:string& string::operator+=( const string&);-------
------------------------string& string::operator+=( const char*);---------
------------------------string& string::operator+=(char)---------------***/
string& string::operator+= (const string& obj)
{
for(int i=0;i<=obj.count;i++)
str[++count]=obj.str[i];
return *this;
}

string& string::operator+= (const char* str)
{
for(int i=0;i<strlen(str);i++)
this->str[++count]=str[i];
return *this;
}

string& string::operator+= (char c)
{
str[++count]=c;
return *this;
}
/***-----判断字符串是否相等:bool string::operator==(const string&);----------
***-------------------------bool string::operator!=(const string&);--------**/
bool string::operator== (const string & obj)
{
int flag=-1;
if(count==obj.count)
{
for(int i=0;i<=count;i++)
if(str[++flag]==obj.str[i])
continue;
else
break;
}
else
return false;
if(flag!=count)
return false;
return true;
}

bool string::operator!= (const string& obj)
{
if(*this==obj)
return false;
else
return true;
}
/***----------返回对象长度:int string::length();-------***/
int string::length() const
{
return (count+1);
}
/**-----------添加字串到指定位置:int string::Insert(int=0,char*,int);------*/
int string::Insert(char *str,int l,int i)
{
if(i==0)
{
for(int j=count;j>=0;j--)
this->str[j+l]=this->str[j];
for(int n=0;n<l;n++)
this->str[n]=str[n];
count=length()+l-1;
}
else
{
int flag=-1;
if(i==length())
{
for(int j=i;j<i+l;j++)
this->str[j]=str[++flag];
flag=-1;
count=length()+l-1;
}
else
{
for(int j=count;j>=i;j--)
this->str[j+l]=this->str[j];
for(int n=i;n<i+l;n++)
this->str[n]=str[++flag];
flag=-1;
count=length()+l-1;
}
}
return i;
}


搜索更多相关主题的帖子: 操作符 string类 重载 中国 
2007-11-18 16:56
neverDie
Rank: 1
等 级:新手上路
威 望:1
帖 子:123
专家分:0
注 册:2007-5-5
收藏
得分:0 
不错,虽然和真正的string类差的很远

2007-11-18 19:45
中学者
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:20
帖 子:3554
专家分:80
注 册:2007-9-14
收藏
得分:0 

发现代码问题,更改:
[CODE]string operator+ (const string&); //连接字符串
string operator+ (const char*);
string operator+ (char);
/***-------连接字符串: string& string::operator+( const string&);-------
-----------------------string& string::operator+(const char*);----------
-----------------------string& string::operator+(char);--------------**/
string string::operator+ (const string& obj)
{
string oobj;
for(int i=0;i<=count;i++)
oobj.str[++oobj.count]=str[i];
for(int j=0;j<=obj.count;j++)
oobj.str[++oobj.count]=obj.str[j];
return oobj;
}
string string::operator+ (const char* str)
{
string obj;
for(int i=0;i<=count;i++)
obj.str[++obj.count]=this->str[i];
for(int j=0;j<static_cast<int>(strlen(str));j++)
obj.str[++obj.count]=str[j];
return obj;
}
string string::operator+ (char c)
{
string obj;
for(int i=0;i<=count;i++)
obj.str[++obj.count]=this->str[i];
obj.str[++obj.count]=c;
return obj;
}[/CODE]


樱花大战,  有爱.
2007-11-18 21:07
快速回复:[原创]操作符重载:实现部分string类
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.012432 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved