Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Need Help with Inheritence and containers

Posted on 2006-11-19
7
Medium Priority
?
237 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
7 Comments
 
LVL 19

Accepted Solution

by:
mrwad99 earned 1500 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

721 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