array和string的运算符重载-创新互联
1、array类
成都创新互联公司是一家专注于成都网站制作、做网站与策划设计,江州网站建设哪家好?成都创新互联公司做网站,专注于网站建设十年,网设计领域的专业建站公司;建站业务涵盖:江州等地区。江州做网站价格咨询:18982081108(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)、运行结果:
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
分享题目:array和string的运算符重载-创新互联
浏览路径:http://azwzsj.com/article/dhgjho.html