Chinaunix首页 | 论坛 | 博客
  • 博客访问: 88257
  • 博文数量: 34
  • 博客积分: 2000
  • 博客等级: 大尉
  • 技术积分: 395
  • 用 户 组: 普通用户
  • 注册时间: 2009-03-23 10:22
文章分类

全部博文(34)

文章存档

2011年(1)

2010年(4)

2009年(29)

我的朋友

分类: C/C++

2009-05-07 16:06:10

#include "stdafx.h"
#include
#include
#include
#include
using namespace std;
//newString类实现:一个处理字符串的类
class newString
{
 friend ostream& operator<<(ostream&,const newString&);
 friend istream& operator>>(istream&,newString&);
public:
 const newString& operator=(const newString&);
 //返回const引用,禁止(a=b)=c通过
 newString(const char *);
 //constructor;conversion from the char string
 newString();
 //default constructor
 newString(const newString&);
 //copy constructor
 ~newString();
 //destructor
 char& operator[] (int);
 const char& operator[] (int) const;
 //overload the relational operators
 bool operator==(const newString&) const;
 bool operator!=(const newString&) const;
 bool operator<=(const newString&) const;
 bool operator >=(const newString&) const;
 bool operator<(const newString&) const;
 bool operator>(const newString&) const ;
private:
 char *strPtr;
 int strLength;
};
newString::newString (const char* str)
{
 strLength=strlen(str);
 strPtr=new char[strLength+1];
 assert(strPtr!=NULL);
 strcpy(strPtr,str);
}
//default constructor
newString::newString ()
{
 strLength=0;
 strPtr=new char[1];
 assert(strPtr!=NULL);
 strcpy(strPtr,"");
}
//copy constructor:use deep copy
newString::newString (const newString& rstr)
{
 strLength=rstr.strLength ;
 strPtr=new char[strLength+1];
 assert(strPtr!=NULL);
 strcpy(strPtr,rstr.strPtr);
}
newString::~newString ()
{
 delete [] strPtr;
 strPtr=NULL;
}
//overload the assignment operator
const newString& newString::operator=(const newString& rStr)
{
 if(this!=&rStr)
 {
  delete [] strPtr;
  strLength=rStr.strLength ;
  strPtr=new char[strLength+1];
  assert(strPtr!=NULL);
  strcpy(strPtr,rStr.strPtr );
 }
 return *this;
}
char& newString::operator[](int index)
{
 assert(index>=0 && index return strPtr[index];
}
const char& newString::operator[](int index) const
{
 assert(index>=0 && index return strPtr[index];
}
//overload the relational operator
bool newString::operator==(const newString& rStr) const
{
 return(strcmp(strPtr,rStr.strPtr )==0);
}
bool newString::operator !=(const newString& rStr) const
{
 return !(*this==rStr);
}
bool newString::operator<(const newString& rStr) const
{
 return (strcmp(strPtr,rStr.strPtr)<0 );
}
bool newString::operator<=(const newString& rStr) const
{
 return (strcmp(strPtr,rStr.strPtr)<=0 );
}
bool newString::operator>(const newString& rStr) const
{
 return (strcmp(strPtr,rStr.strPtr)>0 );
}
bool newString::operator>=(const newString& rStr) const
{
 return (strcmp(strPtr,rStr.strPtr)>=0 );
}
//overload the stream insertion operator <<
ostream& operator<<(ostream& os,const newString& str)
{
 os< return os;
}
istream& operator>>(istream& is,newString& str)
{
 char temp[81];
 is>>setw(81)>>temp;
 //ensure the char string read less than 80
 str=temp;
 return is;
}
//test program
int main()
{
 newString s1="Sunny";
 const newString s2("Warm");
 newString s3;
 newString s4;
 cout<<"s1 and s2 are :"<
 if(s1<=s2)
  cout<<"s1 is less than s2"< else
  cout<<"s2 is less than s1"<
 cout<<"enter a string not less than 7 characters:"< cin>>s1;
 cout<<"the new value of s1:"<
 s4=s3="Birthday";
 cout<<"s3 and s4 are:"<
 cout<<"s3[3]="<
 s3[3]=s1[2];
 cout<<"After s3[3]=s1[2]:"< cout<<"new value of s3 is:"< 
 return 0;
}
阅读(617) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~