TPassward.h
上传用户:wenliang_x
上传日期:2013-05-26
资源大小:19k
文件大小:1k
- #ifndef TPASSWARDH
- #define TPASSWARDH
- class TPassward //定义能任意长度的字符串对象
- {
- private:
- char* string; //字符串首地址
- int len; //字符串长度
- public:
- //缺省构造函数
- TPassward()
- {
- len = 0;
- string = new char[len+1];
- strcpy(string,"");
- }
-
- //重载构造函数
- TPassward(int _len)
- {
- len = _len;
- string = new char[len+1];
- }
- //拷贝构造函数
- TPassward(TPassward &S)
- {
- len = S.len;
- string = new char [len+1];
- strcpy(string,S.string);
- }
- //得到字符串首地址
- char* GetString()
- {
- return string;
- }
- //两个对象相等
- TPassward operator=(TPassward& S)
- {
- len = S.len;
- delete string;
- string = new char[len+1];
- strcpy(string,S.string);
- return *this;
- }
- //加上一个字符
- TPassward operator+(char c)
- {
- TPassward temp(len+1);
- strcpy(temp.string,string);
- temp.string[len]=c;
- temp.string[len+1]=' ';
- *this = temp;
- return *this;
- }
- //是否和一个字符串相等
- int operator==(const char *const s)
- {
- int slen = strlen(s);
- if (len != slen)
- return false;
- for (int i=0;i<len;i++)
- if (string[i] != s[i])
- return false;
- return true;
- }
- //清空字符串
- void clear()
- {
- len = 0;
- delete string;
- string = new char[len+1];
- strcpy(string,"");
- }
- //析构函数
- ~TPassward()
- {
- len = 0;
- delete string;
- }
- };
- #endif