c++自定义String类

 

c++自定义String类

  748人阅读  评论(2)  收藏  举报

String.h头文件:

[cpp]  view plain copy
  1. using namespace std;  
  2. class String{  
  3. public:  
  4.     //默认构造函数  
  5.     String();  
  6.     //带参构造函数  
  7.     String(const char *str);  
  8.     //析构函数  
  9.     ~String();  
  10.     //拷贝构造  
  11.     String(const String &obj);  
  12.     //复制初始化  
  13.     String &operator=(const String &obj);  
  14. public:  
  15.     //把后面字符串加在*this上  
  16.     String &operator+=(const String &obj);  
  17.     //把后面字符串加在前面  
  18.     friend String operator+(const String &obj,const String &obj1);  
  19.     //==判断字符串是否相等  
  20.     bool operator==(const String &str)const;  
  21.     //!=判断字符串是否相等  
  22.     bool operator!=(const String &str)const;  
  23.     //[]下标运算符重载  
  24.     char operator[](int index);  
  25.     //<<运算符重载  
  26.     friend ostream &operator<<(ostream &os,const String &obj);  
  27.     //>>运算符重载  
  28.     friend istream &operator>>(istream &is,const String &obj);      
  29.     //返回字符串长度  
  30.     int leng()const;  
  31.     //取从position所指位置连续取len个字符组成子串返回  
  32.     String& subString(int position,int len);  
  33. private:  
  34.     char *_str;  
  35.     int _len;  
  36. };  

String.cpp实现文件:

[cpp]  view plain copy
  1. #include <iostream>  
  2. //#include <string>  
  3. #include "String.h"  
  4. const int maxLength=128;//字符串的最大长度  
  5. //默认构造函数  
  6. String::String(){  
  7.     _len=0;  
  8.     _str=new char[1];  
  9. }  
  10. //带参构造函数  
  11. String::String(const char *str){  
  12.     _str=new char[maxLength+1];  
  13.     if(!_str)  
  14.     {  
  15.         cerr<<"Allocation Error!\n";  
  16.         exit(1);  
  17.     }  
  18.     _len=strlen(str);  
  19.     strcpy(_str,str);  
  20. }  
  21. //析构函数  
  22. String::~String(){  
  23.     delete _str;  
  24.     _str=NULL;  
  25. }  
  26. //拷贝构造  
  27. String::String(const String &obj){  
  28.     _str=new char[maxLength+1];  
  29.     if(!_str)  
  30.     {  
  31.         cerr<<"Allocation Error!\n";  
  32.         exit(1);  
  33.     }  
  34.     _len=obj._len;  
  35.     strcpy(_str,obj._str);  
  36. }  
  37. //复制初始化  
  38. String &String::operator=(const String &obj){  
  39.     _str=new char[maxLength+1];  
  40.     if(!_str)  
  41.     {  
  42.         cerr<<"Allocation Error!\n";  
  43.         exit(1);  
  44.     }  
  45.     _len=obj._len;  
  46.     strcpy(_str,obj._str);      
  47.     return *this;          
  48. }  
  49. //把后面字符串加在前面  
  50. String &String::operator+=(const String &obj){  
  51.     char *temp=_str;//将*this->ch赋给temp  
  52.     _len=_len+obj._len;//*this->curLen为两字符串的总长度  
  53.     _str=new char[_len+1];  
  54.     if(!_str)  
  55.     {  
  56.         cerr<<"Out of memory!\n";  
  57.         exit(1);  
  58.     }  
  59.     strcpy(_str,temp);  
  60.     strcat(_str,obj._str);  
  61.     delete[] temp;  
  62.     return *this;  
  63. }  
  64. //把后面字符串加在前面  
  65. String operator+(const String &obj,const String &obj1){  
  66.     int len=obj._len+obj1._len;//两个长度加起来  
  67.     char *str0=new char[len+1];  
  68.     if(!str0)  
  69.     {  
  70.         cerr<<"Out of memory!\n";  
  71.         exit(1);  
  72.     }  
  73.     strcpy(str0,obj._str);  
  74.     strcat(str0,obj1._str);  
  75.     String str(str0);  
  76.     delete[] str0;  
  77.     return str;  
  78. }  
  79. //==判断字符串是否相等  
  80. bool String::operator==(const String &str)const{  
  81.     if(strcmp(_str, str._str)==0){  
  82.         return true;  
  83.     }else{  
  84.         return false;  
  85.     }      
  86. }  
  87. //!=判断字符串是否相等  
  88. bool String::operator!=(const String &str)const{  
  89.     if(strcmp(_str, str._str)!=0){  
  90.         return true;  
  91.     }else{  
  92.         return false;  
  93.     }  
  94. }  
  95. //[]下标运算符重载  
  96. char String::operator[](int index){  
  97.     return _str[index];  
  98. }  
  99. //<<运算符重载  
  100. ostream &operator<<(ostream &os,const String &obj){  
  101.     os<<obj._str;  
  102.     return os;  
  103. }  
  104. //>>运算符重载  
  105. istream &operator>>(istream &is,const String &obj){  
  106.     is>>obj._str;  
  107.     return is;  
  108. }  
  109. //返回字符串长度  
  110. int String::leng()const{  
  111.     return _len;  
  112. }  
  113. //取从position所指位置连续取len个字符组成子串返回  
  114. String& String::subString(int position,int len){  
  115.     if(position<0||position+len-1>=maxLength||len<0) //参数不合理,不取子串  
  116.     {  
  117.         _len=0;  
  118.         _str[0]='\0';  
  119.     }  
  120.     else  
  121.     {  
  122.         if(position+len-1>=_len)                         //字符串不够长  
  123.             len=_len-position;  
  124.         _len=len;  
  125.         for(int i=0,j=position;i<len;i++,j++)  
  126.             _str[i]=_str[j];  
  127.         _str[len]='\0';  
  128.     }  
  129.     return *this;  
  130. }  

main.cpp主函数测试:

[cpp]  view plain copy
  1. #include <iostream>  
  2. #include "String.h"  
  3. using namespace std;  
  4. int main (int argc, const char * argv[])  
  5. {  
  6.     String string="abc";//调用带参构造  
  7.     String string1="abd";//调用带参构造     
  8.     String string2(string);//调用拷贝构造  
  9.     string2=string1;//调用=运算符重载  
  10.     string2+=string;//调用+=运算符重载  
  11.     String str=string+string1;//string+string1调用+运算符重载,然后String str调用带参构造  
  12.     if(string!=string1){//比较是否不相等  
  13.         cout<<"两个字符串不相等"<<endl;  
  14.     }else{  
  15.         cout<<"两个字符串相等"<<endl;  
  16.     }  
  17.     cout<<str[0]<<endl;//调用下标运算符重载  
  18.     cout<<string<<endl;//调用<<运算符重载  
  19.     cout<<"请输入字符串:"<<endl;  
  20.     cin>>str;//调用>>运算符重载  
  21.     cout<<str<<endl;  
  22.     int len=string2.leng();//得到长度  
  23.     cout<<"string2的长度为:"<<len<<endl;  
  24.     cout<<string.subString(1, 2)<<endl;  
  25.     return 0;  
  26. }  

ok!!完成,不懂的留言,我贴的全是代码,String类你可以自己添加你自己想要的功能,我再此只想到了这些!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
为了方便,把代码放在Word里面了,每次上机实验的题目代码都在。 第一次: 对如下多项式编写定义: + + +…+ 其中,n为多项式的次数。完成如下功能: (1) 可存储任意大的多项式(提示:可用动态数组实现)。 (2) 定义构造函数、析构函数、拷贝构造函数。 (3) 包含一个static成员存储定义的多项式的数量。 (4) 定义一个成员函数输出多项式。(可参照-x^4-6x^3+5格式输出) (5) 定义一个成员函数计算多项式的值。 (6) 写main函数测试的功能。 (7) 采用多文件实现。 考虑:哪些成员函数可以声明为const. 第二次: (8) 重载“+”运算符,实现两个多项式相加。 (9) 重载“-”运算符,实现两个多项式相减。 (10) 重载“*”运算符,实现两个多项式相乘。 (11) 重载“=”运算符,实现两个多项式的赋值运算。 考虑:把其中某个运算符重载为友元函数。 第三次: C++的一般编译器都定义和封装了字符串功能,请模仿定义string的实现,可以实现并支持如下功能: (1)string s = “吉林大学”; (2)string t = s; (3)string m; m = t; (4)m.legnth() 函数测量字符串的长度 (5)m.cat(string const &)连接字符串 第四次: 我公司为仪器生产企业,目前生产摄像机和行车记录仪两种产品,分别销售给用户。 摄像机包含摄像、图像质量设定、编码算法等属性。 将摄像机增加相应芯片(具有操作菜单、自动拍摄、车速传感器、源代码等功能)后,形成一个行车记录仪。 要求: 设计摄像机,并请根据下列不同的功能要求,采用不同的继承方式,设计行车记录仪,并添加测试代码,体验不同继承方式下的成员访问属性。(设计时可根据需要自行添加数据成员和其他成员函数。) (1) 行车记录仪的芯片可以使用摄像机的摄像、图像质量设定功能。 行车记录仪用户可以操作行车记录仪的操作菜单和摄像机的摄像功能。 (2)行车记录仪的芯片可以使用摄像机的拍摄、图像质量设定功能。 行车记录仪用户仅仅可以操作行车记录仪的操作菜单。 (3) 行车记录仪的芯片可以使用摄像机的拍摄、图像质量设定功能。 行车记录仪用户仅仅可以操作行车记录仪的操作菜单 同时其他公司购买行车记录仪,因该公司也用于销售,不得泄露其全部内容 课后: (1)采用组合方式设计行车记录仪,增加相应测试代码,体验继承和组合的关系。 (2)分别为继承和组合方式下为各添加构造函数、析构函数,增加相应测试代码,体验对象的初始化和构造顺序。 (3)将摄像机和行车记录仪功能相近的函数(如拍摄、编码等功能函数)设为同名函数,增加相应测试代码,体验同名函数覆盖。 (4)为我公司建立一个多态的产品层次结构,使用抽象,测试时,创建一个指针的容器,通过基指针调用虚函数,体验多态。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值