Skip to content

Commit

Permalink
Create String.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
Huixxi authored Apr 24, 2020
1 parent 569b6dc commit cfaa14f
Showing 1 changed file with 43 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
class String {
char* str;

public:
String() : str(new char[1]) { *str = '\0'; }

String(String&& movestr) : str(movestr.str) {
movestr.str = nullptr;
}

String(const char* initstr) {
if(initstr != nullptr) {
str = new char[strlen(initstr) + 1];
strcpy(str, initstr);
}
}

String(const String& Copystr) { // Copy Constructor
if(Copytstr.str != nullptr) {
str = new char[strlen(Copystr.str) + 1];
strcpy(str, Copystr.str);
}
else
str = nullptr;
}

String& operator= (const String& Copystr) {
if(this != &Copystr) {
swap(Copystr);
}
return *this;
}

const char* c_str() const {
return str;
}

void swap(String& rhs) {
std::swap(str, rhs.str);
}

virutal ~String() { delete[] str}
}

0 comments on commit cfaa14f

Please sign in to comment.