The problem is that the result after the operations A+B and AB not displayed on the screen. Code:

 #include <iostream> class multiplicity { private: int size; int* array; public: //стандартный конструктор multiplicity() { size = 15; array = new int[15]; for (int i = 0; i<15; ++i) { array[i] = rand() % 10; } }; //конструктор с опредленным размером множетсва multiplicity(const int inputSize) { array = (int*)calloc(inputSize, sizeof(int)); size = inputSize; }; //конструктор копий multiplicity(const multiplicity& CopyMult) { if (CopyMult.array != NULL) { array = new int[CopyMult.size]; for (int i = 0; i<size; ++i) { array[i] = CopyMult.array[i]; } } else array = NULL; } //конструктор повышающий производительность, он же конструктор перемещения multiplicity(multiplicity&& MoveMult) { if (MoveMult.array != NULL) { array = MoveMult.array; MoveMult.array = NULL; } } //деструктор ~multiplicity() { if (array != NULL) delete[] array; } void add() const { int inputInt; std::cin >> inputInt; for (int i = 0; i<size;++i) { if (array[i] == 0) { array[i] = inputInt; break; } } } void show() const { for (int i = 0; i<size; ++i) { if (array[i] != 0) std::cout << array[i] << " "; } std::cout << std::endl; }; friend const multiplicity operator+(const multiplicity& left, const multiplicity& right) { multiplicity newMult(left.size); for (int i = 0; i<left.size; ++i) { newMult.array[i] = left.array[i]; } int newsize = right.size; for (int i = 0; i < right.size; ++i) { if (newMult.array[i] != right.array[i]) { newMult.array = (int*)realloc(newMult.array, sizeof(int)); newsize++; newMult.array[newsize - 1] = right.array[i]; } } return newMult; } friend const multiplicity operator-(const multiplicity& left, const multiplicity& right) { int newsize = left.size; multiplicity newMult(left.size); for (int i = 0; i < left.size; ++i) { newMult.array[i] = left.array[i]; } for (int i = 0; i < right.size; ++i) { if (newMult.array[i] == right.array[i]) { for (int j = i - 1; j < newMult.size; ++j) { newMult.array[j] = newMult.array[j + 1]; } } } return newMult; } }; int main() { multiplicity A; multiplicity B; A.show(); B.show(); int k; std::cin >> k; if (k == 1) { multiplicity D = A + B; D.show(); } if (k == 2) { multiplicity C = A - B; C.show(); } return 0; } 
  • The problem is that the result after the operations A + B and AB is not displayed on the screen - DivinityToffee
  • Run under the debugger and see what's up. - αλεχολυτ
  • Compiled All output. There is a suspicion that k either do not enter, or it is read from the ceiling and is not equal to 1 or 2. - KoVadim

1 answer 1

I'm starting to swear ...

Do not mix in any way new and calloc/realloc !

The argument const int - const clearly redundant ...

In the copy constructor, who will size initialize? Dijkstra? And in moving - Wirth? :)

delete works quietly with zero, verification is not needed.

But in general - I never got the idea of ​​what is being done in the operators + and - - explain with normal words, please ...

  • still NULL instead of nullptr - strangeqargo