0

Why pointer to pointer has been used rather than single pointer in the code? Also do you think the destructor was written wrong if it is how can i make it correct?

pointer to pointer: employee** _arr;

You can see the code below:

#include<iostream>


class employee {
private:
    std::string _name;
    std::string _surname;
    int _year;
    double _salary;
    static int numberOfEmployees;
public:

    employee() {
        _name = "not-set";
        _surname = "not-set";
        _year = 0;
        _salary = 0;

        numberOfEmployees++;
    }
    employee(int year, std::string name, std::string surname) {
        _name = name;
        _surname = surname;
        _year = year;

        numberOfEmployees++;
        calculateSalary();
    }
    void calculateSalary() {

        //salary = 2310 + 2310 * year * 12/100.0
        _salary = 2310 + (2310 * (double)_year) * (12 / 100.0);
    }
    void printInfo() {
        std::cout << _name << " " << _surname << " " << _year << " " << " " << _salary << " TL/month" << std::endl;
    }

    static int getEmployeeCount() {
        return numberOfEmployees;
    }
};

class employeeList {
private:
    int _size;
    int _lenght;
    employee** _arr;
public:
    employeeList() :_size(1), _lenght(0), _arr(NULL) {}
    employeeList(int size) :_size(size) {
        _arr = new employee * [_size];
        _lenght = 0;
    }
    int listLength() {
        return _lenght;
    }
    employee retrieve_employeeFromIndex(int index) {
        if (index >= 0 && index < _size) {
            return *_arr[index];
        }
    }
    void addToList(employee* item) {
        _lenght++;
        if (_lenght <= _size) {
            _arr[_lenght - 1] = item;
        }
        else {
            std::cout << "you cannot add another employee!";
        }
    }
    static void printEmployees(employeeList el) {
        for (int i = 0; i < el._lenght; i++) {
            el._arr[i]->printInfo();
        }
    }
    ~employeeList() {
        delete[]  _arr;
    }
};
int employee::numberOfEmployees = 0;


int main() {


    employee a;
    employee b(5, "John", " Doe");
    employee c(3, "Sue", "Doe");



    employeeList empList(employee::getEmployeeCount());


    empList.addToList(&a);
    empList.addToList(&b);
    empList.addToList(&c);



    employeeList::printEmployees(empList);
    std::cout << empList.listLength() << std::endl;



    return 0;
}

you can see the output:

enter image description here

Why pointer to pointer has been used rather than single pointer in the code? Also do you think the destructor was written wrong if it is how can i make it correct?

2 Answers2

0

Im not expert but u will bumb your topic :P I think that question is not precised. You mean that pointer to pointer:? employee** _arr; Because is pointing a pointer: _arr = new employee * [_size]; I think that it have sense because array is a pointer? I can be wrong ofcourse coz I just started do educate. Why do you think destruktor is wrong? It's deleting a pointer.

  • *Why do you think destruktor is wrong? It's deleting a pointer.* This is true. The object will contain a pointer to an array of pointers, and currently the objects pointed at by the array of pointers are automatically allocated and do not need to be `delete`ed. Unfortunately using automatic allocations does not does not scale well, and when it is suitable, there is no need for the array to be dynamically allocated. It's size is known, fixed by the number of known number of automatic allocation to be added to it. So currently it is correct, but likely not correct for long. – user4581301 Apr 12 '21 at 18:42
0

Why pointer to pointer was used in the code?

This is known only by the author who wrote the code. We can make a reasonable guess that their intention may have been to:

  1. Allocate a dynamic array of objects, using a bare pointer to the first element of that array.
  2. Indirectly point to objects stored elsewhere, hence they wanted to use an array of pointers, thus a pointer to first element of the array is a pointer to a pointer.

Their choice 1. to use an owning bare pointer is unnecessary, and there are better choices available which do not require an owning bare pointer. Most commonly, std::vector would be used to create a dynamic array.

Their choice 2. to indirectly point to objects that aren't owned by the class instance is not quite as safe as having the class instance own the objects, but regardless that may have been a reasonable choice depending on the reasons they chose this design. It is impossible to tell whether the choice was good without documentation of what the program is supposed to do. Based on the generic name of the class, I suspect that it wasn't a good choice.

do you think the destructor was written wrong

It can be considered correct. There are other issues with the class though.

The entire employeeList class seems pointless, and can easily be replaced by a std::vector. printEmployees is the only member function that wouldn't be directly provided by a vector. You can use a non-member function for that instead.

eerorika
  • 232,697
  • 12
  • 197
  • 326