




版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
第詳解如何利用C++實(shí)現(xiàn)Mystring類目錄功能實(shí)現(xiàn)一:基本功能(實(shí)現(xiàn)源碼)二:拓展功能完整版源碼三:細(xì)節(jié)部分修改1.使用指針實(shí)例化對(duì)象部分2.重載=運(yùn)算符函數(shù)的過(guò)程
功能實(shí)現(xiàn)
基本功能
1實(shí)現(xiàn)頭文件的封裝:MyString.h
2缺省構(gòu)造函數(shù)對(duì)字符串的初始化(MyString())
3使用構(gòu)造函數(shù)初始化字符串的另外兩種方式*2(動(dòng)態(tài)指針+拷貝構(gòu)造函數(shù))
4析構(gòu)函數(shù)(釋放動(dòng)態(tài)申請(qǐng)的字符串空間)
5重載輸出運(yùn)算符()
6重載賦值運(yùn)算符*2(=)
7重載下標(biāo)運(yùn)算符([],索引輸出)
拓展功能
1字符串長(zhǎng)度的比較
2字符串的排序功能
3字符串的倒置
4字符串中指定兩個(gè)字符的交換
5查找某字符串是否位于指定的字符串中(采用暴力查找)
細(xì)節(jié)修改
1使用自定義函數(shù)來(lái)替換strlen()和strcpy()
一:基本功能(實(shí)現(xiàn)源碼)
1)MyString.h
#pragmaonce
#define_CRT_SECURE_NO_WARNINGS
#includeiostream
#includestring.h//會(huì)借用strlen與strcpy函數(shù)實(shí)現(xiàn)相應(yīng)的功能
usingnamespacestd;
classMyString{
public:
MyString();
MyString(constchar*const);
MyString(constMyString
~MyString();
intlength()const;//const函數(shù)不能修改其數(shù)據(jù)成員,僅僅起到輸出數(shù)據(jù)的作用
intsize()const;//和length功能一致
constchar*getString()const;//直接調(diào)用字符串首指針?lè)祷?/p>
friendostreamoperator(ostream,constMyString//重載輸出運(yùn)算符
MyStringoperator=(constMyString
MyStringoperator=(constchar*);
charoperator[](constintindex);
private:
char*str;//指向數(shù)組首地址(此時(shí)為野指針)
intlen;
2)MyString.cpp
#include"MyString.h"
usingnamespacestd;
MyString::MyString()//構(gòu)造空字符串
str=newchar[1];
str[0]='\0';
len=0;
MyString::MyString(constchar*constP)//按照動(dòng)態(tài)指針來(lái)構(gòu)造相應(yīng)的字符串
if(P)
len=strlen(P);//取長(zhǎng)度
str=newchar[len+1];//開(kāi)空間
strcpy(str,P);//復(fù)制值
else
MyString();//如果傳入的字符串為空,直接調(diào)用缺省值構(gòu)造函數(shù)
MyString::MyString(constMyStringAnotherMyString)//拷貝構(gòu)造函數(shù),這里的形參使用了const,該形參類中的所有函數(shù)都要使用const來(lái)修飾
len=AnotherMyString.length();
str=newchar[len+1];
strcpy(str,AnotherMyString.str);
intMyString::length()const//求長(zhǎng)度成員函數(shù)
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語(yǔ)句
MyStringMyString::operator=(constchar*P)
delete[]str;
len=strlen(P);
str=newchar[len+1];
strcpy(str,P);
return*this;
//TODO:在此處插入return語(yǔ)句
charMyString::operator[](constintindex)
if(indexlen)//如果索引越界,輸出最后一個(gè)字符
cout"Warning!!!"endl;
cout"Outofboundary!Thelastcharis:";
returnstr[len-1];
else
returnstr[index-1];
//TODO:在此處插入return語(yǔ)句
MyString::~MyString()//釋放數(shù)組空間
delete[]str;
len=0;
ostreamoperator(ostreamoutput,constMyStringstr)//重載輸出運(yùn)算符
outputstr.getString();
returnoutput;
//TODO:在此處插入return語(yǔ)句
這里需要提到的一點(diǎn)是析構(gòu)函數(shù)中的delete[]str;
delete[]與delete的區(qū)別
使用new得來(lái)的空間使用delete釋放;使用new[]得來(lái)的空間使用delete[]釋放;這是永遠(yuǎn)不會(huì)錯(cuò)的。
但是更加深入一點(diǎn)去理解:
使用new[]得到的空間如果動(dòng)態(tài)申請(qǐng)的數(shù)據(jù)類型時(shí)基本數(shù)據(jù)類型也可以使用delete直接釋放,但是如果使用new[]申請(qǐng)的數(shù)據(jù)的類型時(shí)自定義類型(例如類名),這就必須使用delete[]來(lái)進(jìn)行釋放,只有這樣才能夠調(diào)用自定義類型的析構(gòu)函數(shù)進(jìn)行對(duì)自定義類型進(jìn)行釋放。
除此之外,再提一點(diǎn)關(guān)于delete[]的注意事項(xiàng):
當(dāng)使用new[]動(dòng)態(tài)生成內(nèi)存的時(shí)候,刪除的時(shí)候必須將刪除的指針指向new[]出來(lái)的內(nèi)存的首地址:
#includeiostream
usingnamespacestd;
intmain()
int*p=newint[3];
*p=1;
p++;
*p=2;
delete[]p;
cout"*"endl;
return0;
這一段小程序中:
因?yàn)閜指針不是指向了首地址,所以程序雖然沒(méi)報(bào)錯(cuò),但是無(wú)法正常運(yùn)行!我們可以將申請(qǐng)的首地址保存起來(lái),供刪除的時(shí)候使用。
3)test_main.cpp
#include"MyString.h"
usingnamespacestd;
intmain()
MyStringa;
cout"【調(diào)用缺省構(gòu)造函數(shù)實(shí)現(xiàn)初始化】"endl;
cout"stringa="aendl;
cout"Length="a.length()endlendl;
MyStringb("123456");
cout"【調(diào)用普通構(gòu)造函數(shù)實(shí)現(xiàn)初始化】"endl;
cout"stringb="bendl;
cout"Length="b.length()endlendl;
MyStringc(b);
cout"【調(diào)用拷貝構(gòu)造函數(shù)實(shí)現(xiàn)初始化】"endl;
cout"stringc="cendl;
cout"Length="c.length()endlendl;
MyStringd=b;//這里不會(huì)再次調(diào)用缺省構(gòu)造函數(shù)進(jìn)行初始化
cout"【調(diào)用=(對(duì)象)實(shí)現(xiàn)賦值】"endl;
cout"stringd="dendl;
cout"Length="d.length()endlendl;
MyStringe="00000000";
cout"【調(diào)用=(動(dòng)態(tài)指針)實(shí)現(xiàn)賦值】"endl;
cout"stringd="eendl;
cout"Length="e.length()endlendl;
MyStringf="abcdefghijklmn";
charstr=f[5];
cout"【調(diào)用[]實(shí)現(xiàn)索引定位輸出】"endl;
cout"f[5]="strendlendl;
return0;
二:拓展功能
字符串長(zhǎng)度的比較
使用//=/=等符號(hào)進(jìn)行比較,返回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);
字符串的排序功能
使用類中的成員函數(shù)對(duì)類中的私有字符串進(jìn)行從小到大的排序:
A.Sort_String_LB();A.Sort_String_BL();
字符串的倒置
使用類中的成員函數(shù)對(duì)類中的私有字符串進(jìn)行倒置:
A.Reverse();
字符串中指定兩個(gè)字符的交換
A.ChangeTwoCharPosition(intfirstposition,intsecondposition);
查找某字符串是否位于指定的字符串中(采用暴力查找)
A.Find(char*search_string);
完整版源碼
MyString.h
#pragmaonce
#define_CRT_SECURE_NO_WARNINGS
#includeiostream
#includestring.h//會(huì)借用strlen與strcpy函數(shù)實(shí)現(xiàn)相應(yīng)的功能
usingnamespacestd;
classMyString{
public:
//構(gòu)造函數(shù)+析構(gòu)函數(shù)
MyString();
MyString(constchar*const);
MyString(constMyString
~MyString();
//直接調(diào)用字符串首指針?lè)祷兀祷氐闹羔樋梢灾苯邮褂胏out輸出
constchar*getString()const;
//求字符串的長(zhǎng)度(直接返回類中的私有成員len的值)
//const函數(shù)不能修改其數(shù)據(jù)成員,僅僅起到輸出數(shù)據(jù)的作用
intlength()const;
intsize()const;
//重載賦值運(yùn)算符,使得可以使用對(duì)象與"xxxxxx"來(lái)賦值
MyStringoperator=(constMyString
MyStringoperator=(constchar*);
//重載下標(biāo)運(yùn)算符
charoperator[](constintindex);
//重載輸出運(yùn)算符
friendostreamoperator(ostream,constMyString
//字符串長(zhǎng)度比較
booloperator(constMyStringstr);
booloperator(constchar*c_str);
booloperator(constMyStringstr);
booloperator(constchar*c_str);
booloperator=(constMyStringstr);
booloperator=(constchar*c_str);
booloperator=(constMyStringstr);
booloperator=(constchar*c_str);
//字符串內(nèi)部?jī)?nèi)容的冒泡排序(ASCII碼),Little-Big
voidSort_String_LB();
voidSort_String_BL();
//對(duì)字符串進(jìn)行倒置
voidReverse();
//交換字符串中兩個(gè)字符的位置
voidChangeTwoCharPosition(intfirstposition,intsecondposition);
//查詢某字符串是否是指定字符串的子串(暴力模式)
boolFind(char*search_string);
private:
char*str;//指向數(shù)組首地址(此時(shí)為野指針)
intlen;//字符串的長(zhǎng)度
MyString.cpp
#include"MyString.h"
usingnamespacestd;
MyString::MyString()//構(gòu)造空字符串
str=newchar[1];
str[0]='\0';
len=0;
MyString::MyString(constchar*constP)//按照動(dòng)態(tài)指針來(lái)構(gòu)造相應(yīng)的字符串
if(P)
len=strlen(P);//取長(zhǎng)度
str=newchar[len+1];//開(kāi)空間
strcpy(str,P);//復(fù)制值
else
MyString();//如果傳入的字符串為空,直接調(diào)用缺省值構(gòu)造函數(shù)
MyString::MyString(constMyStringAnotherMyString)//拷貝構(gòu)造函數(shù),這里的形參使用了const,該形參類中的所有函數(shù)都要使用const來(lái)修飾
len=AnotherMyString.length();
str=newchar[len+1];
strcpy(str,AnotherMyString.str);
intMyString::length()const//求長(zhǎng)度成員函數(shù)
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語(yǔ)句
MyStringMyString::operator=(constchar*P)
//delete[]str;
len=strlen(P);
str=newchar[len+1];
strcpy(str,P);
return*this;
//TODO:在此處插入return語(yǔ)句
charMyString::operator[](constintindex)
if(indexlen)//如果索引越界,輸出最后一個(gè)字符
cout"Warning!!!"endl;
cout"Outofboundary!Thelastcharis:";
returnstr[len-1];
else
returnstr[index-1];
//TODO:在此處插入return語(yǔ)句
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. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 光伏發(fā)電系統(tǒng)故障診斷與維護(hù)技術(shù)光伏組件熱斑故障分析考核試卷
- 冷凍飲品生產(chǎn)環(huán)境中的空氣質(zhì)量管理考核試卷
- 海鮮養(yǎng)殖面試題及答案
- 船舶輻射考試題及答案
- 防震減災(zāi)面試題及答案
- 三一技師考試試題及答案
- 老房改造測(cè)試題及答案
- 湖南省長(zhǎng)沙市岳麓實(shí)驗(yàn)中學(xué)2024-2025學(xué)年高一下學(xué)期6月月考數(shù)學(xué)試卷
- 2025屆上海市風(fēng)華中學(xué)高二化學(xué)第二學(xué)期期末學(xué)業(yè)質(zhì)量監(jiān)測(cè)試題含解析
- 數(shù)據(jù)分析基礎(chǔ)(第2版)課件 第4.2 描述性統(tǒng)計(jì)
- 富海(菏澤)新能源發(fā)展有限公司富海單縣200MW集中式風(fēng)電項(xiàng)目配套220kV升壓站項(xiàng)目報(bào)告表
- GMI傳感器在無(wú)損檢測(cè)技術(shù)中的應(yīng)用研究進(jìn)展
- 2025煤礦井巷工程建設(shè)項(xiàng)目合同
- 《中華民族共同體概論》第十四講新中國(guó)與中華民族新紀(jì)元課件
- 農(nóng)村傳統(tǒng)村落保護(hù)與活化利用策略
- 管道施工安全管理培訓(xùn)課件
- LY/T 2071-2024人造板類產(chǎn)品生產(chǎn)綜合能耗
- 2024年反洗錢(qián)考試題庫(kù)及答案
- 固定矯治器護(hù)理查房
- 2024版標(biāo)本采集課件
- Unit 4 Friends forever Understanding ideas 課件高中英語(yǔ)外研版(2019)必修第一冊(cè)-2
評(píng)論
0/150
提交評(píng)論