您好,登錄后才能下訂單哦!
1、array類
(1)、代碼如下:
#include<iostream> using 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]; } //有指針,的進行深拷貝; 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<<array.getData(i)<<" "; } cout<<endl; Array array1 = array; for(i = 0; i < array1.length(); i++){ cout<<array1.getData(i)<<" "; } cout<<endl; return 0; }
(2)、運行結果:
2、string類
(1)、代碼如下:
#include<iostream> #include<stdio.h> #include<string.h> 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){ //默認參數看我們是否自己開辟大小的空間; 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{ //判斷與字符串是否相等,看長度和里面的內容是否相等!!! 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<<s1.m_p; return out; } istream& operator>>(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]); //測試[]改變值了嗎? cout<<s4<<endl; if(s2 == "s2"){ cout<<"相等"<<endl; }else{ cout<<"不相等"<<endl; } s3 = "aaa"; int flag = (s3 < "bbb"); if(flag < 0){ cout<<"s3小于bbb"<<endl; }else{ cout<<"s3大于bbb"<<endl; } s3 = "adasf"; strcpy(s3.c_str(), "sga"); cout<<s3<<endl; MyString s9(100);//默認輸入要開辟字符串的空間大小; cout<<"請輸入一個數字 :"; cin>>s9; cout<<s9<<endl; return 0; }
(2)、運行結果:
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。