pointer problems

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
First you need to define the Employee class

1
2
3
4
5
6
7
8
9
10
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 ) );
}



hey binarybob thanks for the help

what do you think?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Employee
{
  private:
    string m_name;
    int    m_salary;

//RIGHT HERE I HAVE A QUESTION....WHY DO I NEED ":m_name(name), m_salary(salary)
//   {}"?? 
  public:
    Employee( string name, int salary ) : m_name(name), m_salary(salary)
    {}
};

vector< Employee* > employees;

for( int i=0; i < howMany; i++ )
{
 cout << "enter name" << endl;
 cin >> name;
cout << "enter salary" << endl;
cin >> salary;
Employee one(name, salary);
employees.push_back( new Employee( name, salary ) );
}
immediately after instantiating an object belonging to class Employee, it assigns name to m_name and salary to m_salary. So its like saying
1
2
3
4
5
6
public:
Employee(string name, int salary)
{
m_name=name;
m_salary=salary;
}
Hey Matri X thanks for the info
does the below look fine?

1
2
3
4
5
6
7
8
9
10
11
vector< Employee* > employees;

for( int i=0; i < howMany; i++ )
{
 cout << "enter name" << endl;
 cin >> name;
cout << "enter salary" << endl;
cin >> salary;
Employee one(name, salary);
employees.push_back( new Employee( name, salary ) );
}


I'm not so sure if I'm using the push back correctly
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.

Hey MikeyBoy,

so what you're saying is

1
2
3
4
5
//THIS
Employee one(name, salary);

//IS ALREADY INCLUDED IN THIS
employees.push_back ( new Employee (name, salary) );
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.
Hey MikeyBoy,

so what you're saying is

1
2
3
4
5
//THIS
Employee one(name, salary);

//IS ALREADY INCLUDED IN THIS
employees.push_back ( new Employee (name, salary) );


No. I'm saying that you're creating an Employee object called one, and then never doing anything with it at all. It's a waste of time.

The Employee objects that you're putting into the vector are being created by new Employee (name, salary). Those are totally different from one.
Topic archived. No new replies allowed.