GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 09-Nov-2008, 16:47
Gannon56789 Gannon56789 is offline
New Member
 
Join Date: Nov 2008
Posts: 4
Gannon56789 is on a distinguished road

Array of pointers to objects


I have tried to initialize an array of pointers to objects of an ABC class. The code complies but isn't working how i want it to. I am supposed to derive four classes from the abstract class employee and create an array of pointers to Employee objects. I don't know which objects are going to be made ahead of time, i am supposed to ask for user input. Am i supposed to use new to allocate memory for each part of the array?

Here is my constructor:

CPP / C++ / C Code:
Employee* Earray[10]
Employee()
{
    for ( int i = 0; i < 10; i++)
    {
     Earray[i] = 0;
    }
}
Last edited by admin : 09-Nov-2008 at 23:49. Reason: Please insert your example C/C++ codes between [CPP] and [/CPP] tags
  #2  
Old 09-Nov-2008, 17:37
ocicat ocicat is offline
Regular Member
 
Join Date: May 2008
Posts: 580
ocicat is a jewel in the roughocicat is a jewel in the rough

Re: Array of pointers to objects


Quote:
Originally Posted by Gannon56789
I don't know which objects are going to be made ahead of time...
Yet we can only assume that there will be no more than ten given:
Quote:
CPP / C++ / C Code:
Employee* Earray[10];
Quote:
Am i supposed to use new to allocate memory for each part of the array?
Most likely, yes given the information presented.
Quote:
Here is my constructor...
Whenever code is posted, please place a [cpp] tag before the code, & a [/cpp] tag afterwards. This preserves formatting & color codes keywords & identifiers.
  #3  
Old 09-Nov-2008, 17:44
Gannon56789 Gannon56789 is offline
New Member
 
Join Date: Nov 2008
Posts: 4
Gannon56789 is on a distinguished road

Re: Array of pointers to objects


Yes, the maximum objects is supposed to be 10. How do i initialize the array? I am supposed to make a constructor that creates an empty array. Then later in my code i ask for input from the user and add to the array based on what derived object it is of ABC employee.

CPP / C++ / C Code:

Employee* Earray[10];
Employee()
{  
   for ( int i = 0; i < 10; i++ )
   {
         Earray[i] = new Employee    // Employee is ABC won't work
   }
}
  #4  
Old 09-Nov-2008, 22:50
n00pster n00pster is offline
Junior Member
 
Join Date: Sep 2008
Location: Miami
Posts: 40
n00pster will become famous soon enough

Re: Array of pointers to objects


Quote:
Originally Posted by Gannon56789
CPP / C++ / C Code:
Employee* Earray[10];
Employee()
{  
   for ( int i = 0; i < 10; i++ )
   {
         Earray[i] = new Employee    // Employee is ABC won't work
   }
}

some how am confused about how you want to store an array of pointers to itself in an abstract employee class. It would great if you could please quote the question exactly.

This might make sense at least if you dont want an abstract base class but just a regular base class.
Last edited by n00pster : 10-Nov-2008 at 00:16.
  #5  
Old 09-Nov-2008, 23:05
n00pster n00pster is offline
Junior Member
 
Join Date: Sep 2008
Location: Miami
Posts: 40
n00pster will become famous soon enough

Re: Array of pointers to objects


this is what I guess what you might have to do


1. create a class employee
2. create 4 derived classes which uses this employee class as the base class
3. create an array of pointers to the employee class in the main
4. ask input from the user 10 times with the 4 derived classes as the choices
5. populate the array with this user input.

then what else does the question say?
  #6  
Old 09-Nov-2008, 23:31
n00pster n00pster is offline
Junior Member
 
Join Date: Sep 2008
Location: Miami
Posts: 40
n00pster will become famous soon enough

Re: Array of pointers to objects


might be useful for someone: if you are making an abstract class, it is a general coding guideline to keep its default constructor as protected. I guess the reason is that even if it doesnt have any pure virtual functions yet, it cannot be instantiated with a protected default constructor.
  #7  
Old 10-Nov-2008, 00:35
Gannon56789 Gannon56789 is offline
New Member
 
Join Date: Nov 2008
Posts: 4
Gannon56789 is on a distinguished road

Re: Array of pointers to objects


I am supposed to use a constructor that creates an empty array. The array is supposed to be of pointers to abstract base class Employee. Ask a user for input, they input information based on what type of Employee it is. For instance manager employee has name salary. Foreperson Employee has name and hourly wage. Then use a destructor that deletes all the items of the array.
  #8  
Old 10-Nov-2008, 00:58
n00pster n00pster is offline
Junior Member
 
Join Date: Sep 2008
Location: Miami
Posts: 40
n00pster will become famous soon enough

Re: Array of pointers to objects


Quote:
Originally Posted by Gannon56789
I am supposed to use a constructor that creates an empty array.

constructor for which class? will you be allowed to create some test class which creates this array of employee ptrs in its constructor?

the idea of the exercise is to introduce you to the basic concepts of abstract base classes and runtime polymorphism. but it really isn't pretty if you have to hold an empty array of the base class pointers in itself (and this base class is also supposed to be an abstract base class).
  #9  
Old 10-Nov-2008, 02:46
Gannon56789 Gannon56789 is offline
New Member
 
Join Date: Nov 2008
Posts: 4
Gannon56789 is on a distinguished road

Re: Array of pointers to objects


oh okay i can see how i am not being very clear. I am making one class called Payroll from which i am supposed to have an array of pointers to objects of type Employee. Employee is an abstract bass class. From employee i have four classes derived. The constructor of the payroll class is supposed to create the 10 element array of pointers to Employee objects but empty. The destructor is supposed to delete all the elements of the array. I have a function from payroll called Run, that prompts the user to input employee names and information into the array.
  #10  
Old 10-Nov-2008, 04:29
ocicat ocicat is offline
Regular Member
 
Join Date: May 2008
Posts: 580
ocicat is a jewel in the roughocicat is a jewel in the rough

Re: Array of pointers to objects


Quote:
Originally Posted by Gannon56789
I am making one class called Payroll from which i am supposed to have an array of pointers to objects of type Employee.
This is beginning to smell like a UML assignment. Whether it is or not, a question you need to answer for yourself is whether there will be only one instance of this class or whether there can be a number of instances of class Payroll. The point here is whether you intend for multiple ten-element arrays to be floating about.
 
 

Recent GIDBlogProgramming ebook direct download available by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
[C++] Array of pointers to class objects Całeczka C++ Forum 2 03-Dec-2006 13:05
Need help deleting the last element in the array headphone69 C++ Forum 2 15-Mar-2006 20:31
[Tutorial] Pointers in C (Part II) Stack Overflow C Programming Language 0 27-Apr-2005 18:36
[Tutorial] Pointers in C (Part I) Stack Overflow C Programming Language 1 08-Apr-2005 19:35

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 19:59.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.