Base class Employee with some derived classes like HourlyWorker, PieceWorker, etc. One virtual function: void print.
Problem is that with only just the method declarations I keep getting error messages about "undefined reference to vtable" when I am trying to define the various constructors. From what little I know, I understand vtable is an array of pointers to virtual functions that cpp has in the background and the only way I have found so far to get around this error message is to define the virtual functions alongside their declarations but obviously its not a very robust or long-term solution.
So any suggestions about how I can keep the all function declarations, including virtual functions, separate and then define them together later would be most welcome. My code is below. Many thanks.
Code:
#include<iostream>
#include<cstring>
using namespace std;
class Employee{
private:
enum {LIM = 20};
char m_first_name[LIM];
char m_last_name[LIM];
public:
Employee(const char* first_name = "none", const char* last_name = "none");
virtual ~Employee(){};
virtual void print(){cout<<m_first_name<<" "<<m_last_name<<endl;};
};
class HourlyWorker: public Employee{
private:
float m_hw_weekly_hours;
float m_hw_hourly_rate;
public:
HourlyWorker(const char* first_name = "none", const char* last_name = "none", float hw_weekly_hours = 0.0, float hw_hourly_rate = 0.0);
virtual ~HourlyWorker(){};
virtual void print(){};
};
class PieceWorker: public Employee{
private:
int m_pw_num_items;
float m_pw_item_rate;
public:
PieceWorker(const char* first_name = "none", const char* last_name = "none", int pw_num_items = 0, float pw_item_rate = 0.0);
virtual ~PieceWorker(){};
virtual void print(){};
};
class BossWorker: public Employee{
private:
float m_bw_weekly_salary;
public:
BossWorker(const char* first_name = "none", const char* last_name = "none", float bw_weekly_salary = 0.0);
virtual ~BossWorker(){};
virtual void print(){};
};
class CommissionWorker: public Employee{
private:
float m_cw_weekly_salary;
float m_cw_gross_sales;
float m_cw_comm_percent;
public:
CommissionWorker(const char* first_name = "none", const char* last_name = "none", float cw_weekly_salary = 0.0,
float cw_gross_sales = 0.0, float cw_comm_percent = 0.0);
virtual ~CommissionWorker(){};
virtual void print(){};
};
int main(){
Employee emp("John", "Smith");
emp.print();
}
Employee::Employee(const char* first_name, const char* last_name){
strncpy(m_first_name,first_name,LIM-1);
m_first_name[LIM-1]='\0';
strncpy(m_last_name,last_name,LIM-1);
m_last_name[LIM-1]='\0';
}
HourlyWorker::HourlyWorker(const char* first_name, const char* last_name, float hw_weekly_hours, float hw_hourly_rate)
:Employee(first_name, last_name){
m_hw_weekly_hours = hw_weekly_hours;
m_hw_hourly_rate = hw_hourly_rate;
}
PieceWorker::PieceWorker(const char* first_name, const char* last_name, int pw_num_items, float pw_item_rate)
:Employee(first_name, last_name){
m_pw_num_items = pw_num_items;
m_pw_item_rate = pw_item_rate;
}
BossWorker::BossWorker(const char* first_name, const char* last_name, float bw_weekly_salary)
:Employee(first_name, last_name){
m_bw_weekly_salary = bw_weekly_salary;
}
CommissionWorker::CommissionWorker(const char* first_name, const char* last_name, float cw_weekly_salary, float cw_gross_sales, float cw_comm_percent)
:Employee(first_name, last_name){
m_cw_weekly_salary = cw_weekly_salary;
m_cw_gross_sales = cw_gross_sales;
m_cw_comm_percent = cw_comm_percent;
}