Vectors of pointers to objects

This is a discussion on Vectors of pointers to objects within the C++ Programming forums, part of the General Programming Boards category; I am trying to set up a vector of pointers to objects using the base object class. I am then ...

  1. #1
    Registered User Myownworstenemy's Avatar
    Join Date
    May 2003
    Posts
    8

    Vectors of pointers to objects

    I am trying to set up a vector of pointers to objects using the base object class. I am then assigning specific objects of pointers to derived classes to the vector. I have attached the actual driver program. All the classes work with a different driver program. I am sure this has inly to do with my use of pointers in the program. I knwo there may be a better way to do this, but I am doing this for an asignment, and the requirements are very specific. This code compiles fine using Borland, but it will not run properly. Any suggestions would be greatly appreciated.

    Here is my code:


    <CODE>
    void doPayroll(vector<Employee*> employeelist);

    int main( )
    {
    clrscr( );

    vector<Employee*> employeelist;

    Salaried* employee1;

    employee1->setName("Laura Bush");
    employee1->setEmpNumber(1);
    employee1->setMonthlySalary(50000.00);

    employee1->setAddress("1600 Pennsylvania Ave.");
    employee1->setPhoneNumber("123-4567");
    employee1->setDeptNumber(100);

    employeelist.push_back(employee1);


    Sales* employee2;

    employee2->setName("Muffin Man");
    employee2->setEmpNumber(2);
    employee2->setMonthlySalary(40000.00);
    employee2->setMonthlySales(165000.00);
    employee2->setCommission(0.4);

    employee2->setAddress("34 Drury Lane");
    employee2->setPhoneNumber("890-1234");
    employee2->setDeptNumber(200);

    employeelist.push_back(employee2);


    Hourly* employee3;

    employee3->setName("Tony Blair");
    employee3->setEmpNumber(3);
    employee3->setHourlyRate(34.50);
    employee3->setHoursWorked(50);

    employee3->setAddress("27 Downing Street");
    employee3->setPhoneNumber("567-8901");
    employee3->setDeptNumber(300);

    employeelist.push_back(employee3);


    PartTime* employee4;

    employee4->setName("Mephistopheles");
    employee4->setEmpNumber(4);
    employee4->setHourlyRate(22.00);
    employee4->setHoursWorked(30);
    employee4->setMaxHours(25);

    employee4->setAddress("7734 Upside Down Street");
    employee4->setPhoneNumber("234-5678");
    employee4->setDeptNumber(400);

    employeelist.push_back(employee4);

    }



    void doPayroll(vector<Employee*> employeelist)
    {
    cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.precision(2);

    int x = 0;

    employeelist[x]->calcSalary( );

    cout << "\n\nDepartment: " << employeelist[x]->getDeptNumber( )
    << "\tEmployee Number: " << employeelist[x]->getEmpNumber( )
    << "\n\nEmployee Name:\t" << employeelist[x]->getName( )
    << "\nAddress:\t" << employeelist[x]->getAddress( )
    << "\nPhone Number:\t" << employeelist[x]->getPhoneNumber( )
    << "\n\nGross Pay: \t\t$" << employeelist[x]->getGrossPay( )
    << "\nFederal Taxes: \t$" << employeelist[x]->getFedTax( )
    << "\nState Taxes: \t\t$" << employeelist[x]->getStateTax( )
    //<< "\nBenefits: \t\t$" << employeelist[x].getBenefits( )
    << "\n\t\t\t--------------"
    << "\nNet Pay: \t\t$" << employeelist[x]->calcTaxes( ) << "\n\n"
    << "_________________________________________";
    }
    </CODE>
    Attached Files Attached Files

  2. #2
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    First, a quick note about the code tags... They use '[' and ']' instead of the angled counterparts ('<' and '>').

    Next, to your problem. This problem occurs in each case, but I'll only use one to demonstrate:
    Code:
      Salaried* employee1;
    
      employee1->setName("Laura Bush");
    What happens is that you have declared the pointer, but not actually initialized it. In this case, you want to create a new instance of the object as follows:
    Code:
      Salaried* employee1 = new Salaried(/*... any constructor params ...*/ );
      employee1->setName("Laura Bush");
    If there are no parameters for the constructor you're using, then the parentheses can be omitted alltogether.

    Once you are done with your vector, loop over each element and call 'delete' on it to free the memory again. To properly free memory though, make sure that your base class destructor is virtual so that the correct destructor will be called for each object (otherwise, only the base class part of the object would be freed, and the rest of the object would be a memory leak).

    **edit**
    Though I am guessing the pointers are part of your assignment, you might want to consider using std::auto_ptr or boost::shared_ptr, et al (www.boost.org) for future needs of this sort. A list of these object instead of the pointers is much safer, and generally adequate.
    Last edited by Zach L.; 09-01-2003 at 11:52 PM.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    161
    You are not allocating memory for your objects.

    Code:
    Salaried* employee1 = new Salaried();
    Also, you'll want to clean up your vector when you're done with it.

    Code:
    vector<Employee*>::iterator itr = employeelist.begin();
    
    for( ; itr != employeelist.end(); itr++)
      delete *itr;
    For this to work, make sure your Employee class has a virtual destructor.


    Edit: beaten by Zach

    Just a note: As far as I know, it's not safe to stuff an auto_ptr into any STL containers.
    Last edited by thefroggy; 09-02-2003 at 12:04 AM.

  4. #4
    Registered User Myownworstenemy's Avatar
    Join Date
    May 2003
    Posts
    8
    Thanks so much to both of you. I was able to complete the assignment. It was the first time I have ever successfully passed a vector of pointers to a function.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing Objects, Constructors, Pointers, References, Values ???
    By BlackSlash12 in forum C++ Programming
    Replies: 24
    Last Post: 12-14-2007, 06:26 PM
  2. Array Of Pointers With Objects
    By TheTaoOfBill in forum C++ Programming
    Replies: 6
    Last Post: 11-28-2007, 09:37 PM
  3. Replies: 4
    Last Post: 11-02-2006, 11:41 AM
  4. objects and pointers
    By SiteResources in forum C++ Programming
    Replies: 2
    Last Post: 12-02-2003, 08:29 AM
  5. Array of pointers to point objects
    By totalfreeloader in forum C++ Programming
    Replies: 6
    Last Post: 11-27-2003, 09:26 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21