Thread: Arrays with base/derived classes

  1. #1
    Registered User
    Join Date
    Jun 2003

    Question Arrays with base/derived classes


    I need some help. I have been tackling this in my head for a while now and I am sure someone out there has my answer on tap. So here goes.

    I am creating a simple employee storage program.

    I have an employee base class and 4 derived classes with specialized attributes.


    I have created all of my classes and created my constructors/gets/sets etc...

    I am having trouble with the following

    I have two simple public methods called

    getEmployeeInfo and displayEmployee

    They are first declared in my base class Employee and then overridden in all of my four derived classes.

    So here is the question you have all been waiting for.

    How can I create an array to hold all of my objects whether they are a Manager or Support or Hardware etc. And how can I go about making sure the correct displayEmployee method is called based on the object type in the array.

    If you need more explanation please let me know.

    Thanks in advance for any help, tips, or advice.

  2. #2
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    have the array hold pointers to Employees. To make sure that the correct function is called, you can either type cast those pointers to the correct type, or create virtual keywords. I'm sure the tutorials on this site explains how to do all of the above.

  3. #3
    Registered User
    Join Date
    Nov 2001
    Instantiate each instance of the class on the heap. Store its point in an STL container and use dynamic_cast() to cast the base pointer to the correct derived class.


  4. #4
    Registered User
    Join Date
    Jun 2003

    Talking Thank You

    Thank you both for your help!!
    skorman00 and kuphryn...

    I am working on the solution now thanks to your help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic allocation of 2 dim. arrays in classes
    By circuitbreaker in forum C++ Programming
    Replies: 4
    Last Post: 02-10-2008, 12:13 PM
  2. Help with arrays and pointers please...
    By crazyeyesz28 in forum C++ Programming
    Replies: 8
    Last Post: 03-17-2005, 01:48 PM
  3. Questions on Classes
    By Weng in forum C++ Programming
    Replies: 2
    Last Post: 11-18-2003, 06:49 AM
  4. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 07:26 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM