Код: Выделить всё
String String::operator+(String& other) {
int newLength = getTrueSize() + other.getTrueSize() + 1;
char* newStr = new char[newLength];
copy(str, str + maxLength, newStr);
copy(other.str, other.str + other.maxLength, newStr + getTrueSize());
return String(newStr, newLength);
}
Код: Выделить всё
String BigInt::operator+(String& other) {
BigInt B2(other);
BigInt result;
long long res1 = arrayToNumber(digits, size);
if (negative) {
res1 = res1 - (res1 + res1);
}
long long res2 = arrayToNumber(B2.digits, B2.size);
if (B2.negative) {
res2 = res2 - (res2 + res2);
}
long long resultNumber = res1 + res2;
if (resultNumber < 0) {
result.negative = true;
resultNumber = abs(resultNumber);
}
int* resultDigits;
int resultSize;
numberToArray(resultNumber, resultDigits, resultSize);
result.size = resultSize;
result.digits = resultDigits;
char* resultChars = new char[resultSize + 1];
for (int i = 0; i < resultSize; ++i) {
resultChars[i] = '0' + resultDigits[i];
}
resultChars[resultSize] = '\0';
String s;
s.str = resultChars;
s.maxLength = resultSize;
return s;
}
Код: Выделить всё
String& String::operator=(String&& other) {
if (this != &other) {
delete[] str;
maxLength = other.maxLength;
str = other.str;
other.str = nullptr;
other.maxLength = 0;
}
return *this;
}
Код: Выделить всё
#include "BigInt.h"
#include "String.h"
#include
#include
using namespace std;
int main() {
BigInt B1;
String B2, BB;
cout B1;
cout B2;
cout
Подробнее здесь: [url]https://stackoverflow.com/questions/78356126/how-to-build-a-polymorphic-cluster-based-on-a-virtual-one-within-the-class-hiera[/url]