1. C++ 實現mystring類
阿新 • • 發佈:2019-01-12
mystring.h
#ifndef MYSTRING_H #define MYSTRING_H #include <iostream> #include <string.h> using namespace std; class mystring; class sender { public: ostream& operator<<(mystring const & s); }; class mystring { friend ostream& sender::operator<<(mystring const & s); friend ostream& operator <<(ostream & co,mystring const & s); friend istream& operator >>(istream & ci,mystring & s); public: #if 0 mystring(); mystring(char* p); #endif #if 1 mystring(char* p = nullptr);//預設引數只能放在宣告處,且這裡用於做標誌位。 #endif ~mystring();//析構 mystring(const mystring & another); mystring& operator=(const mystring &another); mystring operator+(const mystring &another); mystring& operator+=(const mystring &another); bool operator>=(mystring const &another); char& operator[](int const & idx); mystring& operator++(); const mystring operator++(int); char* c_str(); void printstr(); private: char* _str; }; ostream& operator <<(ostream & co,mystring const& s); istream& operator >>(istream & ci,mystring & s); #endif // MYSTRING_H
mystring.cpp
#include <iostream> #include "mystring.h" #include <string.h> using namespace std; ostream& sender::operator<<(mystring const & s) { cout<<s._str; return cout; } #if 0 mystring::mystring(char* p) { int len = strlen(p); this->_str = new char[len+1]; strcpy(this->_str,p); } mystring::mystring() { this->_str = new char[1]; *(this->_str) = '\0'; } #endif #if 1 mystring::mystring(char* p) { if(p) { int len = strlen(p); this->_str = new char[len+1]; strcpy(this->_str,p); } else { this->_str = new char[1]; *(this->_str) = '\0'; } } #endif mystring::mystring(const mystring & another) { int len = strlen(another._str);//同類之間無私處 this->_str = new char[len+1]; strcpy(this->_str, another._str); } mystring::~mystring() { cout<<"~mystring"<<endl; delete[] _str; } mystring& mystring::operator=(const mystring &another) { if(this != &another)//防止自賦值 { delete[]this->_str;//防止記憶體洩漏 int len = strlen(another._str); this->_str = new char[len+1]; strcpy(this->_str,another._str); } return *this; } mystring mystring::operator+(const mystring &another) { mystring sumstr; delete[]sumstr._str; sumstr._str = new char[strlen(this->_str)+strlen(another._str)+1]{0};//初始化為0不可少 strcpy(sumstr._str,this->_str); strcat(sumstr._str,another._str); return sumstr; } #if 0 mystring& mystring::operator+=(const mystring &another) { char *sumstr = new char[strlen(this->_str)+strlen(another._str)+1]; strcpy(sumstr,this->_str); strcat(sumstr,another._str); delete[]this->_str; this->_str = sumstr; return *this; } #endif #if 1 mystring& mystring::operator +=(const mystring &another) { int len = strlen(this->_str)+strlen(another._str); this->_str = (char*)(realloc(this->_str,len+1)); // this->_str = static_cast<char*>(realloc(this->_str,len+1)); memset(this->_str+strlen(this->_str),0,strlen(another._str)+1); strcat(this->_str,another._str); return *this; } #endif bool mystring::operator >=(mystring const &another) { return strcmp(this->_str,another._str)>=0; } char& mystring::operator[](int const & idx) { return this->_str[idx]; } mystring& mystring::operator++()//前++ { char* p = _str; while(*p) { (*p)++; p++; } return *this; } const mystring mystring::operator++(int)//後++ { mystring stemp(*this); char* p = _str; while(*p) { (*p)++; p++; } return stemp; } char* mystring::c_str() { return this->_str; } void mystring::printstr() { cout<<_str<<endl; } ostream& operator <<(ostream & co,mystring const& s) { co<<s._str; return co; } istream& operator >>(istream & ci,mystring & s) { ci>>s._str; return ci; }
main.c
#include <iostream> #include "mystring.h" #include <string> using namespace std; int main() { #if 0 //生成物件,需滿足的各種生成物件方式 //呼叫建構函式 mystring s1("china");//有參定義string物件 s1.printstr(); mystring ss11 = "china"; ss11.printstr(); mystring s2;//無參定義string物件的邏輯,其實本質是傳入了一個nullptr空指標 s2.printstr(); mystring s3("");//有參定義string物件,傳入空串,"",只包含一個'\0' s2.printstr(); mystring *sp4 = new mystring("china"); sp4->printstr(); delete sp4; mystring *sp5 = new mystring; sp5->printstr(); delete sp5; //呼叫拷貝構造 mystring s6 = s1; mystring s7(s1); mystring *sp8 = new mystring(s1); delete sp8; //呼叫了轉換構造 //= //防止重析構,防止記憶體洩漏,防止自賦值。 mystring s9; s9 = s1; s9 = s2 = s1; s9 = (s2 = s1); (s9 = s2) = s1; // s9 = s3; //+ mystring s10 = s1+s2; s10 = s1+s2; s10 = s1+s2+s3; s10 = (s1+s2)+s3; (s9 + s10) = s1; //+= mystring s11 = (s2 += s1); mystring s12(s2+=s1); mystring s13 = s1 += s2; s11 += s1; s11 += s1 += s2; //mystring s11; // s11 += s1 += s2; // s11.operator +=(s1.operator+=(s2)); //>= //[] mystring s14("china"); s14[2] = 't'; cout<<"s14:"; //s14.printstr(); //c_str //<< >> cout<<s14<<endl; mystring s15; // cin>>s15; cout<<"s15:"<<s15<<endl; //前++ 後++ mystring s16("aaaaa"); // cout<<"s16:"<<++s16<<endl; // cout<<"s16:"<<s16<<endl; // cout<<"s16:"<<++++s16<<endl; // cout<<"s16:"<<s16<<endl; cout<<"s16:"<<s16++<<endl; cout<<"s16:"<<s16<<endl; #endif //轉換建構函式 mystring s17("aaaaa"); s17 = "bbbbb"; // cout<<"s17::"<<s17<<endl; //sender<<s17 cout<<"s17::"; sender sd; sd<<s17<<endl<<"21312321"<<endl<<endl<<endl;//<<endl; sd<<"abcd"<<endl; return 0; } //explicit修飾符放在宣告還是定義處?