0

I'm confused on how to put numerous objects into a class. So we are required to read in a file that contains a timestamp, employee ID, location number, event code. An example of the input is:

10039865 WHITE99 1 OP
10039876 WHITE99 1 EN
10047500 PINK01 1 EN
10047624 SMITH01 3 EX
10047701 TAN07 2 EN
10048567 DIITZ01 2 OP
10048577 DIITZ01 2 OP
10048587 DIITZ01 2 OP

how do I set those information into objects in a class? here is what I got so far, and stuck from here. We are required to write a program using an array of pointers to objects.

class Employee {

    long timestamp;
    string staffID;
    int locNum;
    string eventCode;

public:
    void setValues (long, string, int, string);

};

void Employee::setValues(long timestamp, string staffID, int locNum, string eventCode) {
    this->timestamp = timestamp;
    this->staffID = staffID;
    this->locNum = locNum;
    this->eventCode = eventCode;
}
5
  • Create an object, and call the setValues member with the correct values from one line? Commented Aug 22, 2017 at 3:50
  • Off topic: "We are required to write a program using an array of pointers to objects" Complete the assignment as required, but know this: You will almost never want to do this in real programming. Also explains why this poor sucker looks like they are trying to code their way into a deep hole. Commented Aug 22, 2017 at 3:56
  • On topic: Zip on down to the section on Bitshift Operators (used for Stream I/O) and give it a read. Build into the >> operator Option 2 from this answer and you should be on your way with code that looks something like while(file >> emplyeelist[count]) count++; Commented Aug 22, 2017 at 4:01
  • 1
    "We are required to write a program using an array of pointers to objects." Well then, create an array of pointers to Employee objects, read from the file line-by-line to create and fill an Employee object and then insert its address into the array created earlier. Commented Aug 22, 2017 at 4:17
  • Can you show me how to do that? I'm still new at coding, so I'm not really sure how to implement this. Commented Aug 22, 2017 at 6:59

1 Answer 1

0

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:

  1. You aren't using a constructor. Sure a default constructor is nice, but it can help to make your own, especially when starting out.
  2. 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
Sign up to request clarification or add additional context in comments.

4 Comments

@Macmade agreed! Though OP needs a collection of pointers to objects, and I don't know OP's experience or restrictions - so thought it was a simple way to appease a starting level should vectors be ruled out. What is your alternative?
Thank you! This helped me a lot. Our professor is limiting us in using arrays since we have not discussed vectors or maps in class yet. I have a question about the pointers to employee objects. Since I am using an array, how would I do it?
@futilecheese28 stackoverflow.com/questions/620843/… I believe the second answer will get you were you want. You'll notice this question is similar to your own ;)
@Macmade that does make things more simple - I'll update it soon

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.