1、array类
蕉城网站制作公司哪家好,找创新互联!从网页设计、网站建设、微信开发、APP开发、响应式网站设计等网站项目制作,到程序开发,运营维护。创新互联成立与2013年到现在10年的时间,我们拥有了丰富的建站经验和运维经验,来保证我们的工作的顺利进行。专注于网站建设就选创新互联。
(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]; } //有指针,的进行深拷贝; 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)、运行结果:
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){ //默认参数看我们是否自己开辟大小的空间; 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< >(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< >s9; cout< (2)、运行结果:
网站名称:array和string的运算符重载
文章URL:http://jkwzsj.com/article/igjiho.html