Automatic object creation?

Oct 26, 2009 at 4:06pm
Okay, so I need to have a program setup so that people can enter in a user's first name and last name, hours and salary. They need to be able to keep entering in data until they decide they are done. Then, they need to be able to recall the information. I can get it to work with 1 employee, but I don't know how to have it automatically create the objects (each object is an employee). Here is what I have:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// Payroll Main
// Programmed by Brent Wassell
#include <iostream>

#include "payroll.h" // include definition of Class Employee

using std::cout;
using std::cin;
using std::endl;
#include <string>
using std::string;

// function main
int main()
{
int EmployeeNumber = 0;
string EmpFirstName;
string EmpLastName;
int EmpHours;
int EmpSalary;


	// Output Data
	cout << "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n";
	cout << "+++                       Welcome to Employee Log!                       +++\n";
	cout << "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n";
	cout << endl;
	cout << "Please enter employees first name...\n";
	cin >> EmpFirstName;
	cout << "Please enter employees last name...\n";
	cin >> EmpLastName;
	cout << "Please enter how many hours " << EmpFirstName << " " << EmpLastName << " worked this week...\n";
	cin >> EmpHours;
	cout << "Please enter how many dollars " << EmpFirstName << " " << EmpLastName << " gets paid per hour...\n";
	cin >> EmpSalary;
	cout << endl;
	Employee employee1( EmpFirstName , EmpLastName , EmpHours, EmpSalary ); // Create employee 1 with information
	cout << "Employee 1 is named " << employee1.getFirstName() << " " << employee1.getLastName() << "\nThey worked " << employee1.getHoursWorked() << " hours and they earned $" << employee1.getPayPerHour() << ".00 per hour" << endl;
cout << endl;
		
			

return 0;
}


How do I use pointers to when I create the object use

Employee employeeobject( EmpFirstName , EmpLastName , EmpHours, EmpSalary );

And then recall ALL of the employees with something like

1
2
cout << "Employee is named " << employeenumber.getFirstName() << " " << employeenumber.getLastName() << "\nThey worked " << employeenumber.getHoursWorked() << " hours and they earned $" << employeenumber.getPayPerHour() << ".00 per hour" << endl;
cout << endl;
Oct 26, 2009 at 5:51pm
Use a container like STL vector or list. Put all your employee variables into a single struct, then create a vector or list of that struct type.

http://cplusplus.com/reference/stl/vector/
Oct 27, 2009 at 5:38pm
I know the way is to use pointers...but I don't know how. I followed the link and read up on it, but I need more coding examples and that did not make a whole lot of sense to me... Any other ideas?
Oct 27, 2009 at 7:01pm
For example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <vector> //for vectors

class Obj {
    int data;
public:
    void set_data(int new) { this->data = new; }
    int get_data() { return this->data; }
}

int main() {
    std::vector<Obj> objects; //this makes a vector of objects
    Obj my_new_obj;
    my_new_obj.set_data(5); //make and set a new object
    objects.push_back(Obj); //add the object to the vector
    //...
    for(unsigned int i = 0; i < objects.size(); ++i) { //loop through the vector
        std::cout<<"Obj "<<i<<" has "<<objects[i].get_data()<<"\n"; //print out the info
    }
}
Topic archived. No new replies allowed.