You guys have got to be sick of these C++ noobs coming in here asking for homework help but thats what Im going to have to do because I am totally lost....I was following pretty good till we got to overloading and passing things polymorphicly
Anyway the point of this homework assignment is to take an already created payrole program that sends information up polymorphically depending on the position of the employee. We are supposed to modify it to contain a birthdate variable in the employee class and have a date passed from the main program to the employee class for each employee and if this month is their birthday it's supposed to give them a $100 bonus. Well I can't even get it to pass the date all the way to employee without getting some weird linking error. I have no idea how to fix them. I hear they are caused when I have a function that doesn't get called or something like that...not sure if thats what I'm doing or not but here is the code im working with. I apologize in advanced for how much im going to give you but being that the complier doesn't tell me where exactly im doing something wrong I have to post everything to be sure i can get a straight answer...
HEADERS
BasePlusCommissionEmployee.h
#ifndef BASEPLUS_H
#define BASEPLUS_H
#include "CommissionEmployee.h" // CommissionEmployee class definition
#include "Date.h"
class BasePlusCommissionEmployee : public CommissionEmployee
{
public:
BasePlusCommissionEmployee( const string &, const string &,
const string &, Date & , double = 0.0, double = 0.0, double = 0.0 );// add date values here
void setBaseSalary( double ); // set base salary
double getBaseSalary() const; // return base salary
// keyword virtual signals intent to override
virtual double earnings() const; // calculate earnings
virtual void print() const; // print BasePlusCommissionEmployee object
private:
double baseSalary; // base salary per week
}; // end class BasePlusCommissionEmployee
#endif // BASEPLUS_H
CommissionEmployee.h
// Fig. 13.19: CommissionEmployee.h
// CommissionEmployee class derived from Employee.
#ifndef COMMISSION_H
#define COMMISSION_H
#include "Employee.h" // Employee class definition
class CommissionEmployee : public Employee
{
public:
CommissionEmployee( const string &, const string &,
const string &, Date &, double = 0.0, double = 0.0 );// add date values here
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; // calculate earnings
virtual void print() const; // print CommissionEmployee object
private:
double grossSales; // gross weekly sales
double commissionRate; // commission percentage
}; // end class CommissionEmployee
#endif // COMMISSION_H
Date.h
// Fig. 11.12: Date.h
// Date class definition.
#ifndef DATE_H
#define DATE_H
#include <iostream>
using std::ostream;
class Date
{
friend ostream &operator<<( ostream &, const Date & );
public:
Date( int m = 1, int d = 1, int y = 1900 ); // default constructor
Date( Date &);
void setDate( int, int, int ); // set month, day, year
Date &operator++(); // prefix increment operator
Date operator++( int ); // postfix increment operator
const Date &operator+=( int ); // add days, modify object
bool leapYear( int ) const; // is date in a leap year?
bool endOfMonth( int ) const; // is date at the end of month?
private:
int month;
int day;
int year;
static const int days[]; // array of days per month
void helpIncrement(); // utility function for incrementing date
}; // end class Date
#endif
Employee.h
// Fig. 13.13: Employee.h
// Employee abstract base class.
#ifndef EMPLOYEE_H
#define EMPLOYEE_H
#include <string> // C++ standard string class
using std::string;
#include "Date.h"
class Employee
{
public:
Employee( const string &, const string &, const string &, Date & );
void setFirstName( const string & ); // set first name
string getFirstName() const; // return first name
void setLastName( const string & ); // set last name
string getLastName() const; // return last name
void setSocialSecurityNumber( const string & ); // set SSN
string getSocialSecurityNumber() const; // return SSN
void setBirthDate(Date &);
// pure virtual function makes Employee abstract base class
virtual double earnings() const = 0; // pure virtual
virtual void print() const; // virtual
private:
string firstName;
string lastName;
string socialSecurityNumber;
Date &birthDate;
}; // end class Employee
#endif // EMPLOYEE_H
HourlyEmployee.h
// Fig. 13.17: HourlyEmployee.h
// HourlyEmployee class definition.
#ifndef HOURLY_H
#define HOURLY_H
#include "Employee.h" // Employee class definition
class HourlyEmployee : public Employee
{
public:
HourlyEmployee( const string &, const string &,
const string &, Date &, double = 0.0, double = 0.0);// add date values here
void setWage( double ); // set hourly wage
double getWage() const; // return hourly wage
void setHours( double ); // set hours worked
double getHours() const; // return hours worked
// keyword virtual signals intent to override
virtual double earnings() const; // calculate earnings
virtual void print() const; // print HourlyEmployee object
private:
double wage; // wage per hour
double hours; // hours worked for week
}; // end class HourlyEmployee
#endif // HOURLY_H
SalariedEmployee.h
// Fig. 13.15: SalariedEmployee.h
// SalariedEmployee class derived from Employee.
#ifndef SALARIED_H
#define SALARIED_H
#include "Employee.h" // Employee class definition
#include <string> // C++ standard string class
using std::string;
class SalariedEmployee : public Employee
{
public:
SalariedEmployee( const string &, const string &,
const string &, Date &, double = 0.0);// add date values here
void setWeeklySalary( double ); // set weekly salary
double getWeeklySalary() const; // return weekly salary
// keyword virtual signals intent to override
virtual double earnings() const; // calculate earnings
virtual void print() const; // print SalariedEmployee object
private:
double weeklySalary; // salary per week
}; // end class SalariedEmployee
#endif // SALARIED_H
SOURCE FILES
BasePlusCommissionEmployee.cpp
// Fig. 13.22: BasePlusCommissionEmployee.cpp
// BasePlusCommissionEmployee member-function definitions.
#include <iostream>
using std::cout;
// BasePlusCommissionEmployee class definition
#include "BasePlusCommissionEmployee.h"
// constructor
BasePlusCommissionEmployee::BasePlusCommissionEmployee(
const string &first, const string &last, const string &ssn, Date &d,
double sales, double rate, double salary )// add date values here
: CommissionEmployee( first, last, ssn, d, sales, rate )
{
setBaseSalary( salary ); // validate and store base salary
} // end BasePlusCommissionEmployee constructor
// set base salary
void BasePlusCommissionEmployee::setBaseSalary( double salary )
{
baseSalary = ( ( salary < 0.0 ) ? 0.0 : salary );
} // end function setBaseSalary
// return base salary
double BasePlusCommissionEmployee::getBaseSalary() const
{
return baseSalary;
} // end function getBaseSalary
// calculate earnings;
// override pure virtual function earnings in Employee
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
CommissionEmployee.h
// Fig. 13.19: CommissionEmployee.h
// CommissionEmployee class derived from Employee.
#ifndef COMMISSION_H
#define COMMISSION_H
#include "Employee.h" // Employee class definition
class CommissionEmployee : public Employee
{
public:
CommissionEmployee( const string &, const string &,
const string &, Date &, double = 0.0, double = 0.0 );// add date values here
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; // calculate earnings
virtual void print() const; // print CommissionEmployee object
private:
double grossSales; // gross weekly sales
double commissionRate; // commission percentage
}; // end class CommissionEmployee
#endif // COMMISSION_H
Date.cpp
// Fig. 11.13: Date.cpp
// Date class member-function definitions.
#include <iostream>
#include "Date.h"
// initialize static member at file scope; one classwide copy
const int Date::days[] =
{ 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
// Date constructor
Date::Date( int m, int d, int y )
{
setDate( m, d, y );
} // end Date constructor
// set month, day and year
void Date::setDate( int mm, int dd, int yy )
{
month = ( mm >= 1 && mm <= 12 ) ? mm : 1;
year = ( yy >= 1900 && yy <= 2100 ) ? yy : 1900;
// test for a leap year
if ( month == 2 && leapYear( year ) )
day = ( dd >= 1 && dd <= 29 ) ? dd : 1;
else
day = ( dd >= 1 && dd <= days[ month ] ) ? dd : 1;
} // end function setDate
// overloaded prefix increment operator
Date &Date::operator++()
{
helpIncrement(); // increment date
return *this; // reference return to create an lvalue
} // end function operator++
// overloaded postfix increment operator; note that the
// dummy integer parameter does not have a parameter name
Date Date::operator++( int )
{
Date temp = *this; // hold current state of object
helpIncrement();
// return unincremented, saved, temporary object
return temp; // value return; not a reference return
} // end function operator++
// add specified number of days to date
const Date &Date::operator+=( int additionalDays )
{
for ( int i = 0; i < additionalDays; i++ )
helpIncrement();
return *this; // enables cascading
} // end function operator+=
// if the year is a leap year, return true; otherwise, return false
bool Date::leapYear( int testYear ) const
{
if ( testYear % 400 == 0 ||
( testYear % 100 != 0 && testYear % 4 == 0 ) )
return true; // a leap year
else
return false; // not a leap year
} // end function leapYear
// determine whether the day is the last day of the month
bool Date::endOfMonth( int testDay ) const
{
if ( month == 2 && leapYear( year ) )
return testDay == 29; // last day of Feb. in leap year
else
return testDay == days[ month ];
} // end function endOfMonth
// function to help increment the date
void Date::helpIncrement()
{
// day is not end of month
if ( !endOfMonth( day ) )
day++; // increment day
else
if ( month < 12 ) // day is end of month and month < 12
{
month++; // increment month
day = 1; // first day of new month
} // end if
else // last day of year
{
year++; // increment year
month = 1; // first month of new year
day = 1; // first day of new month
} // end else
} // end function helpIncrement
// overloaded output operator
ostream &operator<<( ostream &output, const Date &d )
{
static char *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<<
Employee.cpp
// Fig. 13.14: Employee.cpp
// Abstract-base-class Employee member-function definitions.
// Note: No definitions are given for pure virtual functions.
#include <iostream>
using std::cout;
#include "Employee.h" // Employee class definition
#include "Date.h"
// constructor
Employee::Employee( const string &first, const string &last,
const string &ssn, Date &d )
: firstName( first ), lastName( last ), socialSecurityNumber( ssn ), birthDate( d )
{
// empty body
} // end Employee constructor
// set first name
void Employee::setFirstName( const string &first )
{
firstName = first;
} // end function setFirstName
// return first name
string Employee::getFirstName() const
{
return firstName;
} // end function getFirstName
// set last name
void Employee::setLastName( const string &last )
{
lastName = last;
} // end function setLastName
// return last name
string Employee::getLastName() const
{
return lastName;
} // end function getLastName
// set social security number
void Employee::setSocialSecurityNumber( const string &ssn )
{
socialSecurityNumber = ssn; // should validate
} // end function setSocialSecurityNumber
// return social security number
string Employee::getSocialSecurityNumber() const
{
return socialSecurityNumber;
} // end function getSocialSecurityNumber
void Employee::setBirthDate(Date &d)
{
birthDate = d;
};
// print Employee's information (virtual, but not pure virtual)
void Employee::print() const
{
cout << getFirstName() << ' ' << getLastName()
<< "\nsocial security number: " << getSocialSecurityNumber()
<< "\nBirthday: " << &birthDate;
} // end function print
HourlyEmployee.cpp
// Fig. 13.18: HourlyEmployee.cpp
// HourlyEmployee class member-function definitions.
#include <iostream>
using std::cout;
#include "HourlyEmployee.h" // HourlyEmployee class definition
#include "Date.h"
// constructor
HourlyEmployee::HourlyEmployee( const string &first, const string &last,
const string &ssn, Date &d, double hourlyWage, double hoursWorked )// add date values here
: Employee( first, last, ssn, d )
{
setWage( hourlyWage ); // validate hourly wage
setHours( hoursWorked ); // validate hours worked
} // end HourlyEmployee constructor
// set wage
void HourlyEmployee::setWage( double hourlyWage )
{
wage = ( hourlyWage < 0.0 ? 0.0 : hourlyWage );
} // end function setWage
// return wage
double HourlyEmployee::getWage() const
{
return wage;
} // end function getWage
// set hours worked
void HourlyEmployee::setHours( double hoursWorked )
{
hours = ( ( ( hoursWorked >= 0.0 ) && ( hoursWorked <= 168.0 ) ) ?
hoursWorked : 0.0 );
} // end function setHours
// return hours worked
double HourlyEmployee::getHours() const
{
return hours;
} // end function getHours
// calculate earnings;
// override pure virtual function earnings in Employee
double HourlyEmployee::earnings() const
{
if ( getHours() <= 40 ) // no overtime
return getWage() * getHours();
else
return 40 * getWage() + ( ( getHours() - 40 ) * getWage() * 1.5 );
} // end function earnings
// print HourlyEmployee's information
void HourlyEmployee::print() const
{
cout << "hourly employee: ";
Employee::print(); // code reuse
cout << "\nhourly wage: " << getWage() <<
"; hours worked: " << getHours();
} // end function print
SalariedEmployee.cpp
// Fig. 13.16: SalariedEmployee.cpp
// SalariedEmployee class member-function definitions.
#include <iostream>
using std::cout;
#include "SalariedEmployee.h" // SalariedEmployee class definition
#include "Date.h"
// constructor
SalariedEmployee::SalariedEmployee( const string &first,
const string &last, const string &ssn, Date &d, double salary )// add date values here
: Employee( first, last, ssn, d )
{
setWeeklySalary( salary );
} // end SalariedEmployee constructor
// set salary
void SalariedEmployee::setWeeklySalary( double salary )
{
weeklySalary = ( salary < 0.0 ) ? 0.0 : salary;
} // 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
fig13_23.cpp
// Fig. 13.23: fig13_23.cpp
// Processing Employee derived-class objects individually
// and polymorphically using dynamic binding.
#include <iostream>
using std::cout;
using std::endl;
using std::fixed;
#include <iomanip>
using std::setprecision;
#include <vector>
using std::vector;
// include definitions of classes in Employee hierarchy
#include "Employee.h"
#include "SalariedEmployee.h"
#include "HourlyEmployee.h"
#include "CommissionEmployee.h"
#include "BasePlusCommissionEmployee.h"
#include "Date.h"
void virtualViaPointer( const Employee * const ); // prototype
void virtualViaReference( const Employee & ); // prototype
int main()
{
Date JSmith(2,14,1976);
Date KPrice(12,16,1981);
Date SJones(11,06,1987);
Date BLewis(10,10,1981);
Date currentDate(10,3,2006);
// set floating-point output formatting
cout << fixed << setprecision( 2 );
// create derived-class objects
SalariedEmployee salariedEmployee(
"John", "Smith", "111-11-1111", JSmith, 800 );// add date values here
HourlyEmployee hourlyEmployee(
"Karen", "Price", "222-22-2222", KPrice, 16.75, 40 );// add date values here
CommissionEmployee commissionEmployee(
"Sue", "Jones", "333-33-3333", SJones, 10000, .06 );// add date values here
BasePlusCommissionEmployee basePlusCommissionEmployee(
"Bob", "Lewis", "444-44-4444", BLewis, 5000, .04, 300 );// add date values here
cout << JSmith << "\n\n"
<< KPrice << "\n\n"
<< SJones << "\n\n"
<< BLewis;
// output each Employee’s information and earnings using static binding
salariedEmployee.print();
cout << "\nearned $" << salariedEmployee.earnings() << "\n\n";
hourlyEmployee.print();
cout << "\nearned $" << hourlyEmployee.earnings() << "\n\n";
commissionEmployee.print();
cout << "\nearned $" << commissionEmployee.earnings() << "\n\n";
basePlusCommissionEmployee.print();
cout << "\nearned $" << basePlusCommissionEmployee.earnings()
<< "\n\n";
// create vector of four base-class pointers
vector < Employee * > employees( 4 );
// initialize vector with Employees
employees[ 0 ] = &salariedEmployee;
employees[ 1 ] = &hourlyEmployee;
employees[ 2 ] = &commissionEmployee;
employees[ 3 ] = &basePlusCommissionEmployee;
cout << "Employees processed polymorphically via dynamic binding:\n\n";
// call virtualViaPointer to print each Employee's information
// and earnings using dynamic binding
cout << "Virtual function calls made off base-class pointers:\n\n";
for ( size_t i = 0; i < employees.size(); i++ )
virtualViaPointer( employees[ i ] );
// call virtualViaReference to print each Employee's information
// and earnings using dynamic binding
cout << "Virtual function calls made off base-class references:\n\n";
for ( size_t i = 0; i < employees.size(); i++ )
virtualViaReference( *employees[ i ] ); // note dereferencing
return 0;
} // end main
// call Employee virtual functions print and earnings off a
// base-class pointer using dynamic binding
void virtualViaPointer( const Employee * const baseClassPtr )
{
baseClassPtr->print();
cout << "\nearned $" << baseClassPtr->earnings() << "\n\n";
} // end function virtualViaPointer
// call Employee virtual functions print and earnings off a
// base-class reference using dynamic binding
void virtualViaReference( const Employee &baseClassRef )
{
baseClassRef.print();
cout << "\nearned $" << baseClassRef.earnings() << "\n\n";
} // end function virtualViaReference
Again i'm sorry for the monsterious amount of code but I didn't know what else to do. I'm falling way behind in this class and I'm getting really frustrated. I'm usually good at understanding programs...
Anyway here is the build log when i try to compile that:
------ Build started: Project: homework13_12, Configuration: Debug Win32 ------
Compiling...
SalariedEmployee.cpp
HourlyEmployee.cpp
fig13_23.cpp
Employee.cpp
Date.cpp
CommissionEmployee.cpp
BasePlusCommissionEmployee.cpp
Generating Code...
Linking...
Date.obj : error LNK2028: unresolved token (0A00028C) "public: __thiscall Date::Date(class Date &)" (??0Date@@$$FQAE@AAV0@@Z) referenced in function "public: class Date __thiscall Date::operator++(int)" (??EDate@@$$FQAE?AV0@H@Z)
Date.obj : error LNK2019: unresolved external symbol "public: __thiscall Date::Date(class Date &)" (??0Date@@$$FQAE@AAV0@@Z) referenced in function "public: class Date __thiscall Date::operator++(int)" (??EDate@@$$FQAE?AV0@H@Z)
G:\C++\homework\homework13_12_2\Debug\homework13_12.exe : fatal error LNK1120: 2 unresolved externals
Build log was saved at "file://g:\C++\homework\homework13_12_2\homework13_12_2\Debug\BuildLog.htm"
homework13_12 - 3 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Please help if you can.