详解如何利用C++实现Mystring类_第1页
详解如何利用C++实现Mystring类_第2页
详解如何利用C++实现Mystring类_第3页
详解如何利用C++实现Mystring类_第4页
详解如何利用C++实现Mystring类_第5页
已阅读5页,还剩11页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

第详解如何利用C++实现Mystring类目录功能实现一:基本功能(实现源码)二:拓展功能完整版源码三:细节部分修改1.使用指针实例化对象部分2.重载=运算符函数的过程

功能实现

基本功能

1实现头文件的封装:MyString.h

2缺省构造函数对字符串的初始化(MyString())

3使用构造函数初始化字符串的另外两种方式*2(动态指针+拷贝构造函数)

4析构函数(释放动态申请的字符串空间)

5重载输出运算符()

6重载赋值运算符*2(=)

7重载下标运算符([],索引输出)

拓展功能

1字符串长度的比较

2字符串的排序功能

3字符串的倒置

4字符串中指定两个字符的交换

5查找某字符串是否位于指定的字符串中(采用暴力查找)

细节修改

1使用自定义函数来替换strlen()和strcpy()

一:基本功能(实现源码)

1)MyString.h

#pragmaonce

#define_CRT_SECURE_NO_WARNINGS

#includeiostream

#includestring.h//会借用strlen与strcpy函数实现相应的功能

usingnamespacestd;

classMyString{

public:

MyString();

MyString(constchar*const);

MyString(constMyString

~MyString();

intlength()const;//const函数不能修改其数据成员,仅仅起到输出数据的作用

intsize()const;//和length功能一致

constchar*getString()const;//直接调用字符串首指针返回

friendostreamoperator(ostream,constMyString//重载输出运算符

MyStringoperator=(constMyString

MyStringoperator=(constchar*);

charoperator[](constintindex);

private:

char*str;//指向数组首地址(此时为野指针)

intlen;

2)MyString.cpp

#include"MyString.h"

usingnamespacestd;

MyString::MyString()//构造空字符串

str=newchar[1];

str[0]='\0';

len=0;

MyString::MyString(constchar*constP)//按照动态指针来构造相应的字符串

if(P)

len=strlen(P);//取长度

str=newchar[len+1];//开空间

strcpy(str,P);//复制值

else

MyString();//如果传入的字符串为空,直接调用缺省值构造函数

MyString::MyString(constMyStringAnotherMyString)//拷贝构造函数,这里的形参使用了const,该形参类中的所有函数都要使用const来修饰

len=AnotherMyString.length();

str=newchar[len+1];

strcpy(str,AnotherMyString.str);

intMyString::length()const//求长度成员函数

returnlen;

intMyString::size()const

returnlen;

constchar*MyString::getString()const

returnstr;

MyStringMyString::operator=(constMyStringAnotherMyString)

if(AnotherMyString==this)

return*this;

delete[]str;

len=AnotherMyString.length();

str=newchar[len+1];

strcpy(str,AnotherMyString.str);

return*this;

//TODO:在此处插入return语句

MyStringMyString::operator=(constchar*P)

delete[]str;

len=strlen(P);

str=newchar[len+1];

strcpy(str,P);

return*this;

//TODO:在此处插入return语句

charMyString::operator[](constintindex)

if(indexlen)//如果索引越界,输出最后一个字符

cout"Warning!!!"endl;

cout"Outofboundary!Thelastcharis:";

returnstr[len-1];

else

returnstr[index-1];

//TODO:在此处插入return语句

MyString::~MyString()//释放数组空间

delete[]str;

len=0;

ostreamoperator(ostreamoutput,constMyStringstr)//重载输出运算符

outputstr.getString();

returnoutput;

//TODO:在此处插入return语句

这里需要提到的一点是析构函数中的delete[]str;

delete[]与delete的区别

使用new得来的空间使用delete释放;使用new[]得来的空间使用delete[]释放;这是永远不会错的。

但是更加深入一点去理解:

使用new[]得到的空间如果动态申请的数据类型时基本数据类型也可以使用delete直接释放,但是如果使用new[]申请的数据的类型时自定义类型(例如类名),这就必须使用delete[]来进行释放,只有这样才能够调用自定义类型的析构函数进行对自定义类型进行释放。

除此之外,再提一点关于delete[]的注意事项:

当使用new[]动态生成内存的时候,删除的时候必须将删除的指针指向new[]出来的内存的首地址:

#includeiostream

usingnamespacestd;

intmain()

int*p=newint[3];

*p=1;

p++;

*p=2;

delete[]p;

cout"*"endl;

return0;

这一段小程序中:

因为p指针不是指向了首地址,所以程序虽然没报错,但是无法正常运行!我们可以将申请的首地址保存起来,供删除的时候使用。

3)test_main.cpp

#include"MyString.h"

usingnamespacestd;

intmain()

MyStringa;

cout"【调用缺省构造函数实现初始化】"endl;

cout"stringa="aendl;

cout"Length="a.length()endlendl;

MyStringb("123456");

cout"【调用普通构造函数实现初始化】"endl;

cout"stringb="bendl;

cout"Length="b.length()endlendl;

MyStringc(b);

cout"【调用拷贝构造函数实现初始化】"endl;

cout"stringc="cendl;

cout"Length="c.length()endlendl;

MyStringd=b;//这里不会再次调用缺省构造函数进行初始化

cout"【调用=(对象)实现赋值】"endl;

cout"stringd="dendl;

cout"Length="d.length()endlendl;

MyStringe="00000000";

cout"【调用=(动态指针)实现赋值】"endl;

cout"stringd="eendl;

cout"Length="e.length()endlendl;

MyStringf="abcdefghijklmn";

charstr=f[5];

cout"【调用[]实现索引定位输出】"endl;

cout"f[5]="strendlendl;

return0;

二:拓展功能

字符串长度的比较

使用//=/=等符号进行比较,返回bool值

booloperator(constMyStringstr);booloperator(constchar*c_str);booloperator(constMyStringstr);booloperator(constchar*c_str);booloperator=(constMyStringstr);booloperator=(constchar*c_str);booloperator=(constMyStringstr);booloperator=(constchar*c_str);

字符串的排序功能

使用类中的成员函数对类中的私有字符串进行从小到大的排序:

A.Sort_String_LB();A.Sort_String_BL();

字符串的倒置

使用类中的成员函数对类中的私有字符串进行倒置:

A.Reverse();

字符串中指定两个字符的交换

A.ChangeTwoCharPosition(intfirstposition,intsecondposition);

查找某字符串是否位于指定的字符串中(采用暴力查找)

A.Find(char*search_string);

完整版源码

MyString.h

#pragmaonce

#define_CRT_SECURE_NO_WARNINGS

#includeiostream

#includestring.h//会借用strlen与strcpy函数实现相应的功能

usingnamespacestd;

classMyString{

public:

//构造函数+析构函数

MyString();

MyString(constchar*const);

MyString(constMyString

~MyString();

//直接调用字符串首指针返回,返回的指针可以直接使用cout输出

constchar*getString()const;

//求字符串的长度(直接返回类中的私有成员len的值)

//const函数不能修改其数据成员,仅仅起到输出数据的作用

intlength()const;

intsize()const;

//重载赋值运算符,使得可以使用对象与"xxxxxx"来赋值

MyStringoperator=(constMyString

MyStringoperator=(constchar*);

//重载下标运算符

charoperator[](constintindex);

//重载输出运算符

friendostreamoperator(ostream,constMyString

//字符串长度比较

booloperator(constMyStringstr);

booloperator(constchar*c_str);

booloperator(constMyStringstr);

booloperator(constchar*c_str);

booloperator=(constMyStringstr);

booloperator=(constchar*c_str);

booloperator=(constMyStringstr);

booloperator=(constchar*c_str);

//字符串内部内容的冒泡排序(ASCII码),Little-Big

voidSort_String_LB();

voidSort_String_BL();

//对字符串进行倒置

voidReverse();

//交换字符串中两个字符的位置

voidChangeTwoCharPosition(intfirstposition,intsecondposition);

//查询某字符串是否是指定字符串的子串(暴力模式)

boolFind(char*search_string);

private:

char*str;//指向数组首地址(此时为野指针)

intlen;//字符串的长度

MyString.cpp

#include"MyString.h"

usingnamespacestd;

MyString::MyString()//构造空字符串

str=newchar[1];

str[0]='\0';

len=0;

MyString::MyString(constchar*constP)//按照动态指针来构造相应的字符串

if(P)

len=strlen(P);//取长度

str=newchar[len+1];//开空间

strcpy(str,P);//复制值

else

MyString();//如果传入的字符串为空,直接调用缺省值构造函数

MyString::MyString(constMyStringAnotherMyString)//拷贝构造函数,这里的形参使用了const,该形参类中的所有函数都要使用const来修饰

len=AnotherMyString.length();

str=newchar[len+1];

strcpy(str,AnotherMyString.str);

intMyString::length()const//求长度成员函数

returnlen;

intMyString::size()const

returnlen;

constchar*MyString::getString()const

returnstr;

MyStringMyString::operator=(constMyStringAnotherMyString)

if(AnotherMyString==this)

return*this;

//delete[]str;

len=AnotherMyString.length();

str=newchar[len+1];

strcpy(str,AnotherMyString.str);

return*this;

//TODO:在此处插入return语句

MyStringMyString::operator=(constchar*P)

//delete[]str;

len=strlen(P);

str=newchar[len+1];

strcpy(str,P);

return*this;

//TODO:在此处插入return语句

charMyString::operator[](constintindex)

if(indexlen)//如果索引越界,输出最后一个字符

cout"Warning!!!"endl;

cout"Outofboundary!Thelastcharis:";

returnstr[len-1];

else

returnstr[index-1];

//TODO:在此处插入return语句

boolMyString::operator(constMyStringstr)

if(this-lenstr.len)

returntrue;

else

returnfalse;

returnfalse;

boolMyString::operator(constchar*c_str)

if(this-lenint(strlen(c_str)))

returntrue;

else

returnfalse;

returnfalse;

boolMyString::operator(constMyStringstr)

if(this-lenstr.len)

returntrue;

else

returnfalse;

returnfalse;

boolMyString::operator(constchar*c_str)

if(this-lenint(strlen(c_str)))

returntrue;

else

returnfalse;

returnfalse;

boolMyString::operator=(constMyStringstr)

if(this-lenstr.len)

returntrue;

elseif(this-len=str.len)

returntrue;

else

returnfalse;

returnfalse;

boolMyString::operator=(constchar*c_str)

if(this-lenint(strlen(c_str)))

returntrue;

elseif(this-len=strlen(c_str))

returntrue;

else

returnfalse;

returnfalse;

boolMyString::operator=(constMyStringstr)

if(this-lenstr.len)

returntrue;

elseif(this-len=str.len)

returntrue;

else

returnfalse;

returnfalse;

boolMyString::operator=(constchar*c_str)

if(this-lenint(strlen(c_str)))

returntrue;

elseif(this-len=strlen(c_str))

returntrue;

else

returnfalse;

returnfalse;

voidMyString::Sort_String_LB()

intlength=this-

chartemp_data;

char*c_str=this-

boolischanged=false;

for(inti=length-1;ii--)

for(intj=0;jj++)

if(c_str[j]c_str[j+1])

temp_data=c_str[j];

c_str[j]=c_str[j+1];

c_str[j+1]=temp_data;

ischanged=true;

if(!ischanged)

return;

voidMyString::Sort_String_BL()

intlength=this-

chartemp_data;

char*c_str=this-

boolischanged=false;

for(inti=length-1;ii--)

for(intj=0;jj++)

if(c_str[j]c_str[j+1])

temp_data=c_str[j];

c_str[j]=c_str[j+1];

c_str[j+1]=temp_data;

ischanged=true;

if(!ischang

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论