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

已知类String的原型为:

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

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

 /*
ccnu_hupo_cpp_class_tst6exercise2_by:lele_2013_10_30
new不忘delete
Design the string class in the C++ library by providing your own implementation for the following functions (name your class MyString): MyString();//构造
MyString(const char* cString);//地址不能变
char at(int index) const;//输入数组下标,返回字符
int length() const;//长度
void clear();//清空 len=0
bool empty() const;//是否清空?len不变
int compare(const MyString& s) const;//
int compare(int index, int n, const MyString& s) const;
void copy(char s[], int index, int n);
char* data() const;
int find(char ch) const;
int find(char ch, int index) const;
int find(const MyString& s, int index) const; */ #include<iostream>
#include<string.h>
using namespace std;
class MyString
{
public:
MyString(void):a(NULL){cout << "MyString()" << endl;}
MyString(const char* cString=NULL);//ordinary func
MyString(const MyString &other);//copy func
MyString & operator =(const MyString &other);//assign func
//~MyString(void);
char at(int index) const;//
int length() const;
void clear();
bool empty() const;
int compare(const MyString& s) const;
int compare(int index, int n, const MyString& s) const;
void copy(char s[], int index, int n);
char* data() const;
int find(char ch) const;
int find(char ch, int index) const;
int find(const MyString& s, int index) const; // MyString &operator+=(const MyString &other);
//MyString operator +(const MyString &mystr);
//MyString &operator +(const MyString &mystr);
//重载为友元函数,重载"+"运算符(不改变被加对象) 注意返回引用不行
friend MyString operator +(const MyString& mystr1,const MyString &mystr2);
MyString &operator+=(const MyString &other); bool operator > (const MyString & another);
bool operator < (const MyString & another);
bool operator == (const MyString & another); char& operator[](int idx); void dis();
friend ostream& operator<<(ostream& os,const MyString& other); ~MyString(); private:
char *a;
int len;
}; //MyString::MyString(){
// cout << "MyString()" << endl;
//}
MyString::~MyString(){
cout<<"~MyString():delete..."<<endl;
delete []a;
} MyString::MyString(const char* cString)
{
if (cString==NULL)
{
a=new char[];
a[]=;
//int len=0;
}
else
{
len = strlen(cString);//new
a=new char [len+];
strcpy(a,cString);
//len=strlen(cString);
}
} MyString::MyString(const MyString &other){
int len = strlen(other.a);
a = new char[len+];
strcpy(a, other.a);
} MyString &MyString::operator =(const MyString &other){
//self check assign
if(this == &other){
return *this;
}
//释放原有的内存资源
delete []a;
//分配新的内存资源,并复制内容
int len = strlen(other.a);
a = new char[len+];
strcpy(a, other.a);
//返回本对象的引用
return *this;
} char MyString::at(int index) const
{
if(len==)
{cout<<"no char in string"<<endl;
return ;
}
if(index>len)
{
cout<<"the maximun array exceed"<<endl;
return ;
}
else
{ return a[index-];
}
} int MyString::length() const
{
return len;
} void MyString::clear()
{
if(!a)
{
delete []a;
a=NULL;
} len=;
//a=" ";
}
bool MyString::empty() const
{
if(len==)
return true;
else
return false;
} int MyString::compare(const MyString& s) const
{
int m=this->len;int n=s.len; for(int i=;i<m&&i<n;i++)
{
if(s.a[i]==a[i])
continue;
else if(s.a[i]<a[i])
return ;
else
return -;
}
return ; }
int MyString::compare(int index, int n, const MyString& s) const
{
int m=len,k=s.len;
if(index+n>m||index+n>k)
{
cout<<"cannot compare!"<<endl;
///I dont know how to solve it
}
for(int i=index-,j=;i<n+index;i++,j++)
{
if(s.a[j]==a[i])
continue;
else if(s.a[j]<a[i])
return ;
else
return -;
}
return ;
} void MyString::copy(char s[], int index, int n)
{
if(n>=len)
{cout<<"the maximun array exceed"<<endl;}
else if(n+index->len)
{
cout<<"the maximun array exceed"<<endl;
return;
}
else
{
for(int i=n-,j=;i<n+index-;i++,j++)
s[j]=a[i];
} }
char* MyString::data() const
{
return a;
} int MyString::find(char ch) const
{ for(int i=;i<len;i++)
{
if(ch==a[i])
return i+;
} return ;
}
int MyString::find(char ch, int index) const
{
if(index>len||index<)
{
cout<<"the index num is should be 1~ "<<len<<endl;
return ;
}
for(int i=index-;i<len;i++)
{
if(ch==a[i])
return i+;
}
return -; } int MyString::find(const MyString& s, int index) const
{
if(index>s.len||index<)
{
cout<<"the index num is should be 1~ "<<s.len<<endl;
return ;
}
char ch=s.a[index-]; for(int i=;i<len;i++)
{
if(ch==a[i])
return i+;
} return ;
} // 加法运算符重载
//MyString MyString::operator +(const MyString & another){
// int len = strlen(this->a) + strlen(another.a);
// MyString str(""); // delete []str.a;
// str.a = new char[len + 1];
// memset(str.a,0,len + 1);
// //int len1 = strlen(this->a) + 1;
// //strcat_s(str.a, len1, this->a);
// strcat(str.a, this->a);
// // 源串长度 + 目标串长度 + 结束符
// //int len2 = strlen(this->a) + strlen(another.a) + 1;
// //strcat_s(str.a,len2, another.a);
// strcat(str.a, another.a);
// return str;
//} //MyString MyString::operator +(const MyString &mystr){//mem leak
// MyString newString("");
// if (!mystr.a)
// newString = *this;
// else if (!a)
// newString = mystr;
// else {
// newString.a = new char[strlen(a) + strlen(mystr.a) + 1];
// strcpy(newString.a, a);
// strcat(newString.a, mystr.a);
// }
// return newString;
//} //重载"+"运算符(不改变被加对象)
//MyString &MyString::operator +(const MyString &mystr){
// MyString *temp=new MyString("");
// temp->a=new char[strlen(a)+strlen(mystr.a)+1];
// strcpy(temp->a,a);
// strcat(temp->a,mystr.a);
// return *temp;
//} /*
//重载"+"运算符(改变被加对象)
Mystring &operator +(Mystring &mystr){
char *temp=str;
str=new char[strlen(temp)+strlen(mystr.str)+1];
strcpy(str,temp);
strcat(str,mystr.str);
delete [] temp;
return *this;
}
*/
//friend
MyString operator +(const MyString& mystr1,const MyString &mystr2){
MyString str("");
delete[] str.a;
str.len = strlen(mystr1.a)+strlen(mystr2.a);
str.a = new char[str.len + ];
strcpy(str.a, mystr1.a);
strcat(str.a, mystr2.a);
return str; // //MyString *temp=new MyString("");//mem leak
// MyString temp("");
// temp.a=new char[strlen(mystr1.a)+strlen(mystr2.a)+1];
// //temp->a=new char[strlen(mystr1.a)+strlen(mystr2.a)+1];
// strcpy(temp.a,mystr1.a);
// strcat(temp.a,mystr2.a);
// return temp;
} // ==关系运算符重载
bool MyString::operator==(const MyString &other)
{
if(strcmp(this->a,other.a) == )
return true;
else
return false;
}
// >关系运算符重载
bool MyString::operator>(const MyString &other)
{
if(strcmp(this->a,other.a) > )
return true;
else
return false;
}
// <运算符重载
bool MyString::operator<(const MyString &other)
{
if(strcmp(this->a,other.a) < )
return true;
else
return false;
}
// []运算符重载
char& MyString::operator[](int idx)
{
return a[idx];
} ostream& operator<<(ostream& os,const MyString& other){
return os << other.a;
} MyString &MyString::operator+=(const MyString &other)
{
int len = strlen(a) + strlen(other.a) + ;
char *newstr = new char[len];
memset(newstr, , len);
strcpy(newstr, a);
strcat(newstr, other.a); delete[] a; a = newstr;
return *this;
} int main(){
MyString ms="hello"; MyString mt("world");
mt += ms;
cout << mt << endl;
return ;
}

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. MySQL编译安装

    1.准备工作 其官方站点为http://www.mysql.com/ 为了避免发生端口冲突.程序冲突现象.建议先查询MySQL软件的安装情况,确认没有使用以RPM方式安装的mysql-server.m ...

  2. Android学习笔记(十九)——内容提供器

    //此系列博文是<第一行Android代码>的学习笔记,如有错漏,欢迎指正! 内容提供器(Content Provider)主要用于在不同的应用程序之间实现数据共享的功能,它提供了一套完整 ...

  3. (原)android中的动画(三)之动画监听&页面切换动画

    1.动画也可以设置监听事件,例如在动画结束时需要执行某操作 把要执行的代码写在onAnimationEnd()回调方法中即可: anim.setAnimationListener(new Animat ...

  4. Android碎片(Fragment)简述

    碎片(Fragment)是一种可以嵌入活动当中的UI片段,它能让程序更加合理和充分地利用大屏幕的空间,因此碎片在平板上的应用非常广泛. 你可以将碎片理解成一个迷你型的活动,水平同样可能包含布局,同样都 ...

  5. Qt5 任务栏托盘功能实现

    23333 有一阵子没写博客了,研究了挺长时间qt,学到任务栏托盘时简直无语,网上找得到的代码大多是废码,Qt5不支持或者本身就有毛病不能实现却被n多人转来转去的,甚是无语. 简单托盘功能以下在Qt5 ...

  6. Vijos 1055 奶牛浴场

    Description 求一个不覆盖指定点的最大子矩阵,\(n,m \leqslant 3\times 10^5,S \leqslant 5\times 10^3\) . Sol 没有名字的算法都叫x ...

  7. div元素抓取

    var files = $(".button").find("input[type='image']"); files.each(function() { $( ...

  8. phpcmsV9.5.8整合百度编辑器Ueditor1.4.3教程

    最近在搞phpcms视频功能,官方的视频功能实在是坑,刚开始是想将优酷的上传功能集成到ckeditor,在coding上有个项目,上传已经集成好了,还没有做上传后视频的获取和显示 项目地址:https ...

  9. SSM 集成的两个配置文件

    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> < ...

  10. 一个很详细的web.xml讲解

    <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE web-app PUBLIC "- ...