Last active
December 14, 2015 14:58
-
-
Save vndmtrx/5103999 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream.h> | |
| #include <string.h> | |
| class string | |
| { | |
| private: | |
| int size; | |
| char *ptr; | |
| string() : size(0), ptr(new char[1]) { ptr[0] = 0; } | |
| string(const string &s) : size(s.size) | |
| { | |
| ptr = new char[size + 1]; | |
| strcpy(ptr, s.ptr); | |
| } | |
| ~string() | |
| { | |
| delete [] ptr; | |
| } | |
| friend ostream &operator <<(ostream &, const string &); | |
| string &operator=(const char *); | |
| }; | |
| ostream &operator<<(ostream &stream, const string &s) | |
| { | |
| return(stream << s.ptr); | |
| } | |
| string &string::operator=(const char *chrs) | |
| { | |
| if (this != &chrs) | |
| { | |
| delete [] ptr; | |
| size = strlen(chrs); | |
| ptr = new char[size + 1]; | |
| strcpy(ptr, chrs); | |
| } | |
| return(*this); | |
| } | |
| int main() | |
| { | |
| string str; | |
| str = "Hello, world."; | |
| cout << str << endl; | |
| return(0); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment