I am writing a C++ program about an Employee class about classes and multiple in
ID: 3764469 • Letter: I
Question
I am writing a C++ program about an Employee class about classes and multiple inheritances but I keep getting an error when I try to run it. Please help me identify and solve this error.
Any tips or solutions will be kindly accepted.
NOTE: Please answer this question correctly, I've been getting spam as answers and it is wasting my points and costing me money.
The error is displayed below
This is the code below:
#include
#include
#define FACULTY_MONTHLY_SALARY 5000.00
#define STAFF_MONTHLY_HOURS_WORKED 160
using namespace std;
class Date
{
public:
int d;
int m;
int y;
public:
Date()
{
d=1;
m=1;
y=2010;
}
Date(int d1,int m1,int y1)
{
d=d1;
m=m1;
y=y1;
}
Date(const Date &s)
{
d=s.d;
m=s.m;
y=s.y;
}
};
class Employee
{
private:
string lname;
string fname;
string ID;
char sex;
Date *Bdate;
public:
Employee()
{
lname=" ";
fname=" ";
ID="";
sex='F';
Bdate=new Date(1,1,2010);
}
Employee(const string& ln,const string& fn,const string& id,char s,Date m1)
{
lname=ln;
fname=fn;
ID=id;
sex=s;
Bdate=new Date(m1.d,m1.m,m1.y);
}
void setempfName(const string& f)
{
fname=f;
}
void getemplname(const string& l)
{
lname=l;
}
void getID(const string& id)
{
ID=id;
}
string getempfName()
{
return fname;
}
string getemplname()
{
return lname;
}
string getID()
{
return ID;
}
void putData() const
{
cout<<"Employee Name:"<
cout<<"Employee id:"<
cout<<"Sex:"<
cout<<"Birth Date:"
}
virtual double monthlyEarning()=0;
};
class Staff:public Employee
{
double hourlyRate;
public:
Staff():Employee()
{
hourlyRate=1;
}
Staff(const string& l,const string& f,const string& id,char s,Date m,double rate):Employee(l,f,id,s,m)
{
setHourlyRate(1);
}
void setHourlyRate(double rate)
{
hourlyRate=rate;
}
double getHourlyRate()
{
return hourlyRate;
}
double monthlyEarning()
{
return hourlyRate*160;
}
void putData()
{
cout<<" STAFF INFORMATION:"<
Employee::putData();
cout<<" Monthly Earning:"<< monthlyEarning() <
}
};
class Education
{
string degree;
string major;
int research;
public:
Education()
{
degree="MS";
major="Engg";
research=1;
}
Education(const string& d,const string& m,int r)
{
degree=d;
major=m;
research=r;
}
void setDegree(const string& d)
{
degree=d;
}
string getDegree()const
{
return degree;
}
void setMajor(const string& m)
{
major=m;}
string getMajor()const
{
return major;
}
void setResearch(int r)
{
research=r;
}
int getResearch()
{
return research;
}
};
class Faculty:public Employee
{
string level;
Education *edu;
public:
Faculty():Employee()
{
level="AS";
edu=new Education();
}
Faculty(const string& l,const string& f,const string& id,char s,Date b,const string& lvl,Education e):Employee(l,f,id,s,b)
{
level=lvl;
edu= new Education(e.getDegree(), e.getMajor(), e.getResearch());
}
double monthlyEarning()
{
if(level=="AS")
return FACULTY_MONTHLY_SALARY;
else if(level=="AO")
return FACULTY_MONTHLY_SALARY*1.2;
else if(level=="FU")
return FACULTY_MONTHLY_SALARY*1.4;
return 0;
}
void putData()
{
cout<<"Faculty information:"<
Employee::putData();
cout<
cout<<"Monthly Earning:"<
}
};
class Parttime:public Staff
{
int hoursWorked;
public:
Parttime(const string& l,const string& f,const string& id,char s,Date m,double rate,int wh):Staff(l,f,id,s,m,rate)
{
setHoursWorked(wh);
}
void setHoursWorked(int wh)
{
hoursWorked=wh;
}
double monthlyEarning()
{
double r;
r=Staff::getHourlyRate();
return r*hoursWorked*4;
}
void putData()
{
cout<<"PARTTIMESTAFF INFORMATION:"<
Staff::putData();
cout<<"HOURS WORKS PER MONTH:"<
cout<<"Monthly Earning:"<
}
};
int main()
{
Employee * ePtr[10];
Staff s1;
ePtr[0] = new Staff("John","jerry","12345",'F',new Date(1,12,1965),10);
Staff s2("jerry","K","11111",'M',new Date(1,5,1987),12);
e[0]=s1;
e[1]=s2;
Faculty f1("John","jerry","135",'F',new Date(1,2,1985),"AS",new Education("MS","Engineering",12));
Faculty f2("jai","k","122",'M',new Date(1,3,1995),"AO",new Education("MD","Engineering",8));
e[2]=f1;
e[3]=f2;
Parttime p1("w","charles","133",'M',new Date(1,8,1990),100,4);
Parttime p2("jai","k","155",'F',new Date(1,3,1981),120,6);
e[4]=p1;
e[5]=p2;
for(k1=0;k1<10;k1++)
{
e[k1].putData();
}
s1.putData();
s2.putData();
f1.putData();
f2.putData();
p1.putData();
p2.putData();
}
Explanation / Answer
Header File
-------------------------
The header file compclas.h contains definitions of classes that are used in the main program comprec.cpp. See Main Program Source File.
--------------------------------------------------------------------------------
// compclas.h -- class definitions for comprec.cpp ***
-------------------------------------------------------------------------------
#ifndef _COMPCLAS_H
# define _COMPCLAS_H 1
// Abstract base class definition
class employee {
private:
char * n;
int x;
employee( const employee & lhs);
employee& operator =( const employee & lhs);
protected:
const char * name() { return n; }
int employee_id () { return x; }
public:
// Constructors for class employee.
employee() : n(""), x(0) {};
employee(char * n, int id);
virtual ~employee ();
virtual double pay() =0;
virtual void dump() =0;
};
// End of abstract base class definition
// Derived class definitions.
// Derive a class manager from class employee
class manager : virtual public employee {
protected:
double salary;
manager(double sal);
public:
// Constructors for class manager.
manager(char *n, int id, double sal);
// Member functions for class manager.
double pay();
friend ostream& operator << (ostream& out, manager & lhs);
void dump() { cout << *this; }
};
// End of manager class definition.
// Derive a class regular_emp from class employee
class regular_emp : virtual public employee {
private:
double wage, hours;
public:
// Constructor
regular_emp(char *n, int id, double wg, double hrs) ;
// Member functions
double pay();
friend ostream& operator << (ostream& out, regular_emp & lhs);
void dump() { cout << *this; }
};
// End of regular_emp class definition.
// Derive a class sales_person from class employee
class sales_person : virtual public employee {
protected:
double commission;
int units;
sales_person(double com, double nts);
public:
// Constructors
sales_person(char *n, int id,
double com, double nts);
// Member functions
double pay();
friend ostream& operator << (ostream& out, sales_person & lhs);
void dump() { cout << *this; }
};
// End of sales_person class definition.
// Derive a class sales_mgr from the manager and sales_person classes
class sales_mgr : public manager, public sales_person {
public:
// Constructors
sales_mgr(char *n, int id, double sal, double comm, double nts);
// Member functions
double pay();
friend ostream& operator << (ostream& out, sales_mgr & lhs) ;
void dump() { cout << *this; }
};
// End of sales_mgr class definition.
#endif
------------------------------
C++ Source File
---------------------------------------
The file compfunc.cpp contains the function definitions for the member functions of the classes that are used in the main program comprec.cpp. See Main Program Source File.
-------------------------------------------------
//compfunc.cpp -- definitions for class member functions
----------------------------------------------------------------------------------
#include <string.h>
#include <iostream.h>
#include "compclas.h"
// Constructor definition for class manager.
employee::employee(char * name, int id)
: n (new char [strlen (name) + 1 ]), x(id) {
strcpy (n, name);
};
employee:: ~employee() { delete [] n; }
// Note different way to initialize.
manager::manager(char *n, int id, double sal)
: employee(n, id), salary(sal){}
manager::manager(double sal)
: salary(sal){}
// Member function definitions for class manager
double manager::pay() {
return salary/12 ;
}
ostream& operator << (ostream& out, manager & lhs) {
out << endl << lhs.name()
<< " is a manager with employee number " << lhs.employee_id () << endl
<< "makes " << lhs.salary << " per year." << endl
<< lhs.manager::name() << " made " << lhs.pay() << " dollars this month."
<< endl << endl;
return out;
}
// Constructor for class regular_emp.
regular_emp::regular_emp(char *n, int id, double wg, double hrs)
: employee(n, id), wage(wg), hours(hrs) {}
// Member function definitions for class reg_emp
double regular_emp::pay() {
return wage*hours;
}
ostream& operator << (ostream& out, regular_emp & lhs) {
out << endl << lhs.name() << " is a regular employee with employee number "
<< lhs.employee_id() << endl
<< "makes " << lhs.wage << " dollars per hour" << endl
<< "and worked " << lhs.hours << " hours this month. " << endl
<< lhs.name() <<" made " << lhs.pay()
<< " dollars this month." << endl << endl;
return out;
}
// Constructor definition for class sales_person.
sales_person::sales_person(char *n, int id,
double comm, double nts)
:employee(n, id), commission(comm), units(nts){}
sales_person::sales_person(double comm, double nts)
: commission(comm), units(nts){}
// Member function definitions for class sales_person.
double sales_person::pay() {
return commission*units;
}
ostream& operator << (ostream& out, sales_person & lhs) {
out << endl << lhs.name() << " is a sales person with employee number "
<< lhs.employee_id() << endl
<< "works for a straight commission of "
<< lhs.commission << endl
<< "dollars per unit sold and sold " << lhs.units
<< " units this month." << endl
<< lhs.name() <<" made " << lhs.pay()
<< " dollars this month." << endl << endl;
return out;
}
// Constructor for class sales_mgr
sales_mgr::sales_mgr(char *n, int id,
double sal, double comm, double nts)
:employee(n, id),
manager(sal),
sales_person(comm, nts) { }
// Member function definitions for class sales_mgr.
double sales_mgr::pay(){
return manager::pay() + sales_person::pay()
;}
ostream& operator << (ostream& out, sales_mgr & lhs) {
out << endl << lhs.name() << " is a sales manager with employee number "
<< lhs.employee_id() << endl
<< "makes " << lhs.salary << " per year and earns a commission of "
<< lhs.commission << " dollars per unit sold." << endl
<< lhs.name() << " was responsible for sales of "
<< lhs.units << " units this month." << endl
<< lhs.name() <<" made " << lhs.pay()
<< " dollars this month."<< endl << endl;
return out;
}
------------------------------------------
Main Program Source File
--------------------------------
The C++ source file comprec.cpp contains the logic of this program.
-----------------------------------------------------------------------------------------------
//comprec.cpp
#include <iostream.h>
#include <strstrea.h>
#include "compclas.h"
#include "compfunc.cpp" // for the inline functions
static void payout(double);
static void payout(double, double);
static void payout(double, double, double);
inline void title() {
cout << "Monthly Employee Pay Report" << endl << endl;
};
// Start of main function
const char * pwcharset = "abcdefghijklmnopqrstuvwxyz"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"0123456789 ";
int main() {
const double managers_pay = 1510.35;
const double reg_emp_pay = 25.75;
const double reg_emp_hrs = 40.00;
const double sales_mgr_units = 200;
const double monthly_salary = 800.00;
const double commission = 1.00;
const double units = 150;
cout.setf(ios::fixed);
cout.precision(2);
title();
// Optionally, build in a security check
char val [512] ;
cout << "To view data on employees, "
<< "type your password (mona) and press Enter:" << endl;
cin >> val ;
char * p = val;
char * where;
int check = 32679;
while (*p) {
where = strchr(pwcharset,*p);
if (where) check *= (where-pwcharset+7);
++p;
}
check &= 32767;
if (check != 9196) {
cout << "Wrong password, bye." << endl;
// cout << "check is " << check << endl;
return 999;
}
payout(managers_pay);
payout(reg_emp_pay, reg_emp_hrs);
payout(monthly_salary, commission, units);
// Define instances of classes and call their member functions
manager smith("Jack Smith", 123, 28020);
cout << smith << endl;
regular_emp james("Everett James", 456, 12,160) ;
cout << james << endl;
sales_person doe("Jackson Doe", 101, 31,65);
cout << doe << endl;
sales_mgr stevens("Jennifer Stevens", 789, 28000, 4, 105) ;
cout << stevens << endl;
double sal1, sal2, sal3, sal4;
sal1 = smith.pay();
sal2 = james.pay();
sal3 = doe.pay();
sal4 = stevens.pay();
// Use the values returned from the pay functions.
cout << "Total wages paid to these employees was: "
<< (sal1+sal2+sal3+sal4)
<< " dollars" << endl << endl ;
// Alternate method of defining instances of a class
employee * ep[] = {
new manager ("Jack Smith", 123, 28020),
new regular_emp ("Everett James", 456, 12,160),
new sales_person ("Jackson Doe", 101, 31,65),
new sales_mgr ("Jennifer Stevens", 789, 28000, 4, 105),
0
} ;
double sal=0;
for (int i=0; ep[i]; ++i) {
ep[i]->dump();
sal+=ep[i]->pay();
delete ep[i];
}
cout << "Total wages paid to these employees was: "
<< sal
<< " dollars" << endl << endl;
return 0;
}
// End of function main
// Definition of payout functions
// An overloaded function with one, two, and 3 arguments
void payout(double managers_pay) {
cout << "The basic salary for a manager is: "
<< managers_pay << " dollars per month." << endl << endl;
}
void payout(double reg_emp_pay, double reg_emp_hrs) {
double reg_monthly_pay;
reg_monthly_pay = reg_emp_pay * reg_emp_hrs;
cout << "The basic pay for a regular employee is: "
<< reg_monthly_pay << " dollars per month." << endl << endl;
}
void payout(double monthly_salary, double commission, double units) {
double reg_monthly_pay;
reg_monthly_pay = (monthly_salary + (commission*units));
cout << "The basic pay for a sales manager is: "
<< reg_monthly_pay << " dollars per month." << endl << endl;
}
-----------------------------------------------------------------------------------------------------------
Program Output:
--------------------------
The output of the sample program is:
Monthly Employee Pay Report
To view data on employees, type your password and press Enter:
The basic salary for a manager is: 1510.35 dollars per month.
The basic pay for a regular employee is: 1030.00 dollars per month.
The basic pay for a sales manager is: 950.00 dollars per month.
Jack Smith is a manager with employee number 123
makes 28020.00 per year.
Jack Smith made 2335.00 dollars this month.
Everett James is a regular employee with employee number 456
makes 12.00 dollars per hour
and worked 160.00 hours this month.
Everett James made 1920.00 dollars this month.
Jackson Doe is a sales person with employee number 101
works for a straight commission of 31.00
dollars per unit sold and sold 65 units this month.
Jackson Doe made 2015.00 dollars this month.
Jennifer Stevens is a sales manager with employee number 789
makes 28000.00 per year and earns a commission of 4.00 dollars per unit sold.
Jennifer Stevens was responsible for sales of 105 units this month.
Jennifer Stevens made 2753.33 dollars this month.
Total wages paid to these employees was: 9023.33 dollars
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.