Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
MyString
//Title of this code #include <iostream> #include <cstring> #include <cstdlib> using namespace std; class MyString { char* str; public: MyString(const char*); ~MyString(); MyString operator+(MyString&); friend MyString operator+(const char*, const MyString&); MyString operator+(const char*); MyString operator+(const char); friend ostream& operator<<(ostream&, const MyString&); }; MyString::MyString(const char* s) { str = static_cast<char*>(malloc(sizeof(char) * (strlen(s) + 1))); strcpy(str, s); } MyString::~MyString() { free(str); } MyString MyString::operator+(MyString& s) { return *this + s.str; } MyString operator+(const char* s, const MyString& l) { MyString ms = MyString(s); MyString ms2 = ms;// + l; return ms2; } MyString MyString::operator+(const char* s) { char* newStr; int newSize = strlen(str) + strlen(s) + 1; newStr = static_cast<char*>(malloc(newSize * sizeof(char))); strcpy(newStr, str); strcpy(newStr + (strlen(str)), s); MyString ms = newStr; free(newStr); return ms; } MyString MyString::operator+(const char c) { char* newStr; newStr = static_cast<char*>(malloc((strlen(str) + 1) * sizeof(char))); strcpy(newStr, str); newStr[strlen(str)] = c; newStr[strlen(str)+1] = '\0'; MyString ms = newStr; free(newStr); return ms; } ostream& operator<<(ostream &output, const MyString &s) { output << s.str; return output; } int main() { MyString s1("abc"); MyString s2("def"); MyString s3 = s1+s2; cout << s1 << " + " << s2 << " = " << "fff"+s1; }
run
|
edit
|
history
|
help
0
Wave Sort
max subsequence of array
Stock buy/sell, maximum subarray problem
Test1
Peg Grammar Parser Grasshopper Language
barai_1
Classes Pt 2 c++
Policy based smart pointer
ncr
Mine