Solved

Need Help with Inheritence and containers

Posted on 2006-11-19
7
210 Views
Last Modified: 2010-04-01
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
0
Comment
Question by:imlearning
  • 5
  • 2
7 Comments
 
LVL 19

Accepted Solution

by:
mrwad99 earned 500 total points
ID: 17977882
>> 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
 

Author Comment

by:imlearning
ID: 17979606
Ive tried that, that causes more problems
0
 

Author Comment

by:imlearning
ID: 17980363
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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

Author Comment

by:imlearning
ID: 17980558
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
 

Author Comment

by:imlearning
ID: 17980630
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
 

Author Comment

by:imlearning
ID: 17981616
i firgured it out thanks..
0
 
LVL 19

Expert Comment

by:mrwad99
ID: 17985949
Glad you got it sorted.  Sorry I did not reply to your later questions; we are obviousy in a different time zone.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Quicksort a dynamic deque 33 73
Move constructor only called if marked noexcept? 6 108
Problem with SqlConnection 4 177
FMX TCameraComponent Problem 2 82
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question