![]() |
|
#1
|
|||
|
|||
Array of pointers to objectsI 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:
Last edited by admin : 09-Nov-2008 at 23:49.
Reason: Please insert your example C/C++ codes between [CPP] and [/CPP] tags
|
|||
|
#2
|
||||
|
||||
Re: Array of pointers to objectsQuote:
Quote:
Quote:
Quote:
|
|
#3
|
|||
|
|||
Re: Array of pointers to objectsYes, 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:
|
|
#4
|
|||
|
|||
Re: Array of pointers to objectsQuote:
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
|
|||
|
|||
Re: Array of pointers to objectsthis 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
|
|||
|
|||
Re: Array of pointers to objectsmight 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
|
|||
|
|||
Re: Array of pointers to objectsI 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
|
|||
|
|||
Re: Array of pointers to objectsQuote:
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
|
|||
|
|||
Re: Array of pointers to objectsoh 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
|
|||
|
|||
Re: Array of pointers to objectsQuote:
|
Recent GIDBlog
Programming ebook direct download available by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
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