http://wenku.baidu.com/view/d7ac113243323968011c925b.html

已知类String的原型为:

  1. class String{
  2. public:
  3. String(const char *str = NULL); // 普通构造函数
  4. String(const String &other); // 拷贝构造函数
  5. ~ String(void); // 析构函数
  6. String & operator =(const String &other);// 赋值函数
  7. private:
  8. char *m_data; // 用于保存字符串
  9. };

请编写String的上述4个函数。

  1. /*
  2. ccnu_hupo_cpp_class_tst6exercise2_by:lele_2013_10_30
  3. new不忘delete
  4. Design the string class in the C++ library by providing your own implementation for the following functions (name your class MyString):
  5.  
  6. MyString();//构造
  7. MyString(const char* cString);//地址不能变
  8. char at(int index) const;//输入数组下标,返回字符
  9. int length() const;//长度
  10. void clear();//清空 len=0
  11. bool empty() const;//是否清空?len不变
  12. int compare(const MyString& s) const;//
  13. int compare(int index, int n, const MyString& s) const;
  14. void copy(char s[], int index, int n);
  15. char* data() const;
  16. int find(char ch) const;
  17. int find(char ch, int index) const;
  18. int find(const MyString& s, int index) const;
  19.  
  20. */
  21.  
  22. #include<iostream>
  23. #include<string.h>
  24. using namespace std;
  25. class MyString
  26. {
  27. public:
  28. MyString(void):a(NULL){cout << "MyString()" << endl;}
  29. MyString(const char* cString=NULL);//ordinary func
  30. MyString(const MyString &other);//copy func
  31. MyString & operator =(const MyString &other);//assign func
  32. //~MyString(void);
  33. char at(int index) const;//
  34. int length() const;
  35. void clear();
  36. bool empty() const;
  37. int compare(const MyString& s) const;
  38. int compare(int index, int n, const MyString& s) const;
  39. void copy(char s[], int index, int n);
  40. char* data() const;
  41. int find(char ch) const;
  42. int find(char ch, int index) const;
  43. int find(const MyString& s, int index) const;
  44.  
  45. // MyString &operator+=(const MyString &other);
  46. //MyString operator +(const MyString &mystr);
  47. //MyString &operator +(const MyString &mystr);
  48. //重载为友元函数,重载"+"运算符(不改变被加对象) 注意返回引用不行
  49. friend MyString operator +(const MyString& mystr1,const MyString &mystr2);
  50. MyString &operator+=(const MyString &other);
  51.  
  52. bool operator > (const MyString & another);
  53. bool operator < (const MyString & another);
  54. bool operator == (const MyString & another);
  55.  
  56. char& operator[](int idx);
  57.  
  58. void dis();
  59. friend ostream& operator<<(ostream& os,const MyString& other);
  60.  
  61. ~MyString();
  62.  
  63. private:
  64. char *a;
  65. int len;
  66. };
  67.  
  68. //MyString::MyString(){
  69. // cout << "MyString()" << endl;
  70. //}
  71. MyString::~MyString(){
  72. cout<<"~MyString():delete..."<<endl;
  73. delete []a;
  74. }
  75.  
  76. MyString::MyString(const char* cString)
  77. {
  78. if (cString==NULL)
  79. {
  80. a=new char[];
  81. a[]=;
  82. //int len=0;
  83. }
  84. else
  85. {
  86. len = strlen(cString);//new
  87. a=new char [len+];
  88. strcpy(a,cString);
  89. //len=strlen(cString);
  90. }
  91. }
  92.  
  93. MyString::MyString(const MyString &other){
  94. int len = strlen(other.a);
  95. a = new char[len+];
  96. strcpy(a, other.a);
  97. }
  98.  
  99. MyString &MyString::operator =(const MyString &other){
  100. //self check assign
  101. if(this == &other){
  102. return *this;
  103. }
  104. //释放原有的内存资源
  105. delete []a;
  106. //分配新的内存资源,并复制内容
  107. int len = strlen(other.a);
  108. a = new char[len+];
  109. strcpy(a, other.a);
  110. //返回本对象的引用
  111. return *this;
  112. }
  113.  
  114. char MyString::at(int index) const
  115. {
  116. if(len==)
  117. {cout<<"no char in string"<<endl;
  118. return ;
  119. }
  120. if(index>len)
  121. {
  122. cout<<"the maximun array exceed"<<endl;
  123. return ;
  124. }
  125. else
  126. {
  127.  
  128. return a[index-];
  129. }
  130. }
  131.  
  132. int MyString::length() const
  133. {
  134. return len;
  135. }
  136.  
  137. void MyString::clear()
  138. {
  139. if(!a)
  140. {
  141. delete []a;
  142. a=NULL;
  143. }
  144.  
  145. len=;
  146. //a=" ";
  147. }
  148. bool MyString::empty() const
  149. {
  150. if(len==)
  151. return true;
  152. else
  153. return false;
  154. }
  155.  
  156. int MyString::compare(const MyString& s) const
  157. {
  158. int m=this->len;int n=s.len;
  159.  
  160. for(int i=;i<m&&i<n;i++)
  161. {
  162. if(s.a[i]==a[i])
  163. continue;
  164. else if(s.a[i]<a[i])
  165. return ;
  166. else
  167. return -;
  168. }
  169. return ;
  170.  
  171. }
  172. int MyString::compare(int index, int n, const MyString& s) const
  173. {
  174. int m=len,k=s.len;
  175. if(index+n>m||index+n>k)
  176. {
  177. cout<<"cannot compare!"<<endl;
  178. ///I dont know how to solve it
  179. }
  180. for(int i=index-,j=;i<n+index;i++,j++)
  181. {
  182. if(s.a[j]==a[i])
  183. continue;
  184. else if(s.a[j]<a[i])
  185. return ;
  186. else
  187. return -;
  188. }
  189. return ;
  190. }
  191.  
  192. void MyString::copy(char s[], int index, int n)
  193. {
  194. if(n>=len)
  195. {cout<<"the maximun array exceed"<<endl;}
  196. else if(n+index->len)
  197. {
  198. cout<<"the maximun array exceed"<<endl;
  199. return;
  200. }
  201. else
  202. {
  203. for(int i=n-,j=;i<n+index-;i++,j++)
  204. s[j]=a[i];
  205. }
  206.  
  207. }
  208. char* MyString::data() const
  209. {
  210. return a;
  211. }
  212.  
  213. int MyString::find(char ch) const
  214. {
  215.  
  216. for(int i=;i<len;i++)
  217. {
  218. if(ch==a[i])
  219. return i+;
  220. }
  221.  
  222. return ;
  223. }
  224. int MyString::find(char ch, int index) const
  225. {
  226. if(index>len||index<)
  227. {
  228. cout<<"the index num is should be 1~ "<<len<<endl;
  229. return ;
  230. }
  231. for(int i=index-;i<len;i++)
  232. {
  233. if(ch==a[i])
  234. return i+;
  235. }
  236. return -;
  237.  
  238. }
  239.  
  240. int MyString::find(const MyString& s, int index) const
  241. {
  242. if(index>s.len||index<)
  243. {
  244. cout<<"the index num is should be 1~ "<<s.len<<endl;
  245. return ;
  246. }
  247. char ch=s.a[index-];
  248.  
  249. for(int i=;i<len;i++)
  250. {
  251. if(ch==a[i])
  252. return i+;
  253. }
  254.  
  255. return ;
  256. }
  257.  
  258. // 加法运算符重载
  259. //MyString MyString::operator +(const MyString & another){
  260. // int len = strlen(this->a) + strlen(another.a);
  261. // MyString str("");
  262.  
  263. // delete []str.a;
  264. // str.a = new char[len + 1];
  265. // memset(str.a,0,len + 1);
  266. // //int len1 = strlen(this->a) + 1;
  267. // //strcat_s(str.a, len1, this->a);
  268. // strcat(str.a, this->a);
  269. // // 源串长度 + 目标串长度 + 结束符
  270. // //int len2 = strlen(this->a) + strlen(another.a) + 1;
  271. // //strcat_s(str.a,len2, another.a);
  272. // strcat(str.a, another.a);
  273. // return str;
  274. //}
  275.  
  276. //MyString MyString::operator +(const MyString &mystr){//mem leak
  277. // MyString newString("");
  278. // if (!mystr.a)
  279. // newString = *this;
  280. // else if (!a)
  281. // newString = mystr;
  282. // else {
  283. // newString.a = new char[strlen(a) + strlen(mystr.a) + 1];
  284. // strcpy(newString.a, a);
  285. // strcat(newString.a, mystr.a);
  286. // }
  287. // return newString;
  288. //}
  289.  
  290. //重载"+"运算符(不改变被加对象)
  291. //MyString &MyString::operator +(const MyString &mystr){
  292. // MyString *temp=new MyString("");
  293. // temp->a=new char[strlen(a)+strlen(mystr.a)+1];
  294. // strcpy(temp->a,a);
  295. // strcat(temp->a,mystr.a);
  296. // return *temp;
  297. //}
  298.  
  299. /*
  300. //重载"+"运算符(改变被加对象)
  301. Mystring &operator +(Mystring &mystr){
  302. char *temp=str;
  303. str=new char[strlen(temp)+strlen(mystr.str)+1];
  304. strcpy(str,temp);
  305. strcat(str,mystr.str);
  306. delete [] temp;
  307. return *this;
  308. }
  309. */
  310. //friend
  311. MyString operator +(const MyString& mystr1,const MyString &mystr2){
  312. MyString str("");
  313. delete[] str.a;
  314. str.len = strlen(mystr1.a)+strlen(mystr2.a);
  315. str.a = new char[str.len + ];
  316. strcpy(str.a, mystr1.a);
  317. strcat(str.a, mystr2.a);
  318. return str;
  319.  
  320. // //MyString *temp=new MyString("");//mem leak
  321. // MyString temp("");
  322. // temp.a=new char[strlen(mystr1.a)+strlen(mystr2.a)+1];
  323. // //temp->a=new char[strlen(mystr1.a)+strlen(mystr2.a)+1];
  324. // strcpy(temp.a,mystr1.a);
  325. // strcat(temp.a,mystr2.a);
  326. // return temp;
  327. }
  328.  
  329. // ==关系运算符重载
  330. bool MyString::operator==(const MyString &other)
  331. {
  332. if(strcmp(this->a,other.a) == )
  333. return true;
  334. else
  335. return false;
  336. }
  337. // >关系运算符重载
  338. bool MyString::operator>(const MyString &other)
  339. {
  340. if(strcmp(this->a,other.a) > )
  341. return true;
  342. else
  343. return false;
  344. }
  345. // <运算符重载
  346. bool MyString::operator<(const MyString &other)
  347. {
  348. if(strcmp(this->a,other.a) < )
  349. return true;
  350. else
  351. return false;
  352. }
  353. // []运算符重载
  354. char& MyString::operator[](int idx)
  355. {
  356. return a[idx];
  357. }
  358.  
  359. ostream& operator<<(ostream& os,const MyString& other){
  360. return os << other.a;
  361. }
  362.  
  363. MyString &MyString::operator+=(const MyString &other)
  364. {
  365. int len = strlen(a) + strlen(other.a) + ;
  366. char *newstr = new char[len];
  367. memset(newstr, , len);
  368. strcpy(newstr, a);
  369. strcat(newstr, other.a);
  370.  
  371. delete[] a;
  372.  
  373. a = newstr;
  374. return *this;
  375. }
  376.  
  377. int main(){
  378. MyString ms="hello";
  379.  
  380. MyString mt("world");
  381. mt += ms;
  382. cout << mt << endl;
  383. return ;
  384. }

MyString(重写String)的更多相关文章

  1. 重写String类,也有些区别,供参考

    头文件如下: #pragma once #include <string> #include <string.h> #include <stdlib.h> #inc ...

  2. C++ string 类重写

    (我们知道学习C++时,在学习完C的基础内容后最先上手的就是C++的string类来学习字符串处理的内容,这里我们通过重写string类来重新认识字符串处理的内容) 1.树立string类主要函数,确 ...

  3. (1)Object类 (2)包装类和数学处理类 (3)String类

    1.Object类1.1 基本概念 java.lang.Object类是Java类层次结构的根类,任何类都是Object类的直接/间接子类. 1.2 常用的方法(重点) Object() - 无参构造 ...

  4. JavaScript学习笔记之string

    字符串定义: 1,var myString=“内容”:or var myString=‘内容’ 2,var myString= new String(“内容”)           ---〉创建对象, ...

  5. 常用API——字符串String型函数

    上图: 声明 var myString = new String(“Every good boy does fine.”); var myString = “Every good boy does f ...

  6. java内存分配和String类型的深度解析

    [尊重原创文章出自:http://my.oschina.net/xiaohui249/blog/170013] 摘要 从整体上介绍java内存的概念.构成以及分配机制,在此基础上深度解析java中的S ...

  7. Check if a string is NULL or EMPTY using PowerShell

    http://techibee.com/powershell/check-if-a-string-is-null-or-empty-using-powershell/1889 Check if a s ...

  8. java基础(六)-----String性质深入解析

    本文将讲解String的几个性质. 一.String的不可变性 对于初学者来说,很容易误认为String对象是可以改变的,特别是+链接时,对象似乎真的改变了.然而,String对象一经创建就不可以修改 ...

  9. java基础(五) String性质深入解析

    引言   本文将讲解String的几个性质. 一.String的不可变性   对于初学者来说,很容易误认为String对象是可以改变的,特别是+链接时,对象似乎真的改变了.然而,String对象一经创 ...

随机推荐

  1. phpcms标签云

    {pc:get sql="SELECT keyword FROM v9_keyword WHERE siteid=$siteid AND searchnums > 5 ORDER BY ...

  2. 一种map容器遍历的方法

    遍历算法是一种很常见而且非常重要的算法,我们用map容器的时候可能用的比较多的是查找,我今天才第一次要用到遍历.下面举个例子就知道了. map<string,string> mp; str ...

  3. html5拖拽实现

    1.需求 做一个h5正方形的拖拽框 2.分析 使用touchstart,touchmove,touchend这3个事件实现. 需要记录的数据有三组数据,分别是下图的(x0,y0),(x1,y1),(x ...

  4. angularjs入门基础一

    app.controller('firstController',function($scope,$rootScope){ $scope.name='张三'; $rootScope.age='30'; ...

  5. OpenCV成长之路(8):直线、轮廓的提取与描述

    基于内容的图像分析的重点是提取出图像中具有代表性的特征,而线条.轮廓.块往往是最能体现特征的几个元素,这篇文章就针对于这几个重要的图像特征,研究它们在OpenCV中的用法,以及做一些简单的基础应用. ...

  6. vmware vcenter orchestrator configuration提示“用户名密码错误或登录失败超过次数被锁定”

    首次登录,使用默认用户密码登录vmware/vmware vmware vcenter orchestrator configuration提示"用户名密码错误或登录失败超过次数被锁定&qu ...

  7. 表单验证神器——jquery.validate插件

    jquery.validate.js插件应用举例,ajax方式提交数据. html代码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Tr ...

  8. HEOI 2016 游记

    闲来无事,把这玩意儿补上. OI生涯中第一次正经的考试.挂的很惨. Day -1 不小心把机油(雾)sm惹毛了. 好像没啥别的事儿. Day 0 说好了上午直接去机房,然而临时说让我们上完前两节课再去 ...

  9. django xadmin 插件(1)

    1. 插件的作用可以是全局的,也可以是只针对某个模型的.通过其 init_request控制是否加载此插件, demo如下: class SCPCardOverviewPlugin(BaseAdmin ...

  10. 'ModelOptions' object has no attribute 'get_field_names

    peewee安装时随意了点.装了2.8.0的. 倒回到2.6.0就好了. sudo pip uninstall peewee sudo pip install peewee==2.6.0