hey guys.
So im supposed to...
1. Create a small program with vector of pointers to Employee objects.
2. ask user how many employee objects they want
3. create employee objects
4. assign their pointers to the vector
5. Display the vector
now.
what I have is....
1 2 3
Cout << “how many employees?” << endl;
Cin >> howMany;
Employee employee(string name, int salary);
from here im just stumped!
if someone could give me some help I would really appreciate it!
thanks
class Employee
{
private:
string m_name;
int m_salary;
public:
Employee( string name, int salary ) : m_name(name), m_salary(salary)
{}
};
I'm not going to write all the code for you but here is a start
1 2 3 4 5 6 7 8
vector< Employee * > employees;
for( int i=0; i < howMany; i++ )
{
// You need to get the name and salary for each employee here
// and pass it into the constructor
employees.push_back( new Employee( "", 0 ) );
}
Looks fine to me. Although the creation of an employee called one seems to be pointless - you don't do anything with it, and the object will fall out of scope and be destroyed at the end of each iteration of the loop.
The first instance of Employee (called one) is a value type on the stack and is not used by anything after it is declared.
The second instance of Employee (inside the call to push_back) is a pointer to an instance of Employee and is created in the heap and stored in the vector.