Jan 9, 2022 at 8:03pm UTC
Can not compile. Please help to amend
Part 1/2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
#define EMPLOYEE_H
#include <string> // C++ standard string class
class Employee
{
public :
Employee( const std::string &, const std::string &,
const std::string & );
virtual ~Employee() { }; // virtual destructor
void setFirstName( const std::string & ); // set first name
std::string getFirstName() const ; // return first name
void setLastName( const std::string & ); // set last name
std::string getLastName() const ; // return last name
void setSocialSecurityNumber( const std::string & ); // set SSN
std::string getSocialSecurityNumber() const ; // return SSN
// pure virtual function makes Employee an abstract base class
virtual double earnings() const = 0; // pure virtual
virtual void print() const ; // virtual
private :
std::string firstName;
std::string lastName;
std::string socialSecurityNumber;
}; // end class Employee
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
// Fig. 12.11: SalariedEmployee.h
// SalariedEmployee class derived from Employee.
#define SALARIED_H
#include <string> // C++ standard string class
#include "Employee.h" // Employee class definition
class SalariedEmployee : public Employee
{
public :
SalariedEmployee( const std::string &, const std::string &,
const std::string &, double = 0.0 );
virtual ~SalariedEmployee() { }; // virtual destructor
void setWeeklySalary( double ); // set weekly salary
double getWeeklySalary() const ; // return weekly salary
// keyword virtual signals intent to override
virtual double earnings() const override; // calculate earnings
virtual void print() const override; // print object
private :
double weeklySalary; // salary per week
}; // end class SalariedEmployee
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
// Fig. 12.13: CommissionEmployee.h
// CommissionEmployee class derived from Employee.
#define COMMISSION_H
#include <string> // C++ standard string class
#include "Employee.h" // Employee class definition
class CommissionEmployee : public Employee
{
public :
CommissionEmployee( const std::string &, const std::string &,
const std::string &, double = 0.0, double = 0.0 );
virtual ~CommissionEmployee() { }; // virtual destructor
void setCommissionRate( double ); // set commission rate
double getCommissionRate() const ; // return commission rate
void setGrossSales( double ); // set gross sales amount
double getGrossSales() const ; // return gross sales amount
// keyword virtual signals intent to override
virtual double earnings() const override; // calculate earnings
virtual void print() const override; // print object
private :
double grossSales; // gross weekly sales
double commissionRate; // commission percentage
}; // end class CommissionEmployee
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
// Fig. 12.15: BasePlusCommissionEmployee.h
// BasePlusCommissionEmployee class derived from CommissionEmployee.
#ifndef BASEPLUS_H
#define BASEPLUS_H
#include <string> // C++ standard string class
#include "CommissionEmployee.h" // CommissionEmployee class definition
class BasePlusCommissionEmployee : public CommissionEmployee
{
public :
BasePlusCommissionEmployee( const std::string &, const std::string &,
const std::string &, double = 0.0, double = 0.0, double = 0.0 );
virtual ~BasePlusCommissionEmployee() { }; // virtual destructor
void setBaseSalary( double ); // set base salary
double getBaseSalary() const ; // return base salary
// keyword virtual signals intent to override
virtual double earnings() const override; // calculate earnings
virtual void print() const override; // print object
private :
double baseSalary; // base salary per week
}; // end class BasePlusCommissionEmployee
#endif // BASEPLUS_H
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
// SalariedEmployee class member-function definitions.
#include <iostream>
#include <stdexcept>
#include "SalariedEmployee.h" // SalariedEmployee class definition
using namespace std;
// constructor
SalariedEmployee::SalariedEmployee( const string &first,
const string &last, const string &ssn, double salary )
: Employee( first, last, ssn )
{
setWeeklySalary( salary );
} // end SalariedEmployee constructor
// set salary
void SalariedEmployee::setWeeklySalary( double salary )
{
if ( salary >= 0.0 )
weeklySalary = salary;
else
throw invalid_argument( "Weekly salary must be >= 0.0" );
} // end function setWeeklySalary
// return salary
double SalariedEmployee::getWeeklySalary() const
{
return weeklySalary;
} // end function getWeeklySalary
// calculate earnings;
// override pure virtual function earnings in Employee
double SalariedEmployee::earnings() const
{
return getWeeklySalary();
} // end function earnings
// print SalariedEmployee's information
void SalariedEmployee::print() const
{
cout << "salaried employee: " ;
Employee::print(); // reuse abstract base-class print function
cout << "\nweekly salary: " << getWeeklySalary();
} // end function print
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
// Fig. 12.14: CommissionEmployee.cpp
// CommissionEmployee class member-function definitions.
#include <iostream>
#include <stdexcept>
#include "CommissionEmployee.h" // CommissionEmployee class definition
using namespace std;
// constructor
CommissionEmployee::CommissionEmployee( const string &first,
const string &last, const string &ssn, double sales, double rate )
: Employee( first, last, ssn )
{
setGrossSales( sales );
setCommissionRate( rate );
} // end CommissionEmployee constructor
// set gross sales amount
void CommissionEmployee::setGrossSales( double sales )
{
if ( sales >= 0.0 )
grossSales = sales;
else
throw invalid_argument( "Gross sales must be >= 0.0" );
} // end function setGrossSales
// return gross sales amount
double CommissionEmployee::getGrossSales() const
{
return grossSales;
} // end function getGrossSales
// set commission rate
void CommissionEmployee::setCommissionRate( double rate )
{
if ( rate > 0.0 && rate < 1.0 )
commissionRate = rate;
else
throw invalid_argument( "Commission rate must be > 0.0 and < 1.0" );
} // end function setCommissionRate
// return commission rate
double CommissionEmployee::getCommissionRate() const
{
return commissionRate;
} // end function getCommissionRate
// calculate earnings; override pure virtual function earnings in Employee
double CommissionEmployee::earnings() const
{
return getCommissionRate() * getGrossSales();
} // end function earnings
// print CommissionEmployee's information
void CommissionEmployee::print() const
{
cout << "commission employee: " ;
Employee::print(); // code reuse
cout << "\ngross sales: " << getGrossSales()
<< "; commission rate: " << getCommissionRate();
} // end function print
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
// Fig. 12.16: BasePlusCommissionEmployee.cpp
// BasePlusCommissionEmployee member-function definitions.
#include <iostream>
#include <stdexcept>
#include "BasePlusCommissionEmployee.h"
using namespace std;
// constructor
BasePlusCommissionEmployee::BasePlusCommissionEmployee(
const string &first, const string &last, const string &ssn,
double sales, double rate, double salary )
: CommissionEmployee( first, last, ssn, sales, rate )
{
setBaseSalary( salary ); // validate and store base salary
} // end BasePlusCommissionEmployee constructor
// set base salary
void BasePlusCommissionEmployee::setBaseSalary( double salary )
{
if ( salary >= 0.0 )
baseSalary = salary;
else
throw invalid_argument( "Salary must be >= 0.0" );
} // end function setBaseSalary
// return base salary
double BasePlusCommissionEmployee::getBaseSalary() const
{
return baseSalary;
} // end function getBaseSalary
// calculate earnings;
// override virtual function earnings in CommissionEmployee
double BasePlusCommissionEmployee::earnings() const
{
return getBaseSalary() + CommissionEmployee::earnings() ;
} // end function earnings
// print BasePlusCommissionEmployee's information
void BasePlusCommissionEmployee::print() const
{
cout << "base-salaried " ;
CommissionEmployee::print(); // code reuse
cout << "; base salary: " << getBaseSalary();
} // end function print
Last edited on Jan 9, 2022 at 8:04pm UTC
Jan 9, 2022 at 8:25pm UTC
Proper formatting that includes some white space & indentation would go a long way to make that wall of text more readable.
Jan 9, 2022 at 9:22pm UTC
You may want to start by fixing most of your #include guards. The fist several files don't have proper guards, look at your baseplus header for the proper use of guards.
Jan 10, 2022 at 7:49am UTC
Actually there are no compiler errors but linker errors.
This Employee::print(); // reuse abstract base-class print function
is not allowed. Since Employee::print has no body.
You probably also need to implement the Employee constructor.
Jan 10, 2022 at 11:30am UTC
Still don't understand meaning ifndef, yet, i always deleted book ifndef.
Fixed. Works.
Thank.
If someone could easily explain it's usage i will cast big plus to carma.
Namaste