Link to home
Start Free TrialLog in
Avatar of imlearning
imlearning

asked on

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
ASKER CERTIFIED SOLUTION
Avatar of mrwad99
mrwad99
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of imlearning
imlearning

ASKER

Ive tried that, that causes more problems
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
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){}
                                                         
         }
 }
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;
i firgured it out thanks..
Glad you got it sorted.  Sorry I did not reply to your later questions; we are obviousy in a different time zone.