Need Help with Inheritence and containers

Hi everyone,
I need some help with homework please.

I have an Employee class and an Employees(container) class that sorted,added,deleted,and printed a simple payroll report.

My next assignment was to derive three classes  Hourly,Salaried,Piecework from class Employee. (done)

Now im trying to figure out how to incorperate the new derived types into the container.  as it stands now I cant becuase if I do this

Employee *pEmp = new Hourly(......);
Employees company;
company.add(pEmp) <--- this will not work it( plase see my container code)

Is there a way to incorperate my existing container with the derived classes? if not how would my data scheme be for a new one.?

Here is my code
===========================================
class Employee {
  private:
     char *lname;
     char *fname;
     float deffered;
     float gross;
     float net;
     float fedTax;
     float stateTax;
     float ssiTax;
     float calcFed(void) {return (gross-deffered)*FEDTAXRATE;}
     float calcState(void){return (fedTax * STATETAXRATE);}
     float calcSSI(void){return (gross-deffered)*SSITAXRATE;}

  public:
     Employee(const char*last="",const char* first="",float deff=0.0)
     :lname(NULL), fname(NULL),deffered(deff),gross(0), net(0), fedTax(0), stateTax(0), ssiTax(0)
     {
        int len = strlen(last);
        lname = new char[len+1];
        strcpy(lname, last);
        len = strlen(first);
        fname = new char[len+1];
        strcpy(fname, first);

      }
      ~Employee()//destruct
      {
          delete [] fname;
          delete [] lname;
      }
     Employee(const Employee& e) //copy
     :lname(NULL), fname(NULL),deffered(e.deffered),gross(e.gross), net(e.net), fedTax(e.fedTax),stateTax(e.stateTax), ssiTax(e.ssiTax)
     {  int len = strlen(e.lname);
        lname = new char[len+1];
        strcpy(lname, e.lname);
        len = strlen(e.fname);
        fname = new char[len+1];
        strcpy(fname, e.fname);
     }
     //Get services
     char* getLastName(void)const{return lname;}
     char* getFirstName(void)const{return fname;}
     const float getDeffered(void)const{return deffered;}
     const float getGross(void)const{return gross;}
     const float getNet(void)const{return net;}
     const float getFedTax(void)const{return fedTax;}
     const float getStateTax(void)const{return stateTax;}
     const float getSSITax(void)const{return ssiTax;}
     
     //change services
     void setLastName(const char * last){ int len =strlen(last);lname = new char[len+1];strcpy(lname, last);}
     void setFirstName(const char * first){int len =strlen(first);fname = new char[len+1];strcpy(fname, first);}
     void setDeffered(const float def){deffered=def;}
     void setGross(const float f){gross=f;}

     virtual void calcGross(void){}
   
     void calcNet(void);
     Employee operator = (const Employee &e); //overloaded = for swapping

};
void Employee::calcNet(void)
{
     this->fedTax = calcFed();
     this->stateTax = calcState();
     this->ssiTax = calcSSI();
     this->net = gross-(fedTax+stateTax+ssiTax+deffered);
  }
 

Employee Employee::operator =(const Employee &e)
{
  int len = strlen(e.fname);
  this->fname = new char[len+1];
  strcpy(fname, e.fname);
  len = strlen(e.lname);
  this->lname = new char[len+1];
  strcpy(lname, e.lname);
  this->deffered=e.deffered;
  this->gross=e.gross;
  this->fedTax=e.fedTax;
  this->ssiTax=e.ssiTax;
  this->stateTax=e.stateTax;
  this->net=e.net;
  return(e);
}
==================================================
class Hourly : public  Employee{

   private:
      float hours;
      float payrate;
   public:
      Hourly(char * last="",char *first="",float pr=0,float hr=0,float deff=0)
      {
         int len = strlen(last);
         this->setLastName(new char[len+1]);
         this->setLastName(strcpy(this->getLastName(), last));
         len = strlen(first);
         this->setFirstName(new char[len+1]);
         this->setFirstName(strcpy(this->getFirstName(), first));
         (deff<0)?this->setDeffered(0):this->setDeffered(deff);
         (hr<0)?hours=0:hours=hr;
         (pr<0)?payrate=0:payrate=pr;
      }
      void calcGross()
      {
         if(hours <= 40)
          this->setGross(hours * payrate);
         else
          this->setGross( 40 * payrate + (hours-40)* 1.5 * payrate);
      }
};
===========================================================
class PieceWork : public  Employee{
   private:
      float pieces;
      float payPerPiece;
   public:
      PieceWork(char * last="",char *first="",float NumPieces=0,float ppp=0,float deff=0)
      {
         int len = strlen(last);
         this->setLastName(new char[len+1]);
         this->setLastName(strcpy(this->getLastName(), last));
         len = strlen(first);
         this->setFirstName(new char[len+1]);
         this->setFirstName(strcpy(this->getFirstName(), first));
         (deff<0)?this->setDeffered(0):this->setDeffered(deff);
         (NumPieces<0)?pieces=0:pieces=NumPieces;
         (ppp<0)?payPerPiece=0:payPerPiece=ppp;
      }
      void calcGross(){this->setGross(pieces*payPerPiece);}
};
========================================================
class Salary : public  Employee{

   private:
      float salary;
   public:
      Salary(void){salary=0;}
      Salary(char * last="",char *first="",float f=0,float deff=0)
      {
         int len = strlen(last);
         this->setLastName(new char[len+1]);
         this->setLastName(strcpy(this->getLastName(), last));
         len = strlen(first);
         this->setFirstName(new char[len+1]);
         this->setFirstName(strcpy(this->getFirstName(), first));
         (deff<0)?this->setDeffered(0):this->setDeffered(deff);
         (f<0)?salary=0:salary=f;
      }
      void setSalary(const float f){(f<0)?salary=0:salary=f;}
      const float getSalary()const{return salary;}
      void calcGross(){this->setGross(salary);}
};
========================================================
/ Dynamic Container Class stores Employees,sorts,adds,deletes
class Employees{
  private:
     Employee* allEmployees;
     int EmployeeCount;
     int EmployeeSize;
     void qs_lname(int left, int right);
  public:
     Employees(void) : allEmployees(NULL), EmployeeCount(0), EmployeeSize(0)
     {allEmployees= new Employee[EmployeeSize];}
     Employees(Employees& e) : allEmployees(NULL), EmployeeCount(e.EmployeeCount), EmployeeSize(e.EmployeeSize)
     {
      allEmployees= new Employee[e.EmployeeSize];
      for(int i=0;i<EmployeeSize;i++)
         allEmployees[i]=e.allEmployees[i];
     }
     ~Employees(void){delete [] allEmployees;}
     //change state
     void add(const Employee& e);
     void remove(const Employee &e);
     float search(const char* last);
     void quicksort(){qs_lname(0,EmployeeCount-1);}
     void printPayrollReport(void);
     const int getEmployeeCount(void)const{return EmployeeCount;}
};
void Employees::add(const Employee &e)
{
  if(e.getNet()>0)
  {
  int found=search(e.getLastName());
  if (found >= 0)
     allEmployees[found] = e;   // update the entry found
  else
  {   if (EmployeeCount < EmployeeSize)
         allEmployees[EmployeeCount++] = e;
      else
      {
         EmployeeSize += 32;//set the array to 32
         Employee* all = new Employee[EmployeeSize];
         for (int i = 0; i <  EmployeeCount; ++i)
            all[i] = allEmployees[i];
         delete [] allEmployees;
         allEmployees = all;
         allEmployees[EmployeeCount++] = e;
   }
 }
}
else
{cout<< "Error: net is not calculated";
}
}
float Employees::search(const char* last)
{  for(int i=0;i<EmployeeSize;i++)
  {   if(strcmp(last, allEmployees[i].getLastName())==0)
         return i;
  }
  return-1;
}

void Employees::qs_lname(int left, int right)
{  int i, j;
  const char *x; // temp string
  i = left; j = right;
  x = allEmployees[(left+right)/2].getLastName(); // sorting by last name
  do
  {  while((strcmp(allEmployees[i].getLastName(),x) < 0) && (i < right))i++;
     while((strcmp(allEmployees[j].getLastName(),x) > 0) && (j > left)) j--;
     if(i <= j)
     { // swap
        Employee temp =allEmployees[i];
        allEmployees[i] = allEmployees[j];
        allEmployees[j] = temp;
        i++; j--;
     }
   } while(i <= j);
   if(left < j) qs_lname(left, j);
   if(i < right) qs_lname(i, right);
}
void Employees::remove(const Employee &e)
{
  int found=search(e.getLastName());
  if (found >= 0)
  {
     char erased[] = { char(255), '\0' };  
     Employee erase(erased);
     allEmployees[found] = erase;// set it to a new employee
     quicksort();
     Employee empty;
     allEmployees[--EmployeeCount] = empty;   // set it to a new employee
  }
}


Thanks so much for your help
imlearningAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
mrwad99Connect With a Mentor Commented:
>> Employee *pEmp = new Hourly(......);
>> Employees company;
>> company.add(pEmp) <--- this will not work it( plase see my container code)

You are trying to add a pointer; your code expects a reference:

void add(const Employee& e);

Change your code to be like this:

>> Employee *pEmp = new Hourly(......);
Employees company;
company.add(*pEmp) // Dereference the pointer, then add it
0
 
imlearningAuthor Commented:
Ive tried that, that causes more problems
0
 
imlearningAuthor Commented:
actually im sorry your right it seems to be working now thank you, one more question i have to produce a new payroll report that groups hourlys,pieceworks,salarys to do this it seems like a would have to add a new member to the Employee class, but is than an easier way like in java you could use instanceof
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
imlearningAuthor Commented:
wait im sorry again It doesnt work, because im making a method in my container called calcAllGross

void calcAllGross(void)
 {
         for(int i=0;i<EmployeeCount;i++)
         {
            allEmployees[i].calcGross(); //<--- this gets called and and it goes to the Employee Class calcGross,  not the dervied class  calcGross
                                                    // i.e.          virtual void calcGross(void){}
                                                         
         }
 }
0
 
imlearningAuthor Commented:
I forgot to mention also that class Employee is suppose to be an abstract class

so i guess i need to change it to

virtual void calcGross(void)=0;
0
 
imlearningAuthor Commented:
i firgured it out thanks..
0
 
mrwad99Commented:
Glad you got it sorted.  Sorry I did not reply to your later questions; we are obviousy in a different time zone.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.