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)*FEDTAXRAT E;}
float calcState(void){return (fedTax * STATETAXRATE);}
float calcSSI(void){return (gross-deffered)*SSITAXRAT E;}
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.def fered),gro ss(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{ret urn lname;}
char* getFirstName(void)const{re turn fname;}
const float getDeffered(void)const{ret urn deffered;}
const float getGross(void)const{return gross;}
const float getNet(void)const{return net;}
const float getFedTax(void)const{retur n fedTax;}
const float getStateTax(void)const{ret urn stateTax;}
const float getSSITax(void)const{retur n 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+ssi Tax+deffer ed);
}
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(t his->getLa stName(), last));
len = strlen(first);
this->setFirstName(new char[len+1]);
this->setFirstName(strcpy( this->getF irstName() , first));
(deff<0)?this->setDeffered (0):this-> setDeffere d(deff);
(hr<0)?hours=0:hours=hr;
(pr<0)?payrate=0:payrate=p r;
}
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(t his->getLa stName(), last));
len = strlen(first);
this->setFirstName(new char[len+1]);
this->setFirstName(strcpy( this->getF irstName() , first));
(deff<0)?this->setDeffered (0):this-> setDeffere d(deff);
(NumPieces<0)?pieces=0:pie ces=NumPie ces;
(ppp<0)?payPerPiece=0:payP erPiece=pp p;
}
void calcGross(){this->setGross (pieces*pa yPerPiece) ;}
};
========================== ========== ========== ==========
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(t his->getLa stName(), last));
len = strlen(first);
this->setFirstName(new char[len+1]);
this->setFirstName(strcpy( this->getF irstName() , first));
(deff<0)?this->setDeffered (0):this-> setDeffere d(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,delet es
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.EmployeeCo unt), EmployeeSize(e.EmployeeSiz e)
{
allEmployees= new Employee[e.EmployeeSize];
for(int i=0;i<EmployeeSize;i++)
allEmployees[i]=e.allEmplo yees[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,Emp loyeeCount -1);}
void printPayrollReport(void);
const int getEmployeeCount(void)cons t{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].getLastNam e())==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].getLast Name(); // sorting by last name
do
{ while((strcmp(allEmployees [i].getLas tName(),x) < 0) && (i < right))i++;
while((strcmp(allEmployees [j].getLas tName(),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[--EmployeeCou nt] = empty; // set it to a new employee
}
}
Thanks so much for your help
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)*FEDTAXRAT
float calcState(void){return (fedTax * STATETAXRATE);}
float calcSSI(void){return (gross-deffered)*SSITAXRAT
public:
Employee(const char*last="",const char* first="",float deff=0.0)
:lname(NULL), fname(NULL),deffered(deff)
{
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.def
{ 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{ret
char* getFirstName(void)const{re
const float getDeffered(void)const{ret
const float getGross(void)const{return
const float getNet(void)const{return net;}
const float getFedTax(void)const{retur
const float getStateTax(void)const{ret
const float getSSITax(void)const{retur
//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+ssi
}
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(t
len = strlen(first);
this->setFirstName(new char[len+1]);
this->setFirstName(strcpy(
(deff<0)?this->setDeffered
(hr<0)?hours=0:hours=hr;
(pr<0)?payrate=0:payrate=p
}
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(t
len = strlen(first);
this->setFirstName(new char[len+1]);
this->setFirstName(strcpy(
(deff<0)?this->setDeffered
(NumPieces<0)?pieces=0:pie
(ppp<0)?payPerPiece=0:payP
}
void calcGross(){this->setGross
};
==========================
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(t
len = strlen(first);
this->setFirstName(new char[len+1]);
this->setFirstName(strcpy(
(deff<0)?this->setDeffered
(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
};
==========================
/ Dynamic Container Class stores Employees,sorts,adds,delet
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.EmployeeCo
{
allEmployees= new Employee[e.EmployeeSize];
for(int i=0;i<EmployeeSize;i++)
allEmployees[i]=e.allEmplo
}
~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,Emp
void printPayrollReport(void);
const int getEmployeeCount(void)cons
};
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
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
}
}
}
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].getLastNam
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)/
do
{ while((strcmp(allEmployees
while((strcmp(allEmployees
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[--EmployeeCou
}
}
Thanks so much for your help
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
ASKER
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){}
}
}
void calcAllGross(void)
{
for(int i=0;i<EmployeeCount;i++)
{
allEmployees[i].calcGross(
// i.e. virtual void calcGross(void){}
}
}
ASKER
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;
so i guess i need to change it to
virtual void calcGross(void)=0;
ASKER
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.
ASKER