Skip to content

Instantly share code, notes, and snippets.

@vndmtrx
Last active December 14, 2015 14:58
Show Gist options
  • Select an option

  • Save vndmtrx/5103999 to your computer and use it in GitHub Desktop.

Select an option

Save vndmtrx/5103999 to your computer and use it in GitHub Desktop.
#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