国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

文章標題

2019-11-08 20:03:52
字體:
來源:轉載
供稿:網友

MyString.h

PRagma once

include

include

class CMyString { public: // CMyString(void); ~CMyString(void);

CMyString(const char* str = NULL); CMyString(const CMyString & another); CMyString & Operator = (const CMyString & another); CMyString operator + (const CMyString & another); bool operator > (const CMyString & another); bool operator < (const CMyString & another); bool operator == (const CMyString & another); char& operator[](int idx); void dis();

private: char* _str; }; MyString.cpp

include “stdafx.h”

include “MyString.h”

// 默認構造器 CMyString::CMyString(const char* str) { if (NULL == str) { _str = new char[1]; *_str = ‘/0’; } else { int len = strlen(str); _str = new char[len + 1]; strcpy_s(_str, len + 1, str); } } // 拷貝構造器 CMyString::CMyString(const CMyString & another) { int len = strlen(another._str); _str = new char[len + 1]; strcpy_s(_str, len + 1, another._str); } // 析構函數 CMyString::~CMyString() { delete []_str; } // 賦值運算符重載 CMyString & CMyString::operator = (const CMyString & another) { // 自賦值,出現錯誤 if (this == &another) { return *this; } // 先刪除自己開辟的空間 delete []_str; int len = strlen(another._str); this->_str = new char[len + 1]; strcpy_s(this->_str, len + 1, another._str); return *this; } // 加法運算符重載 CMyString CMyString::operator + (const CMyString & another) { int len = strlen(this->_str) + strlen(another._str); CMyString str; delete []str._str; str._str = new char[len + 1]; memset(str._str,0,len + 1); int len1 = strlen(this->_str) + 1; strcat_s(str._str, len1, this->_str); // 源串長度 + 目標串長度 + 結束符 int len2 = strlen(this->_str) + strlen(another._str) + 1; strcat_s(str._str,len2, another._str); return str; } // ==關系運算符重載 bool CMyString::operator==(const CMyString &other) { if(strcmp(this->_str,other._str) == 0) return true; else return false; } // >關系運算符重載 bool CMyString::operator>(const CMyString &other) { if(strcmp(this->_str,other._str) > 0) return true; else return false; } // <運算符重載 bool CMyString::operator<(const CMyString &other) { if(strcmp(this->_str,other._str) < 0) return true; else return false; } // []運算符重載 char& CMyString::operator[](int idx) { return _str[idx]; } // 打印函數 void CMyString::dis() { using namespace std; for (size_t i = 0; i < strlen(this->_str); i++) { cout << _str[i]; } cout << endl; }


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 平山县| 贡嘎县| 株洲市| 衢州市| 措勤县| 长垣县| 兴和县| 平利县| 武汉市| 简阳市| 宽甸| 县级市| 高唐县| 白银市| 尚义县| 万州区| 淮南市| 桑日县| 上杭县| 桐乡市| 新建县| 曲阳县| 鹤峰县| 威海市| 二连浩特市| 化德县| 台山市| 宜丰县| 绥滨县| 长宁县| 莲花县| 长沙市| 汝州市| 镇雄县| 阜城县| 西畴县| 新乡县| 尉犁县| 茶陵县| 体育| 衡山县|