unsure how to combine current working classes with date class, need to amend syntax

Jan 10, 2022 at 4:24pm
Unsure how to combine current working classes with date class, need to amend syntax

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

#ifndef EMPLOYEE_H
#define EMPLOYEE_H
#include "Date.h"
#include <string> 
class Employee
{
public:
Employee( const std::string &, const std::string &,
const std::string &, int,int,int );

Date getBirthDate() const;


virtual ~Employee() { }; 
void setFirstName( const std::string & ); 
std::string getFirstName() const; 
void setLastName( const std::string & ); 
std::string getLastName() const; 
void setSocialSecurityNumber( const std::string & ); 
std::string getSocialSecurityNumber() const; 
// pure virtual function makes Employee an abstract base class
virtual double earnings() const = 0;
virtual void print() const; 
private:
std::string firstName;
std::string lastName;
std::string socialSecurityNumber;
Date birthDate;

}; 
#endif 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 #include <array>
#include <iostream>
class Date
{
friend std::ostream &operator<<( std:: ostream &, const Date & );
public:
Date( int m = 1, int d = 1, int y = 1900 );
void setDate( int, int, int ); 
Date &operator++(); 
Date operator++( int ); 
Date &operator+=( unsigned int ); 
static bool leapYear( int ); 
bool endOfMonth( int ) const; 
private:
unsigned int month;
unsigned int day;
unsigned int year;

static const std::array< unsigned int, 13 > days; 
void helpIncrement(); 
}; 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

#ifndef BASEPLUS_H
#define BASEPLUS_H
#include <string> 
#include "CommissionEmployee.h" 
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() { };
void setBaseSalary( double ); 
double getBaseSalary() const; 

virtual double earnings() const override; 
virtual void print() const override; 
private:
double baseSalary; 
}; 
#endif 



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

#ifndef COMMISSION_H
#define COMMISSION_H
#include <string> 
#include "Employee.h" 
class CommissionEmployee : public Employee
{
public:
CommissionEmployee( const std::string &, const std::string &,
const std::string &, double = 0.0, double = 0.0,int,int,int );
virtual ~CommissionEmployee() { }; 
void setCommissionRate( double ); 
double getCommissionRate() const; 
void setGrossSales( double ); 
double getGrossSales() const; 

virtual double earnings() const override; 
virtual void print() const override; 
private:
double grossSales; 
double commissionRate; 
}; 
#endif  



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

#define SALARIED_H
#include <string> 
#include "Employee.h" n
class SalariedEmployee : public Employee
{
public:
SalariedEmployee( const std::string &, const std::string &,
const std::string &, double = 0.0, int, int ,int);
virtual ~SalariedEmployee() { }; 
void setWeeklySalary( double ); 
double getWeeklySalary() const; 
// keyword virtual signals intent to override
virtual double earnings() const override; 
virtual void print() const override; 
private:
double weeklySalary; 
}; 


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
55
56
57
58

#include <iostream>
#include "Employee.h" 
using namespace std;


Employee::Employee( const string &first, const string &last,
const string &ssn, int mm ,int dd ,int yy )
: firstName( first ), lastName( last ), socialSecurityNumber( ssn ), birthDate (mm,dd,yy)
{

} 

void Employee::setFirstName( const string &first )
{
firstName = first;
}

string Employee::getFirstName() const
{
return firstName;
} 

void Employee::setLastName( const string &last )
{
lastName = last;
} 

string Employee::getLastName() const
{

return lastName;
} 

void Employee::setSocialSecurityNumber( const string &ssn )
{
socialSecurityNumber = ssn; 
}

string Employee::getSocialSecurityNumber() const
{
return socialSecurityNumber;
} 

void Employee::print() const
{
cout << getFirstName() << ' ' << getLastName()
<< "\nsocial security number: " << getSocialSecurityNumber();
} 

Date Employee::getBirthDate() const
 {
 return birthDate; 
 }





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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <iostream>
#include <string>
#include "Date.h"
using namespace std;

const array< unsigned int, 13 > Date::days =
{ 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

Date::Date( int month, int day, int year )
{
setDate( month, day, year );
} 

void Date::setDate( int mm, int dd, int yy )
{
if ( mm >= 1 && mm <= 12 )
month = mm;
else
throw invalid_argument( "Month must be 1-12" );
if ( yy >= 1900 && yy <= 2100 )
year = yy;
else
throw invalid_argument( "Year must be >= 1900 and <= 2100" );
// test for a leap year
if ( ( month == 2 && leapYear( year ) && dd >= 1 && dd <= 29 ) ||
( dd >= 1 && dd <= days[ month ] ) )
day = dd;
else
throw invalid_argument(
"Day is out of range for current month and year" );
} 

Date &Date::operator++()
{

helpIncrement(); 
return *this; 
} 


Date Date::operator++( int )
{
Date temp = *this; 
helpIncrement();

return temp; 
} 

Date &Date::operator+=( unsigned int additionalDays )
{
for ( int i = 0; i < additionalDays; ++i )
helpIncrement();
return *this; 
} 

bool Date::leapYear( int testYear )
{
if ( testYear % 400 == 0 ||
( testYear % 100 != 0 && testYear % 4 == 0 ) )
return true; 
else
return false; // not a leap year
} 

bool Date::endOfMonth( int testDay ) const
{
if ( month == 2 && leapYear( year ) )
return testDay == 29; 
else
return testDay == days[ month ];
} 

void Date::helpIncrement()
{

if ( !endOfMonth( day ) )
++day; 
else
if ( month < 12 ) 
{
++month; 


day = 1; 
} 
else 
{
++year; 
month = 1; 
day = 1; 
} 
} 

ostream &operator<<( ostream &output, const Date &d )
{
static string monthName[ 13 ] = { "", "January", "February",
"March", "April", "May", "June", "July", "August",
"September", "October", "November", "December" };
output << monthName[ d.month ] << ' ' << d.day << ", " << d.year;
return output; // enables cascading
} // end function operator<< 


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

#include <iostream>
#include <stdexcept>
#include "BasePlusCommissionEmployee.h"
using namespace std;

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
} 


void BasePlusCommissionEmployee::setBaseSalary( double salary )
{
if ( salary >= 0.0 )
baseSalary = salary;
else
throw invalid_argument( "Salary must be >= 0.0" );
} 

double BasePlusCommissionEmployee::getBaseSalary() const
{
return baseSalary;
} 

double BasePlusCommissionEmployee::earnings() const
{
return getBaseSalary() + CommissionEmployee::earnings() ;
} 

void BasePlusCommissionEmployee::print() const
{
cout << "base-salaried ";
CommissionEmployee::print(); 
cout << "; base salary: " << getBaseSalary();
} 


[code]

Jan 10, 2022 at 4:25pm
2/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
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

#include <iostream>
#include <stdexcept>
#include "CommissionEmployee.h" 
using namespace std;

CommissionEmployee::CommissionEmployee( const string &first,
const string &last, const string &ssn, double sales, double rate, int mm, int dd, int yy )
: Employee( first, last, ssn, mm, dd, yy )
{
setGrossSales( sales );
setCommissionRate( rate );
} 

void CommissionEmployee::setGrossSales( double sales )
{
if ( sales >= 0.0 )
grossSales = sales;
else
throw invalid_argument( "Gross sales must be >= 0.0" );
} 

double CommissionEmployee::getGrossSales() const
{
return grossSales;
} 

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" );
} 

double CommissionEmployee::getCommissionRate() const
{
return commissionRate;
} 

double CommissionEmployee::earnings() const
{
return getCommissionRate() * getGrossSales();
} 

void CommissionEmployee::print() const
{
cout << "commission employee: ";
Employee::print(); // code reuse
cout << "\ngross sales: " << getGrossSales()
<< "; commission rate: " << getCommissionRate();
} 


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

#include <iostream>
#include <stdexcept>
#include "SalariedEmployee.h" 
using namespace std;

SalariedEmployee::SalariedEmployee( const string &first,
const string &last, const string &ssn, double salary, int mm, int dd, int yy )
: Employee( first, last, ssn, mm, dd, yy	 )
{
setWeeklySalary( salary );
} 

void SalariedEmployee::setWeeklySalary( double salary )
{
if ( salary >= 0.0 )
weeklySalary = salary;
else
throw invalid_argument( "Weekly salary must be >= 0.0" );
} 

double SalariedEmployee::getWeeklySalary() const
{
return weeklySalary;
} 

double SalariedEmployee::earnings() const
{
return getWeeklySalary();
} 

void SalariedEmployee::print() const
{

cout << "salaried employee: ";
Employee::print(); 
cout << "\nweekly salary: " << getWeeklySalary();
} 

Last edited on Jan 10, 2022 at 4:25pm
Jan 10, 2022 at 5:02pm
What do you mean, "need to amend syntax"?

Give us a clear, complete and precise descritption of your problem. The more you help us, the more we can help you.
Jan 10, 2022 at 5:26pm
Sure! How to avoid Compilation errors below?
I've added Date Class and its data members to combine with Employee derived classes.
I need to stay in this context and make the program work

Employee.h:24:14: error: ‘virtual void Employee::print() const’ cannot be overloaded with ‘void Employee::print() const’
24 | virtual void print() const; // virtual


In file included from SalariedEmployee.cpp:5:
SalariedEmployee.h:11:36: error: default argument missing for parameter 5 of ‘SalariedEmployee::SalariedEmployee(const string&, const string&, const string&, double, int, int, int)’
11 | const std::string &, double = 0.0, int, int ,int);
| ^~~
SalariedEmployee.h:11:22: note: ...following parameter 4 which has a default argument
11 | const std::string &, double = 0.0, int, int ,int);
| ^~~~~~~~~~~~
SalariedEmployee.h:11:41: error: default argument missing for parameter 6 of ‘SalariedEmployee::SalariedEmployee(const string&, const string&, const string&, double, int, int, int)’
11 | const std::string &, double = 0.0, int, int ,int);
| ^~~
SalariedEmployee.h:11:46: error: default argument missing for parameter 7 of ‘SalariedEmployee::SalariedEmployee(const string&, const string&, const string&, double, int, int, int)’
11 | const std::string &, double = 0.0, int, int ,int);
| ^~~
In file included from CommissionEmployee.cpp:5:
CommissionEmployee.h:11:49: error: default argument missing for parameter 6 of ‘CommissionEmployee::CommissionEmployee(const string&, const string&, const string&, double, double, int, int, int)’
11 | const std::string &, double = 0.0, double = 0.0,int,int,int );
| ^~~
CommissionEmployee.h:11:22: note: ...following parameter 4 which has a default argument
11 | const std::string &, double = 0.0, double = 0.0,int,int,int );
| ^~~~~~~~~~~~
CommissionEmployee.h:11:53: error: default argument missing for parameter 7 of ‘CommissionEmployee::CommissionEmployee(const string&, const string&, const string&, double, double, int, int, int)’
11 | const std::string &, double = 0.0, double = 0.0,int,int,int );
| ^~~
CommissionEmployee.h:11:57: error: default argument missing for parameter 8 of ‘CommissionEmployee::CommissionEmployee(const string&, const string&, const string&, double, double, int, int, int)’
11 | const std::string &, double = 0.0, double = 0.0,int,int,int );
| ^~~
In file included from BasePlusCommissionEmployee.h:6,
from BasePlusCommissionEmployee.cpp:5:
CommissionEmployee.h:11:49: error: default argument missing for parameter 6 of ‘CommissionEmployee::CommissionEmployee(const string&, const string&, const string&, double, double, int, int, int)’
11 | const std::string &, double = 0.0, double = 0.0,int,int,int );
| ^~~
CommissionEmployee.h:11:22: note: ...following parameter 4 which has a default argument
11 | const std::string &, double = 0.0, double = 0.0,int,int,int );
| ^~~~~~~~~~~~
CommissionEmployee.h:11:53: error: default argument missing for parameter 7 of ‘CommissionEmployee::CommissionEmployee(const string&, const string&, const string&, double, double, int, int, int)’
11 | const std::string &, double = 0.0, double = 0.0,int,int,int );
| ^~~
CommissionEmployee.h:11:57: error: default argument missing for parameter 8 of ‘CommissionEmployee::CommissionEmployee(const string&, const string&, const string&, double, double, int, int, int)’
11 | const std::string &, double = 0.0, double = 0.0,int,int,int );
| ^~~
In file included from Main.cpp:8:
SalariedEmployee.h:11:36: error: default argument missing for parameter 5 of ‘SalariedEmployee::SalariedEmployee(const string&, const string&, const string&, double, int, int, int)’
11 | const std::string &, double = 0.0, int, int ,int);
| ^~~
SalariedEmployee.h:11:22: note: ...following parameter 4 which has a default argument
11 | const std::string &, double = 0.0, int, int ,int);
| ^~~~~~~~~~~~
SalariedEmployee.h:11:41: error: default argument missing for parameter 6 of ‘SalariedEmployee::SalariedEmployee(const string&, const string&, const string&, double, int, int, int)’
11 | const std::string &, double = 0.0, int, int ,int);
| ^~~
SalariedEmployee.h:11:46: error: default argument missing for parameter 7 of ‘SalariedEmployee::SalariedEmployee(const string&, const string&, const string&, double, int, int, int)’
11 | const std::string &, double = 0.0, int, int ,int);
| ^~~
In file included from Main.cpp:9:
CommissionEmployee.h:11:49: error: default argument missing for parameter 6 of ‘CommissionEmployee::CommissionEmployee(const string&, const string&, const string&, double, double, int, int, int)’
11 | const std::string &, double = 0.0, double = 0.0,int,int,int );
| ^~~
CommissionEmployee.h:11:22: note: ...following parameter 4 which has a default argument
11 | const std::string &, double = 0.0, double = 0.0,int,int,int );
| ^~~~~~~~~~~~
CommissionEmployee.h:11:53: error: default argument missing for parameter 7 of ‘CommissionEmployee::CommissionEmployee(const string&, const string&, const string&, double, double, int, int, int)’
11 | const std::string &, double = 0.0, double = 0.0,int,int,int );
| ^~~
CommissionEmployee.h:11:57: error: default argument missing for parameter 8 of ‘CommissionEmployee::CommissionEmployee(const string&, const string&, const string&, double, double, int, int, int)’
11 | const std::string &, double = 0.0, double = 0.0,int,int,int );
| ^~~
Last edited on Jan 10, 2022 at 5:59pm
Jan 10, 2022 at 5:59pm
If i try to rollback with single Employee Class and Date, it works fine when i create new object and take half arguments from Employee class, half from Date Class Function.
If try to add derived class and declare virtual functions, compiler throws errors:

Employee.h:24:14: error: ‘virtual void Employee::print() const’ cannot be overloaded with ‘void Employee::print() const’
24 | virtual void print() const; // virtual
| ^~~~~
Jan 11, 2022 at 6:58am
The first problem are the default paramters:

1
2
SalariedEmployee( const std::string &, const std::string &,
const std::string &, double = 0.0, int, int ,int);


The compiler requires that all default parameters (like double = 0.0) has to be at the end of the parameter list. So either remove the default = 0.0 or change the order of the parameter.

I would suggest that you solve the default paramter problem first and then trying to figure out that mysterious 'Employee::print()' problem.
Topic archived. No new replies allowed.