I'm going to leave out a few things, since this looks like homework, but this will get you far.
A couple of things to watch out for:
- You aren't using a constructor. Sure a default constructor is nice, but it can help to make your own, especially when starting out.
- You should probably use
vector
instead of an array
.
For example:
// Note that I'm making the members public - this is only for demonstration so I don't have to write getters and setters.
class Employee {
public:
Employee(long, std::string, int, std::string);
long timestamp;
std::string staffID;
int locNum;
std::string eventCode;
};
// Here is the constructor.
Employee::Employee(long l, std::string s, int n, std::string s2): timestamp(l), staffID(s), locNum(n), eventCode(s2){}
As for an array - it may be wiser to stick to using a vector of Employee pointers. Such as:
typedef Employee * EmployeePointer;
EmployeePointer employeePtr;
std::vector<EmployeePointer> employeeVec;
Then .push_back()
new Employees using your fancy new constructor with a pointer.
employeePtr = new Employee(181213, "Bob", 22, "OP");
employeeVec.push_back(employeePtr);
And simply re-use the employeePtr
for new employees.
employeePtr = new Employee(666732, "Sue", 21, "MA");
employeeVec.push_back(employeePtr);
And you will see that a vector (which most other languages refer to as an Array anyway) of pointers to employee objects is created:
for(auto it = employeeVec.begin(); it != employeeVec.end(); it++){
std::cout << (*it)->timestamp << " " << (*it)->staffID << " " << (*it)->locNum << " " << (*it)->eventCode << std::endl;
}
Which displays:
181213 Bob 22 OP
666732 Sue 21 MA
If you can't use vector
for whatever reason then implementing this with an array isn't that different, except.
EmployeePointer empPtrArr;
// You'll need to know the size of your array of pointers or else dynamically allocate it, which I don't think you want to do.
empPtrArr = * new EmployeePointer[2];
And You'll have to use a basic for-loop, and not that fancy for(auto ... )
one I used.
Final comments:
- For every 'new' there is a 'delete', or you'll have a memory leak
- There is at least one
#include
I left out for you to figure out, which shouldn't be difficult to find