詳解如何利用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

提交評論