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;
}
Azeem
  • 11,148
  • 4
  • 27
  • 40
  • Create an object, and call the `setValues` member with the correct values from *one line*? – Some programmer dude Aug 22 '17 at 03: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](https://stackoverflow.com/questions/45807690/c-reading-and-storing-information-from-file) looks like they are trying to code their way into a deep hole. – user4581301 Aug 22 '17 at 03:56
  • On topic: [Zip on down to the section on Bitshift Operators (used for Stream I/O)](https://stackoverflow.com/questions/4421706/what-are-the-basic-rules-and-idioms-for-operator-overloading) and give it a read. Build into the `>>` operator [Option 2 from this answer](https://stackoverflow.com/a/7868998/4581301) and you should be on your way with code that looks something like `while(file >> emplyeelist[count]) count++;` – user4581301 Aug 22 '17 at 04: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. – CinCout Aug 22 '17 at 04: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. – futilecheese28 Aug 22 '17 at 06:59

1 Answers1

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
NonCreature0714
  • 5,744
  • 10
  • 30
  • 52
  • @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? – NonCreature0714 Aug 22 '17 at 07:20
  • 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 Aug 22 '17 at 07:37
  • @futilecheese28 https://stackoverflow.com/questions/620843/how-do-i-create-an-array-of-pointers I believe the second answer will get you were you want. You'll notice this question is similar to your own ;) – NonCreature0714 Aug 22 '17 at 07:42
  • @Macmade that does make things more simple - I'll update it soon – NonCreature0714 Aug 25 '17 at 22:23