1、array類
蕉城網(wǎng)站制作公司哪家好,找創(chuàng)新互聯(lián)!從網(wǎng)頁(yè)設(shè)計(jì)、網(wǎng)站建設(shè)、微信開發(fā)、APP開發(fā)、響應(yīng)式網(wǎng)站設(shè)計(jì)等網(wǎng)站項(xiàng)目制作,到程序開發(fā),運(yùn)營(yíng)維護(hù)。創(chuàng)新互聯(lián)成立與2013年到現(xiàn)在10年的時(shí)間,我們擁有了豐富的建站經(jīng)驗(yàn)和運(yùn)維經(jīng)驗(yàn),來保證我們的工作的順利進(jìn)行。專注于網(wǎng)站建設(shè)就選創(chuàng)新互聯(lián)。
(1)、代碼如下:
#includeusing namespace std; class Array{ public: Array(int count); Array(const Array &t); ~Array(); public: void setData(int i, int data); int getData(int i); int length(); private: int len; int *p; }; Array::Array(int count){ len = count; p = new int[len]; } //有指針,的進(jìn)行深拷貝; Array::Array(const Array &t){ len = t.len; p = new int[len]; for(int i = 0; i < t.len; i++){ p[i] = t.p[i]; } } Array::~Array(){ if(p){ delete []p; p = NULL; } } void Array::setData(int i, int data){ p[i] = data; } int Array::getData(int i){ return p[i]; } int Array::length(){ return len; } int main(void){ Array array(10); int i; for(i = 0; i < array.length(); i++){ array.setData(i, i); } for(i = 0; i < array.length(); i++){ cout< (2)、運(yùn)行結(jié)果:
2、string類
(1)、代碼如下:
#include#include #include using namespace std; class MyString{ public: friend ostream& operator<<(ostream &out, const MyString &s1); friend istream& operator>>(istream &in, MyString &s2); MyString(int len = 0){ //默認(rèn)參數(shù)看我們是否自己開辟大小的空間; if(len != 0){ m_len = len; m_p = new char[m_len+1]; memset(m_p, 0, m_len); }else{ m_len = 0; m_p = new char[m_len+1]; strcpy(m_p, ""); } } MyString(const char *p){ if(p == NULL){ m_len = 0; m_p = new char[m_len+1]; strcpy(m_p, ""); }else{ m_len = strlen(p); m_p = new char[m_len+1]; strcpy(m_p, p); } } MyString(const MyString &s){ m_len = s.m_len; m_p = new char[m_len+1]; strcpy(m_p, s.m_p); } MyString& operator=(const MyString &t){ if(m_p){ delete []m_p; m_p = NULL; m_len = 0; } m_len = t.m_len; m_p = new char[m_len+1]; strcpy(m_p, t.m_p); return *this; } ~MyString(){ if(m_p) { delete []m_p; m_p = NULL; m_len = 0; } } public: MyString operator=(const char *p){ if(m_p){ delete []m_p; m_p = NULL; m_len = 0; } if(p == NULL){ m_len = 0; m_p = new char[m_len+1]; strcpy(m_p, ""); }else{ m_len = strlen(p); m_p = new char[m_len+1]; strcpy(m_p, p); } return *this; } char& operator[](int index){ return m_p[index]; } bool operator==(const char *p)const{ //判斷與字符串是否相等,看長(zhǎng)度和里面的內(nèi)容是否相等!!! if(p == NULL){ if(m_len == 0){ return true; }else{ return false; } }else{ if(m_len == strlen(p)){ return !strcmp(m_p, p); }else{ return false; } } } bool operator==(const MyString &s)const{ if(m_len != s.m_len){ return false; } return !strcmp(m_p, s.m_p); } bool operator!=(const char *p)const{ return !(*this == p); } bool operator!=(const MyString &s)const{ return !(*this == s); } int operator<(const char *p)const{ return strcmp(m_p, p); } int operator<(const MyString &s)const{ return strcmp(m_p, s.m_p); } int operator>(const char *p)const{ return strcmp(p, m_p); } int operator>(const MyString &s)const{ return strcmp(s.m_p, m_p); } //怎么樣把類的指針露出來. public: char *c_str(){ return m_p; } const char *c_str2(){ return m_p; } int length(){ return m_len; } private: int m_len; char *m_p; }; ostream& operator<<(ostream &out, const MyString &s1){ out< >(istream &in, MyString &s2){ in>>s2.m_p; return in; } int main(void){ MyString s1; MyString s2("s2"); MyString s3 = s2; MyString s4 = "s444444444444"; s4 = "s22222222222"; s4 = s2; s4[1] = '3'; printf("%c\n", s4[1]); //測(cè)試[]改變值了嗎? cout< >s9; cout< (2)、運(yùn)行結(jié)果:
當(dāng)前文章:array和string的運(yùn)算符重載
當(dāng)前鏈接:http://weahome.cn/article/igjiho.html