473,378 Members | 1,314 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 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 5059
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

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

Similar topics

2
by: James | last post by:
Hi, I'm hoping someone can help me out. If I declare a class, eg. class CSomeclass { public: var/func etc..... private varfunc etc..
67
by: Ike Naar | last post by:
Hi, Asking your advice on the following subject: Suppose I want to find out whether a given pointer (say, p) of type *T points to an element of a given array (say, a) of type T. A way to...
8
by: Peter B. Steiger | last post by:
The latest project in my ongoing quest to evolve my brain from Pascal to C is a simple word game that involves stringing together random lists of words. In the Pascal version the whole array was...
7
by: Frank M. | last post by:
I'm trying to declare an array of pointers to structures so that I can make the last element a NULL pointer. I figure that it would more easily allow my library routines to know when to stop...
5
by: Chris | last post by:
Hi, to create an array of 2 objects (e.g. of type '__gc class Airplane') I need to do : Airplane * arrAirplanes __gc = new Airplane* __gc; arrAirplanes = new Airplane("N12344"); arrAirplanes...
23
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these...
3
by: Scotty | last post by:
I'm a C++ novice and need help figuring out some odd behavior I've encountered. Here's the situation: I create a class and have its constructor store a random number in a private "number"...
17
by: =?Utf-8?B?U2hhcm9u?= | last post by:
Hi Gurus, I need to transfer a jagged array of byte by reference to unmanaged function, The unmanaged code should changed the values of the array, and when the unmanaged function returns I need...
2
by: StevenChiasson | last post by:
For the record, not a student, just someone attempting to learn C++. Anyway, the problem I'm having right now is the member function detAddress, of object controller. This is more or less, your...
5
by: Immortal Nephi | last post by:
I would like to design an object using class. How can this class contain 10 member functions. Put 10 member functions into member function pointer array. One member function uses switch to call...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.