
作業社區
探索學習新天地,共享知識資源!
MyStudy_HYB 的學生作業:
#include #include #include #include using namespace std; class String { public: String(); String(const char* str = NULL); ~String(); const char operator[](int index) const; void show(void) const; private: char* str; int length; }; String::String() {} String::String(const char* str) { if (str == NULL) { this->str = NULL; } else if (this->str == str) { return; } else { char* p = (char*)str; while (*p++); int len = p - str; this->length = len; this->str = (char*)malloc(len * sizeof(char)); strcpy(this->str, str); } } String::~String() { if (this->str != NULL) { free(this->str); this->str = NULL; } } const char String::operator[](int index) const { if (index >= this->length) { cout length; i++) { if ((*this)[i] != '\0') cout




