第四次作业。

This commit is contained in:
unlockable
2023-03-21 13:26:26 +08:00
parent 3bc65bb35a
commit c4c15ef343

View File

@@ -14,6 +14,7 @@ public:
bool operator>(String &otherStr);
bool operator<(String &otherStr);
String& operator=(String& otherStr);
String& operator+(String& otherStr);
char *getStrPtr();
void printOut();
@@ -60,11 +61,11 @@ bool String::operator<(String &otherString) {
return strcmp(this->str, otherString.getStrPtr()) < 0;
}
String operator+(String &lhs, String &rhs) {
String tmp(new char[strlen(lhs.getStrPtr()) + strlen(rhs.getStrPtr()) + 1]);
strcpy(tmp.getStrPtr(), lhs.getStrPtr());
strcat(tmp.getStrPtr(), rhs.getStrPtr());
return tmp;
String& String::operator+(String &otherString) {
String* tmp = new String(new char[strlen(this->getStrPtr()) + strlen(otherString.getStrPtr()) + 1]);
strcpy(tmp->getStrPtr(), this->getStrPtr());
strcat(tmp->getStrPtr(), otherString.getStrPtr());
return *tmp;
}
String& String::operator=(String &otherString) {
@@ -97,7 +98,7 @@ int main() {
string1.printOut();
string2.printOut();
string3.printOut();
string3 = string1;
string3 = string1 + string2;
string3.printOut();
(string1 + string2).printOut();
if (!(string1 > string2)) {