0

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;
}
Sollpix
  • 49
  • 5
  • Actually, I tried make some thing like this: `Array& operator += (int el) { this->a = (int*)realloc(this->a, sizeof(int) * (this->size2)); a[size1] += el; size1++; this->b = (int*)realloc(this->b, sizeof(int) * (this->size1)); b[size2] += el; size2++; }` –  Sollpix Apr 09 '22 at 16:20
  • And after that put use this in some function, like this: `void Array::addmassive() { if (sum1 > sum2) { cout << "\n"; //... cout << "Add 2 massive to 1 massive: "; } else { cout << "\n"; //... cout << "Add 1 massive to 2 massive: "; } }` –  Sollpix Apr 09 '22 at 16:21
  • Put your attempt *in the question*. But you can't mix `new`/`new[]`/`delete` stuff with `malloc`/`realloc`/`free`. – Adrian Mole Apr 09 '22 at 16:21
  • Ok, maybe you can suggest me what and how I need to do? –  Sollpix Apr 09 '22 at 16:26
  • can you explain why your Array contains 2 arrays?, I cannot read the russian comments – pm100 Apr 09 '22 at 16:41
  • @pm100, cause I think that it`s right, but judging by your reaction, I see that it is not so. Well, then how do you propose to use 1 array in the class in order to further enter 2 arrays from the keyboard and l = continue to work with them? –  Sollpix Apr 09 '22 at 16:45
  • well I would have 2 instances of Array. The name 'Array' means it holds one array, also what would you do if you needed 10 arrays? Would you edit this class to store 10 arrays? No. Have `Array arr1;` and `Array arr2;` – pm100 Apr 09 '22 at 16:48
  • You mean do as I did in main() `Array arr(size1, size2);` but for 1 and then for 2 arrays, right? –  Sollpix Apr 09 '22 at 17:00
  • [What are the basic rules and idioms for operator overloading?](https://stackoverflow.com/questions/4421706/) – Remy Lebeau Apr 10 '22 at 00:06
  • No just have an Array class thats one array. THen do `Array arr1(size1); Array arr2(size2);` – pm100 Apr 10 '22 at 00:07

1 Answers1

-1

You can add an element at the of array instances in your class using overloaded operator+= like this.

Array& operator += (int el)
{
    size1++; // increase size of 'a' array
    a = (int*)realloc(a, sizeof(int) * size1);
    a[size1 - 1] = el;
    
    size2++; // increase size of 'b' array
    b = (int*)realloc(b, sizeof(int) * size2);
    b[size2 - 1] = el;
    
    return *this;
}

It's also better to decompose your "Array" object to real dynamic array that will store size and pointer to the beginning of the elements like std::vector container(or you can just use them, if you can).

nexan_pro
  • 87
  • 1
  • 7
  • the array was created using new , there is no way it should being realloced – pm100 Apr 10 '22 at 00:29
  • 1
    Why? you can write your own realloc, which will use delete[] and new operators. – nexan_pro Apr 10 '22 at 08:15
  • sure you can , but I doubt that somebody who writes an `operator<` that returns an Array (that contains 2 arrays because he needs 2 arrays) it just constructed is going to do that – pm100 Apr 10 '22 at 16:42
  • And again I welcome the main experts of this forum)) I decided to redo the program a little according to your recommendations. Now 'Array' has a pointer to only 1 array, and for subsequent operations I created two objects: 'Array arr1(size1);' and 'Array arr2(size2);'. Made input and output data by overloading >> and <<. But there were problems this time with the "<" operator. The overload seems to have done, but the sum of the elements for each array is not calculated, for some reason (( –  Sollpix Apr 11 '22 at 17:23
  • @Sollpix, before printing sum of elements in overloaded output operator<< you need to calculate it. I see you already wrote a method to do it - Sum(), you can call it in output operator<< before printing array sum simply like this. `friend ostream& operator << (ostream& out, Array& arr) { for (size_t i = 0; i != arr.size; i++) { out << arr.a[i] << " "; } arr.Sum(); out << "\nSum = " << arr.sum; return out; }` also it's not necessity to print semicolon at the end of function body. – nexan_pro Apr 16 '22 at 14:53