Array of pointers to objects

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Gannon56789
    New Member
    • Nov 2008
    • 7

    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?

    Code:
             
    Employee* Earray[10];
    
    Payroll()
    {
        for ( int i = 0; i < 10; i++)
        {
        Earray = new Employee  //can't do because employee is abstract
        }
    }
    
    
    ~Payroll()
    {
       for ( int i = 0; i <10; i++)
       {
              delete Earray[i];
       }
    }
  • arnaudk
    Contributor
    • Sep 2007
    • 425

    #2
    Originally posted by Gannon56789
    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.

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      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

      Comment

      • Gannon56789
        New Member
        • Nov 2008
        • 7

        #4
        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.

        Code:
        Employee* Earray[10];
        Payroll()
        {
            for ( int i = 0; i < 10; i++ )
            {
                Earray = add(*Employee);
            }
        }
        
        add(Employee&)
        {
            
        }

        Comment

        • arnaudk
          Contributor
          • Sep 2007
          • 425

          #5
          I think Employee * Earray needs to be a (private) member of the Payroll class. Then you allocate memory for it in the initializer list:
          [code=cpp]
          Payroll() : Earray = new Employee[10] { }
          ~Payroll() { delete [] Earray; }
          [/code]The Payroll also needs an add() method as Jos mentions to subsequently add employees to the payroll.

          Comment

          • Gannon56789
            New Member
            • Nov 2008
            • 7

            #6
            Thank you guys that helpped me a lot

            Comment

            • weaknessforcats
              Recognized Expert Expert
              • Mar 2007
              • 9214

              #7
              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.

              Comment

              • Gannon56789
                New Member
                • Nov 2008
                • 7

                #8
                So it would be like this? I appreciate the help.

                Code:
                Employee* Earray[10];   // Private member of payroll class
                Payroll()
                {
                   for ( int i = 0; i <10; i++)
                   { 
                        Earray[i] = 0;
                   }
                
                }
                
                ~Payroll()
                {
                     for ( int i = 0; i <10; i++)
                     {
                
                            delete [] Earray;
                     }
                
                Add(*Employee)
                {
                    
                     for ( int i =0; i < 10; i++)
                     {
                     cout << "Input Employee name" <<endl;
                     STRING S;   // STRING type holds strings
                     cin >> S;
                     Earray[i] = S;
                     }  
                }

                Comment

                • boxfish
                  Recognized Expert Contributor
                  • Mar 2008
                  • 469

                  #9
                  Code:
                  for ( int i = 0; i <10; i++)
                  { 
                      delete [] Earray; 
                  }
                  You don't need to delete Earray ten times. A loop like this is only nessecary with a multidimensiona l 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]

                  Comment

                  • Gannon56789
                    New Member
                    • Nov 2008
                    • 7

                    #10
                    Would this work?
                    Code:
                    Employee* Earray[10];    //Private member of payroll
                    Payroll() 
                    { 
                       Earray = new *Employee[10];
                       for ( int i = 0; i <10; i++) 
                       {  
                            Earray[i] = 0; 
                       } 
                      
                    } 
                      
                    ~Payroll() 
                    { 
                        
                      
                                delete [] Earray; 
                         
                      
                    Add(*Employee) 
                    { 
                      
                         for ( int i =0; i < 10; i++) 
                         { 
                         cout << "Input Employee name" <<endl; 
                         STRING S;   // STRING type holds strings 
                         cin >> S; 
                         Earray[i] = S; 
                         }   
                    }

                    Comment

                    • Ganon11
                      Recognized Expert Specialist
                      • Oct 2006
                      • 3651

                      #11
                      *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:

                      Code:
                      Earray = new *Employee[10];
                      is just wrong. You want:

                      Code:
                      Earray = new Employee*[10];
                      Similarly for your Add function...whic h doesn't have a return value right now.

                      Comment

                      • Gannon56789
                        New Member
                        • Nov 2008
                        • 7

                        #12
                        Thank you. Will this work now?

                        Code:
                        Employee* Earray[10];    //Private member of payroll
                        Payroll() 
                        { 
                           Earray = new Employee*[10];
                           for ( int i = 0; i <10; i++) 
                           {  
                                Earray[i] = 0; 
                           } 
                          
                        } 
                          
                        ~Payroll() 
                        { 
                            delete [] Earray; 
                        }  
                        
                        Add(Employee*) 
                        { 
                          
                             for ( int i =0; i < 10; i++) 
                             { 
                             cout << "Input Employee name" <<endl; 
                             STRING S;   // STRING type holds strings 
                             cin >> S; 
                             return  Earray[i] = S; 
                             }   
                        }

                        Comment

                        • Ganon11
                          Recognized Expert Specialist
                          • Oct 2006
                          • 3651

                          #13
                          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.

                          Comment

                          • Ganon11
                            Recognized Expert Specialist
                            • Oct 2006
                            • 3651

                            #14
                            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

                            Comment

                            Working...