谢谢。
解决方案 »
- c++中定义函数或变量的疑问?
- 我刚开始学习vc数据库编程,请老大们给推荐几本书,谢谢!
- 关于通过IE菜单命令实现打印控件内容的问题
- ADO出现错误
- [打印服务器管理软件开发方案]讨论
- 我新做的ATL工程为什么ONSIZE不好使呢?
- 请教关于使用appendchunk方法的问题!
- 工具栏上如何使用16位色的图标?100分!!
- 想在一个RichEdit控件中插入许多带特殊标记的关键字,并将其保存为一个文件,当下次打开时仍能调用关键字内容
- 奇怪的问题,请大家指教。
- 自己定义的对话筐类,怎么在对话框中调用视类(CView)的变量和函数
- 散分了 老板让我把两天写的demo包装一下推出去, 心里怕怕..
using namespace std ;
class StrType
{
friend ostream& operator<<(ostream& stream, StrType& o);
friend istream& operator>>(istream& stream, StrType& o);
friend StrType operator+(char *s, StrType &o);
// concatenate a quoted string with a StrType object public:
StrType():_p(0),_size(0){};
StrType(char *str);//constructor with quoted string parameter
StrType(const StrType &o); // copy constructor
~StrType();//destructor
StrType operator=(StrType& o); // assign a StrType object
StrType operator=(char* s); // assign a quoted string
StrType operator+(StrType& o); // concatenate a StrType object
StrType operator+(char* s); // concatenate a quoted string
void show()
{
cout<<_p<<endl;
}
int operator==(StrType& o);
int operator!=(StrType& o);
// operations between StrType objects and quoted strings
int operator==(char* s);
int strsize(); // return size of string
//operator char* (); // conversion to char *
private:
char* _p;
int _size;
};StrType::StrType(char *str):_size(strlen(str)+1)
{
_p = new char[_size+1];
strcpy(_p,str);
}
StrType::StrType(const StrType &o):_size(o._size){
_p = new char[_size+1];
strcpy(_p,o._p);
}
StrType::~StrType()
{
if(_p)
delete [] _p;
}
StrType StrType::operator =(StrType &o)
{
if(*this != o)
{
delete [] this->_p;
this->_p = new char[o._size+1];
strcpy(this->_p,o._p);
}
return *this;
}
StrType StrType::operator=(char * str)
{
delete [] this->_p;
this->_p = new char[strlen(str)+1];
strcpy(this->_p,str);
return *this;
}
StrType StrType::operator+(StrType &o)
{
StrType s;
s._size = _size+o._size-1;
s._p = new char[s._size];
strcpy(s._p,this->_p);
strcat(s._p,o._p);
return s;
}
StrType StrType::operator+(char *str)
{
StrType s;
s._size = _size+strlen(str);
s._p = new char[s._size];
strcpy(s._p,this->_p);
strcat(s._p,str);
return s;
}int StrType::operator==(StrType & o)
{
return strcmp(this->_p,o._p);
}
int StrType::operator!=(StrType &o)
{
return !(operator==(o));
}int StrType::operator ==(char * str)
{
return strcmp(this->_p,str);
}int StrType::strsize()
{
return this->_size - 1;
}ostream& operator<<(ostream& stream, StrType& o)
{
stream<<o._p;
return stream;
}
istream& operator>>(istream& stream, StrType& o)
{
stream>>o._p;
return stream;
}
StrType operator+(char *s, StrType &o)
{
char * temp = new char[strlen(s)+o._size+1];
strcpy(temp,s);
strcat(temp,o._p);
return StrType(temp);
}
{
Char * str;
public:
String(char *s=0);
String(const String & s);
~string();
string & string::operator=(string & s);
}String::String(char *s=0);
{
If(s)
{
str=new char[strlen(s)+1];
Strcpy(str,s);
}
Else str=0
}String::String(const String & s)
{
str=new char[strlen(s.str)+1];
Strcpy(str,s.str);
}
String::~string()
{
If(str)
{
delete[]str;
}
}
String & string::operator=(string & s)
{
if(this != &s)
{
If(str)
{
delete[]str;
}
Str=new char[strlen(s.str)+1];
Strcpy(str, s.str);
Return *this ;
}
}