String.h头文件:
- using namespace std;
- class String{
- public:
- //默认构造函数
- String();
- //带参构造函数
- String(const char *str);
- //析构函数
- ~String();
- //拷贝构造
- String(const String &obj);
- //复制初始化
- String &operator=(const String &obj);
- public:
- //把后面字符串加在*this上
- String &operator+=(const String &obj);
- //把后面字符串加在前面
- friend String operator+(const String &obj,const String &obj1);
- //==判断字符串是否相等
- bool operator==(const String &str)const;
- //!=判断字符串是否相等
- bool operator!=(const String &str)const;
- //[]下标运算符重载
- char operator[](int index);
- //<<运算符重载
- friend ostream &operator<<(ostream &os,const String &obj);
- //>>运算符重载
- friend istream &operator>>(istream &is,const String &obj);
- //返回字符串长度
- int leng()const;
- //取从position所指位置连续取len个字符组成子串返回
- String& subString(int position,int len);
- private:
- char *_str;
- int _len;
- };
String.cpp实现文件:
- #include <iostream>
- //#include <string>
- #include "String.h"
- const int maxLength=128;//字符串的最大长度
- //默认构造函数
- String::String(){
- _len=0;
- _str=new char[1];
- }
- //带参构造函数
- String::String(const char *str){
- _str=new char[maxLength+1];
- if(!_str)
- {
- cerr<<"Allocation Error!\n";
- exit(1);
- }
- _len=strlen(str);
- strcpy(_str,str);
- }
- //析构函数
- String::~String(){
- delete _str;
- _str=NULL;
- }
- //拷贝构造
- String::String(const String &obj){
- _str=new char[maxLength+1];
- if(!_str)
- {
- cerr<<"Allocation Error!\n";
- exit(1);
- }
- _len=obj._len;
- strcpy(_str,obj._str);
- }
- //复制初始化
- String &String::operator=(const String &obj){
- _str=new char[maxLength+1];
- if(!_str)
- {
- cerr<<"Allocation Error!\n";
- exit(1);
- }
- _len=obj._len;
- strcpy(_str,obj._str);
- return *this;
- }
- //把后面字符串加在前面
- String &String::operator+=(const String &obj){
- char *temp=_str;//将*this->ch赋给temp
- _len=_len+obj._len;//*this->curLen为两字符串的总长度
- _str=new char[_len+1];
- if(!_str)
- {
- cerr<<"Out of memory!\n";
- exit(1);
- }
- strcpy(_str,temp);
- strcat(_str,obj._str);
- delete[] temp;
- return *this;
- }
- //把后面字符串加在前面
- String operator+(const String &obj,const String &obj1){
- int len=obj._len+obj1._len;//两个长度加起来
- char *str0=new char[len+1];
- if(!str0)
- {
- cerr<<"Out of memory!\n";
- exit(1);
- }
- strcpy(str0,obj._str);
- strcat(str0,obj1._str);
- String str(str0);
- delete[] str0;
- return str;
- }
- //==判断字符串是否相等
- bool String::operator==(const String &str)const{
- if(strcmp(_str, str._str)==0){
- return true;
- }else{
- return false;
- }
- }
- //!=判断字符串是否相等
- bool String::operator!=(const String &str)const{
- if(strcmp(_str, str._str)!=0){
- return true;
- }else{
- return false;
- }
- }
- //[]下标运算符重载
- char String::operator[](int index){
- return _str[index];
- }
- //<<运算符重载
- ostream &operator<<(ostream &os,const String &obj){
- os<<obj._str;
- return os;
- }
- //>>运算符重载
- istream &operator>>(istream &is,const String &obj){
- is>>obj._str;
- return is;
- }
- //返回字符串长度
- int String::leng()const{
- return _len;
- }
- //取从position所指位置连续取len个字符组成子串返回
- String& String::subString(int position,int len){
- if(position<0||position+len-1>=maxLength||len<0) //参数不合理,不取子串
- {
- _len=0;
- _str[0]='\0';
- }
- else
- {
- if(position+len-1>=_len) //字符串不够长
- len=_len-position;
- _len=len;
- for(int i=0,j=position;i<len;i++,j++)
- _str[i]=_str[j];
- _str[len]='\0';
- }
- return *this;
- }
main.cpp主函数测试:
- #include <iostream>
- #include "String.h"
- using namespace std;
- int main (int argc, const char * argv[])
- {
- String string="abc";//调用带参构造
- String string1="abd";//调用带参构造
- String string2(string);//调用拷贝构造
- string2=string1;//调用=运算符重载
- string2+=string;//调用+=运算符重载
- String str=string+string1;//string+string1调用+运算符重载,然后String str调用带参构造
- if(string!=string1){//比较是否不相等
- cout<<"两个字符串不相等"<<endl;
- }else{
- cout<<"两个字符串相等"<<endl;
- }
- cout<<str[0]<<endl;//调用下标运算符重载
- cout<<string<<endl;//调用<<运算符重载
- cout<<"请输入字符串:"<<endl;
- cin>>str;//调用>>运算符重载
- cout<<str<<endl;
- int len=string2.leng();//得到长度
- cout<<"string2的长度为:"<<len<<endl;
- cout<<string.subString(1, 2)<<endl;
- return 0;
- }
ok!!完成,不懂的留言,我贴的全是代码,String类你可以自己添加你自己想要的功能,我再此只想到了这些!!