472,128 Members | 1,689 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,128 software developers and data experts.

Array of pointers to objects

Ok i have been trying to figure this out for hours upon hours and i can't get it to work. I have an abstract base class called Employee. From employee i have derived four classes in which i define the pure virtual function no longer making them abstract. I then created a class called payroll. In the payroll class i am creating an array of pointers to objects of abstract base class Employee. I am having trouble figuring out how to do the constructor for the payroll class. Should i use new? Can anyone show me how to do it?

Expand|Select|Wrap|Line Numbers
  1.  
  2. Employee* Earray[10];
  3.  
  4. Payroll()
  5. {
  6.     for ( int i = 0; i < 10; i++)
  7.     {
  8.     Earray = new Employee  //can't do because employee is abstract
  9.     }
  10. }
  11.  
  12.  
  13. ~Payroll()
  14. {
  15.    for ( int i = 0; i <10; i++)
  16.    {
  17.           delete Earray[i];
  18.    }
  19. }
  20.  
Nov 10 '08 #1
13 4627
arnaudk
424 256MB
From employee i have derived four classes in which i define the pure virtual function no longer making them abstract.
Nope, that's not it. If you want to create objects of your derived classes, you can not have pure virtual methods in there, only concrete methods or, in case you will derive from the subclass later, virtual methods.
You need to provide an implementation in derived classes for all abstract methods in the base class if you want to instantiate objects of the derived class.
Nov 10 '08 #2
JosAH
11,448 Expert 8TB
A Payroll doesn't know who's on it beforehand. Make a method 'add(Employee*)'
to your Payroll class to add employees to it.

kind regards,

Jos
Nov 10 '08 #3
I am supposed to have the constructor of the payroll class create the Emparray but make it empty. Then i call Payroll.run and prompt a user for input and the add input to the array based on what type of information they put in. For instance if its a manager they would put in the name and salary pay. Foreperson would be hourly and name. I have all the classes done i just can't figure out how to initialize the array correctly.

Expand|Select|Wrap|Line Numbers
  1.  
  2. Employee* Earray[10];
  3. Payroll()
  4. {
  5.     for ( int i = 0; i < 10; i++ )
  6.     {
  7.         Earray = add(*Employee);
  8.     }
  9. }
  10.  
  11. add(Employee&)
  12. {
  13.  
  14. }
  15.  
Nov 10 '08 #4
arnaudk
424 256MB
I think Employee * Earray needs to be a (private) member of the Payroll class. Then you allocate memory for it in the initializer list:
Expand|Select|Wrap|Line Numbers
  1. Payroll() : Earray = new Employee[10] { }
  2. ~Payroll() { delete [] Earray; }
  3.  
The Payroll also needs an add() method as Jos mentions to subsequently add employees to the payroll.
Nov 10 '08 #5
Thank you guys that helpped me a lot
Nov 10 '08 #6
weaknessforcats
9,208 Expert Mod 8TB
You may not be out of the weeds yet.

Re-read what JosAH said: The Payroll does not know what Employees are on the Payroll ar the time the Payroll is created.

Therefore, you cannot create an array of Employee in the Payroll constrcutor. Instead, you have to create an array of Employee*.

Then you initialize each of these Employee* to zero while still inside the Payroll constructor.

Later when you add an Employee (read JosAH again), you store the Employee* received by the member function in the array of Employee* in the Payroll object.
Nov 10 '08 #7
So it would be like this? I appreciate the help.

Expand|Select|Wrap|Line Numbers
  1.  
  2. Employee* Earray[10];   // Private member of payroll class
  3. Payroll()
  4. {
  5.    for ( int i = 0; i <10; i++)
  6.    { 
  7.         Earray[i] = 0;
  8.    }
  9.  
  10. }
  11.  
  12. ~Payroll()
  13. {
  14.      for ( int i = 0; i <10; i++)
  15.      {
  16.  
  17.             delete [] Earray;
  18.      }
  19.  
  20. Add(*Employee)
  21. {
  22.  
  23.      for ( int i =0; i < 10; i++)
  24.      {
  25.      cout << "Input Employee name" <<endl;
  26.      STRING S;   // STRING type holds strings
  27.      cin >> S;
  28.      Earray[i] = S;
  29.      }  
  30. }
  31.  
  32.  
  33.  
Nov 10 '08 #8
boxfish
469 Expert 256MB
Expand|Select|Wrap|Line Numbers
  1. for ( int i = 0; i <10; i++)
  2.     delete [] Earray; 
  3. }
You don't need to delete Earray ten times. A loop like this is only nessecary with a multidimensional array. As long as you have the brackets after delete, all the elements will get deleted.
Edit:
You are allocating memory for Earray somewhere, aren't you? It's
Earray = new Employee*[10]
Nov 10 '08 #9
Would this work?
Expand|Select|Wrap|Line Numbers
  1. Employee* Earray[10];    //Private member of payroll
  2. Payroll() 
  3.    Earray = new *Employee[10];
  4.    for ( int i = 0; i <10; i++) 
  5.    {  
  6.         Earray[i] = 0; 
  7.    } 
  8.  
  9.  
  10. ~Payroll() 
  11.  
  12.  
  13.             delete [] Earray; 
  14.  
  15.  
  16. Add(*Employee) 
  17.  
  18.      for ( int i =0; i < 10; i++) 
  19.      { 
  20.      cout << "Input Employee name" <<endl; 
  21.      STRING S;   // STRING type holds strings 
  22.      cin >> S; 
  23.      Earray[i] = S; 
  24.      }   
  25.  
Nov 10 '08 #10
Ganon11
3,652 Expert 2GB
*Employee is the syntax for dereferencing a pointer variable.

Employee* is a pointer to an Employee object. You are trying to use the two syntaxes interchangeably throughout your code.

For example:

Expand|Select|Wrap|Line Numbers
  1. Earray = new *Employee[10];
is just wrong. You want:

Expand|Select|Wrap|Line Numbers
  1. Earray = new Employee*[10];
Similarly for your Add function...which doesn't have a return value right now.
Nov 10 '08 #11
Thank you. Will this work now?

Expand|Select|Wrap|Line Numbers
  1. Employee* Earray[10];    //Private member of payroll
  2. Payroll() 
  3.    Earray = new Employee*[10];
  4.    for ( int i = 0; i <10; i++) 
  5.    {  
  6.         Earray[i] = 0; 
  7.    } 
  8.  
  9.  
  10. ~Payroll() 
  11.     delete [] Earray; 
  12. }  
  13.  
  14. Add(Employee*) 
  15.  
  16.      for ( int i =0; i < 10; i++) 
  17.      { 
  18.      cout << "Input Employee name" <<endl; 
  19.      STRING S;   // STRING type holds strings 
  20.      cin >> S; 
  21.      return  Earray[i] = S; 
  22.      }   
  23.  
Nov 10 '08 #12
Ganon11
3,652 Expert 2GB
You still don't have a very good grasp of functions or object-oriented concepts. Your add function still doesn't have a return value, so that's a syntax error.

In your Add function, you are reading STRINGs, then assigning your array entries to these strings. An Employee* is not a STRING. For that matter, an Employee is not a STRING.

Your Add function should take an Employee* object and assign one of your Earray entries to the pointer. The Payroll class/object should not have to know how to ask the user for an Employee - your main() function (or some other function) should take care of that.

By the way, instead of asking us if your code will work, try putting it into your compiler and seeing if it works yourself. That alone would have told you the answer - no, it's not going to work.
Nov 10 '08 #13
Ganon11
3,652 Expert 2GB
Gannon56789,

I deleted your second thread on the same topic. Please do not post two threads on the same subject - continue to use this thread so that anyone attempting to help you can read on everything you've tried and all the suggestions we've made.

When you have the time, please read our Posting Guidelines...it contains valuable information including our rules on things like Double Posting.

MODERATOR
Nov 12 '08 #14

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by James | last post: by
67 posts views Thread by Ike Naar | last post: by
8 posts views Thread by Peter B. Steiger | last post: by
7 posts views Thread by Frank M. | last post: by
5 posts views Thread by Chris | last post: by
17 posts views Thread by =?Utf-8?B?U2hhcm9u?= | last post: by
5 posts views Thread by Immortal Nephi | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.