String類的代碼:
創新互聯建站專注于企業全網整合營銷推廣、網站重做改版、榮縣網站定制設計、自適應品牌網站建設、H5場景定制、成都做商城網站、集團公司官網建設、成都外貿網站建設公司、高端網站制作、響應式網頁設計等建站業務,價格優惠性價比高,為榮縣等各大城市提供網站開發制作服務。class String
{
public:
String(char* str="")
{
_str = new char[strlen(str) + 1];
strcpy(_str, str);
}
String(const String& str)
{
_str = new char[strlen(str._str) + 1];
strcpy(_str, str._str);
}
~String()
{
delete[] _str;
}
String& operator=(const String& str)
{
if (&str == this)
return *this;
delete[] _str;
_str = new char[strlen(str._str) + 1];
strcpy(_str, str._str);
return *this;
}
bool operator==(const String& str)
{
return strcmp(_str, str._str) == 0;
}
friend ostream& operator<<(ostream& o,String& str)
{
o << str._str;
return o;
}
private:
char* _str;
};包括構造函數,拷貝構造函數,析構函數,算數運算符重載。
下面是測試代碼:
String s1 = "hello"; String s2 = "world"; String s3 = "o"; cout << "s1=" << s1 << endl; cout << "s2=" << s2 << endl; cout << "s3=" << s3 << endl; cout << endl; String s4(s1); String s5(s1); String s6(s1); s5 = s2; cout << "s4=" << s4 << endl; cout << "s5=" << s5 << endl; cout << (s4 == s5) << endl; cout << (s4 == s6) << endl;
測試結果:

另外有需要云服務器可以了解下創新互聯scvps.cn,海內外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業上云的綜合解決方案,具有“安全穩定、簡單易用、服務可用性高、性價比高”等特點與優勢,專為企業上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。
分享題目:string類的實現(構造函數,析構函數,運算符重載)-創新互聯
網站地址:http://www.yijiale78.com/article14/ccdpde.html
成都網站建設公司_創新互聯,為您提供靜態網站、響應式網站、網站收錄、搜索引擎優化、網站改版、定制網站
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯