Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
MyStringv2
//Title of this code #include <iostream> #include <cstring> #include <cstdlib> using namespace std; class MyString { char* str; public: MyString(const char*); MyString(const MyString&); ~MyString(); MyString& operator+=(const MyString&); MyString& operator+=(const char*); friend ostream& operator<<(ostream&, const MyString&); }; MyString::MyString(const char* s) { this->str = static_cast<char*>(malloc(sizeof(char) * (strlen(s) + 1))); strcpy(this->str, s); } MyString::MyString(const MyString& s) { this->str = static_cast<char*>(malloc(sizeof(char) * (strlen(s.str) + 1))); strcpy(this->str, s.str); } MyString::~MyString() { free(str); } MyString& MyString::operator+=(const MyString& s) { *this += s.str; return *this; } MyString& MyString::operator+=(const char* s) { int newSize = strlen(this->str) + strlen(s) + 1; this->str = static_cast<char*>(realloc(this->str, newSize * sizeof(char))); strcpy(this->str + (strlen(str)), s); return *this; } MyString operator+(const MyString& s1, const MyString& s2) { MyString s = s1; return s += s2; } MyString operator+(const char* s1, const MyString& s2) { MyString s(s1); return s += s2; } MyString operator+(const MyString& s1, const char* s2) { MyString s(s1); return s += s2; } ostream& operator<<(ostream &output, const MyString &s) { output << s.str; return output; } int main() { MyString s1("abc"); MyString s2("def"); MyString s3 = s1 + s2 + s1 +"s"; cout << s1 << " + " << s2 << " = " << s3; }
run
|
edit
|
history
|
help
0
5345
Rezolvare
codechef
new delete malloc free
integerDivision
ContainerVector2
kadane's algorithm 2
articulation points and bridges
HeapSort
Dar