I have such task:
Overload operations <(compare two lists by sum of elements), + = (add an element to the end of the list), sort an array of instances of a class of lists in descending order using the sort sorting algorithm, add to the list with the largest sum list items with the smallest sum.
I'm new to OOP (only recently started this at the university). But at the same time, I basically succeeded in overload the >
operator, but I’ve been trying with +=
for 3 days already, but nothing works.
There is my code:
#include <iostream>
using namespace std;
class Array {
private:
int* a; // указатель на массив
unsigned int size; // размер массива
int k, n, sum;
public:
Array(); // конструктор по умолчанию
Array(int s); // конструктор с аргументом
~Array(); // деструктор
void delet(); // удаление элемента за номером
void add(); // вставка элемента
void sort(); // сортировка массивов
void Sum(); // находим сумму элементов массива
friend istream& operator >> (istream& in, const Array& arr) {
for (size_t i = 0; i != arr.size; i++) {
in >> arr.a[i];
}
return in;
};
friend ostream& operator << (ostream& out, const Array& arr) {
for (size_t i = 0; i != arr.size; i++) {
out << arr.a[i] << " ";
}
out << "\nSum = " << arr.sum;
return out;
};
bool operator < (Array& o1) {
this->Sum();
o1.Sum();
return this->sum < o1.sum;
};
};
Array::Array() {
size = 0;
a = new int[size];
for (size_t i = 0; i != size; i++) {
a[i] = 0;
}
}
Array::Array(int s) {
if (s > 0) {
size = s;
a = new int[size];
for (size_t i = 0; i != size; i++) {
a[i] = 0;
}
}
}
Array::~Array() {
delete[]a;
}
void Array::delet() {
cin >> k;
for (int i = 0; i < size - 1; i++)
if (i >= k - 1)
{
a[i] = a[i + 1];
}
else;
cout << "New massive: ";
for (int i = 0; i < size - 1; i++)
{
cout << a[i] << " ";
}
cout << endl;
};
void Array::add()
{
cin >> k;
cout << "Enter nmber of item after which you wanna put the number: ";
cin >> n;
for (int i = size - 1; i > n; i--)
a[i] = a[i - 1];
a[n] = k;
for (int i = 0; i < size; i++)
cout << a[i] << " ";
}
void Array::sort() {
int temp;
for (int i = 0; i < size - 1; i++) {
for (int j = 0; j < size - i - 1; j++) {
if (a[j] > a[j + 1]) {
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
for (int i = 0; i < size; i++) {
cout << a[i] << " ";
}
}
void Array::Sum() {
sum = 0;
for (int i = 0; i < size; i++)
sum += a[i];
};
int main() {
int size1, size2;
cout << "Enter count of numbers of 1 massive: ";
cin >> size1;
cout << "Enter count of numbers of 2 massive: ";
cin >> size2;
Array arr1(size1);
Array arr2(size2);
cout << endl;
cout << "Enter 1 massive: ";
cin >> arr1;
cout << "Enter 2 massive: ";
cin >> arr2;
cout << "------------------------\n";
cout << "Massive 1: ";
cout << arr1;
cout << endl;
cout << "Massive 2: ";
cout << arr2;
cout << endl;
cout << "------------------------\n";
cout << "Enter the number of item of 1 massive that nedeed to delete: ";
arr1.delet();
cout << "Enter the number of item of 2 massive that nedeed to delete: ";
arr2.delet();
cout << "------------------------\n";
cout << "Enter the item that nedeed to add: ";
arr1.add();
cout << endl;
cout << "Enter the item that nedeed to add: ";
arr2.add();
cout << endl;
cout << "------------------------\n";
cout << "Sorted 1 massive: ";
arr1.sort();
cout << endl;
cout << "Sorted 2 massive: ";
arr2.sort();
cout << endl;
cout << "------------------------\n";
return 0;
}