




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
第詳解如何利用C++實(shí)現(xiàn)Mystring類目錄功能實(shí)現(xiàn)一:基本功能(實(shí)現(xiàn)源碼)二:拓展功能完整版源碼三:細(xì)節(jié)部分修改1.使用指針實(shí)例化對象部分2.重載=運(yùn)算符函數(shù)的過程
功能實(shí)現(xiàn)
基本功能
1實(shí)現(xiàn)頭文件的封裝:MyString.h
2缺省構(gòu)造函數(shù)對字符串的初始化(MyString())
3使用構(gòu)造函數(shù)初始化字符串的另外兩種方式*2(動(dòng)態(tài)指針+拷貝構(gòu)造函數(shù))
4析構(gòu)函數(shù)(釋放動(dòng)態(tài)申請的字符串空間)
5重載輸出運(yùn)算符()
6重載賦值運(yùn)算符*2(=)
7重載下標(biāo)運(yùn)算符([],索引輸出)
拓展功能
1字符串長度的比較
2字符串的排序功能
3字符串的倒置
4字符串中指定兩個(gè)字符的交換
5查找某字符串是否位于指定的字符串中(采用暴力查找)
細(xì)節(jié)修改
1使用自定義函數(shù)來替換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)用字符串首指針返回
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)指針來構(gòu)造相應(yīng)的字符串
if(P)
len=strlen(P);//取長度
str=newchar[len+1];//開空間
strcpy(str,P);//復(fù)制值
else
MyString();//如果傳入的字符串為空,直接調(diào)用缺省值構(gòu)造函數(shù)
MyString::MyString(constMyStringAnotherMyString)//拷貝構(gòu)造函數(shù),這里的形參使用了const,該形參類中的所有函數(shù)都要使用const來修飾
len=AnotherMyString.length();
str=newchar[len+1];
strcpy(str,AnotherMyString.str);
intMyString::length()const//求長度成員函數(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語句
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)//如果索引越界,輸出最后一個(gè)字符
cout"Warning!!!"endl;
cout"Outofboundary!Thelastcharis:";
returnstr[len-1];
else
returnstr[index-1];
//TODO:在此處插入return語句
MyString::~MyString()//釋放數(shù)組空間
delete[]str;
len=0;
ostreamoperator(ostreamoutput,constMyStringstr)//重載輸出運(yùn)算符
outputstr.getString();
returnoutput;
//TODO:在此處插入return語句
這里需要提到的一點(diǎn)是析構(gòu)函數(shù)中的delete[]str;
delete[]與delete的區(qū)別
使用new得來的空間使用delete釋放;使用new[]得來的空間使用delete[]釋放;這是永遠(yuǎn)不會(huì)錯(cuò)的。
但是更加深入一點(diǎn)去理解:
使用new[]得到的空間如果動(dòng)態(tài)申請的數(shù)據(jù)類型時(shí)基本數(shù)據(jù)類型也可以使用delete直接釋放,但是如果使用new[]申請的數(shù)據(jù)的類型時(shí)自定義類型(例如類名),這就必須使用delete[]來進(jìn)行釋放,只有這樣才能夠調(diào)用自定義類型的析構(gòu)函數(shù)進(jìn)行對自定義類型進(jìn)行釋放。
除此之外,再提一點(diǎn)關(guān)于delete[]的注意事項(xiàng):
當(dāng)使用new[]動(dòng)態(tài)生成內(nèi)存的時(shí)候,刪除的時(shí)候必須將刪除的指針指向new[]出來的內(nèi)存的首地址:
#includeiostream
usingnamespacestd;
intmain()
int*p=newint[3];
*p=1;
p++;
*p=2;
delete[]p;
cout"*"endl;
return0;
這一段小程序中:
因?yàn)閜指針不是指向了首地址,所以程序雖然沒報(bào)錯(cuò),但是無法正常運(yùn)行!我們可以將申請的首地址保存起來,供刪除的時(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)用=(對象)實(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;
二:拓展功能
字符串長度的比較
使用//=/=等符號進(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ù)對類中的私有字符串進(jìn)行從小到大的排序:
A.Sort_String_LB();A.Sort_String_BL();
字符串的倒置
使用類中的成員函數(shù)對類中的私有字符串進(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)用字符串首指針返回,返回的指針可以直接使用cout輸出
constchar*getString()const;
//求字符串的長度(直接返回類中的私有成員len的值)
//const函數(shù)不能修改其數(shù)據(jù)成員,僅僅起到輸出數(shù)據(jù)的作用
intlength()const;
intsize()const;
//重載賦值運(yùn)算符,使得可以使用對象與"xxxxxx"來賦值
MyStringoperator=(constMyString
MyStringoperator=(constchar*);
//重載下標(biāo)運(yùn)算符
charoperator[](constintindex);
//重載輸出運(yùn)算符
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);
//字符串內(nèi)部內(nèi)容的冒泡排序(ASCII碼),Little-Big
voidSort_String_LB();
voidSort_String_BL();
//對字符串進(jìn)行倒置
voidReverse();
//交換字符串中兩個(gè)字符的位置
voidChangeTwoCharPosition(intfirstposition,intsecondposition);
//查詢某字符串是否是指定字符串的子串(暴力模式)
boolFind(char*search_string);
private:
char*str;//指向數(shù)組首地址(此時(shí)為野指針)
intlen;//字符串的長度
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)指針來構(gòu)造相應(yīng)的字符串
if(P)
len=strlen(P);//取長度
str=newchar[len+1];//開空間
strcpy(str,P);//復(fù)制值
else
MyString();//如果傳入的字符串為空,直接調(diào)用缺省值構(gòu)造函數(shù)
MyString::MyString(constMyStringAnotherMyString)//拷貝構(gòu)造函數(shù),這里的形參使用了const,該形參類中的所有函數(shù)都要使用const來修飾
len=AnotherMyString.length();
str=newchar[len+1];
strcpy(str,AnotherMyString.str);
intMyString::length()const//求長度成員函數(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語句
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)//如果索引越界,輸出最后一個(gè)字符
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. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 沂水離婚協(xié)議書
- 煤氣月結(jié)協(xié)議書
- 產(chǎn)品研發(fā)及技術(shù)研發(fā)合作協(xié)議
- 專業(yè)市場招商合作協(xié)議合同書
- 《營銷策略》課件
- 銷售代理業(yè)務(wù)委托協(xié)議書
- 消費(fèi)全返協(xié)議書
- 城市交通管理智能化系統(tǒng)開發(fā)合同
- 車位租賃押金合同協(xié)議
- 連鎖超市合作協(xié)議合同
- 統(tǒng)編版語文一年級下冊2024-2025學(xué)年度語文園地五(課件)
- 2024-2030年中國橡塑防滑墊市場競爭格局與前景發(fā)展策略分析報(bào)告
- 家具的類型(分類全面)-課件
- 重慶市巴蜀學(xué)校2024-2025學(xué)年九年級上學(xué)期12月月考語文試題
- 1、汽輪機(jī)冷端系統(tǒng)診斷和運(yùn)行優(yōu)化
- 《中國名牌大學(xué)簡介》課件
- 火災(zāi)安全培訓(xùn)總結(jié)
- 《城鎮(zhèn)液化石油氣加臭技術(shù)規(guī)程》
- 初二英語語法歸納仁愛版單選題100道及答案解析
- 酒店防洪防汛培訓(xùn)
- 中小學(xué)校財(cái)務(wù)制度知識培訓(xùn)
評論
0/150
提交評論