#include #include using namespace std; class MyString{ private: char * TheString ;// data int AllocatedSpace ;// data public: MyString() ;// constructor MyString(char*) ; // constructor ~MyString() ;// destructor void append(char*) ; // operation int capacity( ) ; // operation void clear ( ) ; // operation int length( ) ;// operation void replace(char, char) ;// operation void push_back(char) ;// operation string cpp_string() ; // operation } ; // don't forget the semicolon MyString::MyString(){ // constructor this->AllocatedSpace = 100 ; // default is 100 characters this->TheString = new char[this->AllocatedSpace] ; cout << "new" << endl ; this->TheString[0] = '\0' ; } // how to use: MyString * name = new MyString() ; MyString::MyString(char * NewString){ // constructor this->AllocatedSpace = strlen(NewString) * 2 ; // double space this->TheString = new char[this->AllocatedSpace] ; cout << "new" << endl ; strncpy(this->TheString, NewString, strlen(NewString)) ; } // how to use: MyString * name = new MyString(.Justin Miller.) ; MyString::~MyString(){ // destructor this->AllocatedSpace = 0 ; delete [] this->TheString ; cout << "delete" << endl ; }// how to use: delete name ; void MyString::append(char * str) { int newsize = strlen(this->TheString) + strlen(str) ; if(newsize > AllocatedSpace) { // double Allocated Space and recopy string AllocatedSpace *= 2 ; char * newString = new char[AllocatedSpace] ; strncpy(newString, this->TheString, AllocatedSpace) ; } strncat(this->TheString, str, AllocatedSpace - strlen(this->TheString)) ; } int MyString::capacity( ) { return this->AllocatedSpace ; } void MyString::clear ( ) { this->TheString[0] = '\0' ; } int MyString::length( ) { return strlen(this->TheString) ; } void MyString::replace(char oldchar, char newchar) { for(int i = 0 ; i < strlen(this->TheString) ; i++) { if(this->TheString[i] == oldchar) { this->TheString[i] = newchar ; } } } void MyString::push_back(char newchar){ int newsize = strlen(this->TheString) + 1 ; if(newsize > AllocatedSpace) { // double Allocated Space and recopy string AllocatedSpace *= 2 ; char * newString = new char[AllocatedSpace] ; strncpy(newString, this->TheString, AllocatedSpace) ; } int end = strlen(this->TheString) ; this->TheString[end] = newchar ; this->TheString[end+1] = '\0' ; } string MyString::cpp_string() { string temp = this->TheString ; return temp ; } int main(int argc, char * argv[]) { MyString temp("Justin Miller") ; MyString * temp2 = new MyString(); delete temp2 ; /* cout << temp.cpp_string() << endl ; temp.replace('i', 'I') ; cout << temp.cpp_string() << endl ; temp.append(" says hi") ; cout << temp.cpp_string() << endl ; temp.push_back('!') ; cout << temp.cpp_string() << endl ; cout << temp.length() << endl ; cout << temp.capacity() << endl ; temp.clear() ; cout << temp.cpp_string() << endl ; */ return EXIT_SUCCESS ; }