1

I'm having an issue with making one array with 3 objects. I'm trying to display the information of employees and my issue is that I don't know where I would assign the strings to the appropriate members and how to keep one variable for name. My code and an example are below:

class employee
{
private:                
    string name;
    string idNumber;

public:                 
    void setName(string);
    void setIDnumber(string);
    string getName();
    string getIDnumber();

};

const int numEmployees = 3;

int main()
{

    employee employeeInfo [numEmployees];
}

I want to create a constructor for 3 employees and input(name, idNumber). for example, in employeeInfo[0], the first employee, I want to input "Tom Jones", "123456" and in employee[1], the second employee, "James Blyth", "QE123"

6
  • Are you familiar with the notions of constructor and Implicitly-declared default constructor Commented Aug 19, 2021 at 22:09
  • @KarenBaghdasaryan yes but not much Commented Aug 19, 2021 at 22:13
  • @David___S__ How would you initialize a single employeeInfo, let alone an array of them? You've seemed to have skipped over the basic concept of constructors. Commented Aug 19, 2021 at 22:13
  • 1
    Are you familiar with arrays generally? employeeInfo[0] will be the first employee in the array, employeeInfo[1] will be the second, etc. There's not really anything special about it being an array of employee objects as opposed to an array of ints or floats or something. Commented Aug 19, 2021 at 22:14
  • 1
    So for example you could call employeeInfo[0].setName("Tom Jones"); to set the name of the first employee in the array to "Tom Jones" and employeeInfo[1].setIDnumber("QE123") Commented Aug 19, 2021 at 22:16

1 Answer 1

3

You can simply call the setter methods on the individual employee objects, eg:

int main()
{
    employee employeeInfo [numEmployees];

    employeeInfo[0].setName("Tom Jones");
    employeeInfo[0].setIDNumber("123456");

    employeeInfo[1].setName("James Blyth");
    employeeInfo[1].setIDNumber("QE123");

    // and so on...

    ...
}

If you really want to add a constructor, then it would look like this:

class employee
{
private:                
    string name;
    string idNumber;

public:                 
    employee() = default;
    employee(string name, string idNumber);

    ...
};

employee::employee(string name, string idNumber) {
    setName(name);
    setIDnumber(idNumber);
};

...
int main()
{
    employee employeeInfo [numEmployees];

    employeeInfo[0] = employee("Tom Jones", "123456");
    employeeInfo[1] = employee("James Blyth", "QE123");
    // and so on...
}

Just note that this will default-construct the array elements first, and then construct temporary objects to overwrite the array elements.

If you want to avoid the temporaries, but still want a fixed array of objects stored consecutively, you can use placement-new to construct the array members directly using your non-default constructor, eg:

#include <type_traits>

int main()
{
    std::aligned_storage_t<sizeof(employee), alignas(employee)> buffer[numEmployees];
    employee* employeeInfo = reinterpret_cast<employee*>(buffer);

    new(&employeeInfo[0]) employee("Tom Jones", "123456");
    new(&employeeInfo[1]) employee("James Blyth", "QE123");
    // and so on...

    ...

    for(int i = 0; i < numEmployees; ++i) {
        employeeInfo[i].~employee();
    }
}

Or, you can use a std::vector (if you don't mind the array being allocated in dynamic memory), eg:

#include <vector>

int main()
{
    std::vector<employee> employeeInfo;
    employeeInfo.reserve(numEmployees);

    employeeInfo.emplace("Tom Jones", "123456");
    employeeInfo.emplace("James Blyth", "QE123");
    // and so on...

    ...
}

Otherwise, create an array of pointers to objects instead, and then you can create the individual objects dynamically using new with your non-default constructor. The objects just won't be stored consecutively in memory, eg:

int main()
{
    employee* employeeInfo[numEmployees];

    employeeInfo[0] = new employee("Tom Jones", "123456");
    employeeInfo[1] = new employee("James Blyth", "QE123");
    // and so on...

    ...

    for(int i = 0; i < numEmployees; ++i) {
        delete employeeInfo[i];
    }
}

Which would be safer handled with std::unique_ptr, eg:

#include <memory>

int main()
{
    std::unique_ptr<employee> employeeInfo[numEmployees];

    employeeInfo[0] = std::make_unique<employee>("Tom Jones", "123456");
    employeeInfo[1] = std::make_unique<employee>("James Blyth", "QE123");
    // and so on...

    ...
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.