Hi I have never used vedctors before so I am just wondering if this is correct or not.
If not, would you mind showing me the correct way to use it?

cout << fixed << setprecision( 2 ); 
	vector < Employee * > employees( 4 );
	employees[ 0 ] = new Manager( 
   "Jack", "Jackson", "123", 800 );
  employees[ 1 ] = new EntryLevelEmployee( 
   "Adrian", "Kiefer", "234", 16.75, 40 );
  employees[ 2 ] = new SalesRep( 
   "Crystal", "Jones", "456", 10000, .06 );
  employees[ 3 ] = new SalesManager( 
   "Robert", "Robertson", "678", 5000, .04, 300 );

I don't see any "push_back" statements. The whole idea of a vector is that you don't have to specify any array indexes. If you're going to do that, might as well use an array, not a vector.

Hmm ok thanks, I think.
Using an array rather than a vector will require me to change a lot of my code or no?

I believe you might have a problem with line #2.

I just looked over the vector class constructors.. the only constructor with a single parameter takes a "reference to a vector" argument. In your code, you are attempting to pass an integer, which will probably throw a "could not convert int to const vector<T,Allocator>&" type of error during compile.

I would recommend declaring the vector using it's default constructor. Since you will have an empty vector, you'll have to push_back( ) new Employee objects, which you can populate using employee class constructors.

Also... I notice that you have a vector of Employee* pointers..... but then you are attempting to assign new objects of type Manager, EntryLevelEmployee, SalesRep, and SalesManager... Unless there is some sort of type casting that I am not aware of.. an Employee* pointer can only hold an address to an Employee object... therefore, you can only populate a vector<Employee*> with new Employee's.

Correct me if I'm wrong, but the two major flaws i've seen in your code are an incorrect use of a vector constructor, and a fundamental misuse of pointers.

>> Using an array rather than a vector will require me to change a lot of my code or no?

That wasn't my point. I'm not telling you to not use a vector. My point is that with a vector, you add an element one at a time and you let the vector keep track of the array index. Hence it would be more like this:

Employee* anEmployee;
anEmployee = new Manager( 
   "Jack", "Jackson", "123", 800 );
employees.push_back(anEmployee);
anEmployee = new EntryLevelEmployee( 
   "Adrian", "Kiefer", "234", 16.75, 40 );
employees.push_back(anEmployee);

// etcetera

The vector figures out what the index is. You just add it using the push_back command. it saves you the work of keeping track.

>> I just looked over the vector class constructors.. the only constructor with a single parameter takes a "reference to a vector" argument. In your code, you are attempting to pass an integer, which will probably throw a "could not convert int to const vector<T,Allocator>&" type of error during compile.

That does look wrong. Instead, use the default constructor, then the resize command. I think that's what you're going for. Or don't even bother resizing it.


>> Also... I notice that you have a vector of Employee pointers..... but then you are declaring new objects of type Manager, EntryLevelEmployee, SalesRep, and SalesManager...

I'm guessing there's some polymorphism going on and Employee is the base class. If so, I think it's OK.

[EDIT]
Scratch that, I think I'm confusing size and capacity. Forget I said anything. :)
[/EDIT]

commented: Good call on the suspected polymorphism +6

I'm guessing there's some polymorphism going on and Employee is the base class. If so, I think it's OK.

Good call. This is where things get a little fuzzy to me.. I think there are some special rules to consider when using pointers to a base class object vs. child class objects..

Hopefully one of our c++ guru's will step in and clear things up.. need a refresher on pointer use during polymorphism.

The point of vectors is that they keep track of indices for you (as VernonDozier pointed out). If you know that you will have EXACTLY 4 employees in your vector (as your code has now), it would make sense to use a simple array, as you are not taking advantage of the features contained in the vector class.

The capacity of a vector is dynamic: it changes as you add more elements. Thus your vector < Employee * > employees( 4 ) is not strictly necessary, though it does improve performance to allocate enough space initially.

The real advantage of a vector is that you can now add a fifth (or 6th, 7th...) employee without messing around managing memory. Again, since the memory management and indexing is taken care of for you, code like the following is possible:

vector < Employee * > employees( 4 );

//Add any number of employees...
employees.push_back(emp_1);
employees.push_back(emp_2);
employees.push_back(emp_3);
employees.push_back(emp_4);
employees.push_back(emp_5);
employees.push_back(emp_6);
...

for (int i = 0; i < employees.size(); i++) {
(perform some action with employees[i]...)
}

while (!employees.empty()){
//Delete the last element
employees.pop_back();
}
//employees is now empty

See how nicely vectors track indexing? Much easier than allocating memory, tracking indices, and reallocating everything yourself.

commented: muy bueno +6

vector<>(int n) constructs n objects, so above code is wrong, employees.size() is 10. The difference between push_back and default constructor is most of the time negligible.

@Zjarek Ah yes, my mistake. I copied the code from the OP in initializing the vector. Thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.