Hello, I am working on a homework problem that has to do with inheritance, I thought that as long as the class interface and implementation files for the base classes the derived would work with the include statement:
include "salariedemployee.h"
and the line class administrator : public salaried employee.
I get no compiler or linker errors when I run this all together as posted, but when I take out the files for the Employee class and the SalariedEmployee class I get linker errors.
What am I doing wrong?
//This is the file administrator.h
//This is the interface for the class administrator
//The implementation for the class administrator is in
//the header file administrator.h
//--------------------------------------------------------------------------
#ifndef ADMINISTRATOR_H
#define ADMINISTRATOR_H
#include "salariedemployee.h"
using namespace std;
using namespace employeessavitch;
class administrator : public SalariedEmployee
{
public:
administrator();
//default constructor for the administrator class
administrator(string the_name, string the_number,
double weekly_salary, string the_title,
string the_area,string the_supervisor,
double annual_sal);
string get_title();
//accsessor function that returns the admins title
string get_responsibility();
//accessor function that returns the admins area of responsibility
string get_supervisor();
//accessor function that returns the admins supervisor
void set_supervisor(string the_supervisor);
//mutator function that changes the admins supervisor
void get_admin_data();
//function to collect data about the admin from keyboard
void print();
//prints the administrators information on the screen
void print_check();
//prints the administrators correct paycheck
private:
string title;
string area_responsibility;
string supervisor;
protected:
double annual_salary;
};
#endif //ADMINISTRATOR_H
//----------------------------------------------------------------------------
//This is the file Administrator.cpp
//This is the implementation for the class Administrator
//The interface for the class Administrator is in
//the header file Administrator.h
#include <iostream>
#include <string>
using namespace std;
administrator::administrator() :SalariedEmployee( ), title("no title yet"),
area_responsibility("no area yet"), supervisor("no supervisor"),
annual_salary(0)
{
//deliberately empty
}
administrator::administrator(string the_name, string the_number,
double weekly_salary, string the_title,
string the_area,string the_supervisor,
double annual_sal):SalariedEmployee(the_name,
the_number, weekly_salary)
{
title = the_title;
area_responsibility = the_area;
supervisor = the_supervisor;
annual_salary = annual_sal;
}
void administrator::get_admin_data()
{
string temp_name, temp_number, temp_title, temp_area, temp_supervisor;
double temp_salary;
cout << "Please enter the following information:" << endl << endl
<< "Administrator name:";
cin >> temp_name;
cout << endl << "Social Security Number:";
cin >> temp_number;
cout << endl << "Job Title";
cin >> title;
cout << endl << "Area of responsibility:";
cin >> area_responsibility;
cout << endl << "supervisor Name:";
cin >> supervisor;
cout << endl << "Annual Salary";
cin >> annual_salary;
set_name(temp_name);
set_ssn(temp_number);
}
void administrator::print()
{
cout << "Administrator:" << get_name() << endl
<< "SSN:" << get_ssn() << endl
<< "Title:" << title << endl
<< "Area of Responsibility:" << area_responsibility << endl
<< "Supervisor Name:" << supervisor << endl
<< "Annual Salary:" << annual_salary << endl;
}
void administrator::print_check()
{
set_net_pay(annual_salary/12);
cout << "\n__________________________________________________\n";
cout << "Pay to the order of " << get_name( ) << endl;
cout << "The sum of " << get_net_pay( ) << " Dollars\n";
cout << "_________________________________________________\n";
cout << "Check Stub NOT NEGOTIABLE \n";
cout << "Employee Number: " << get_ssn( ) << endl;
cout << title << ". Monthly Pay: "
<< get_net_pay() << endl;
cout << "_________________________________________________\n";
}
void administrator::set_supervisor(string the_supervisor)
{
supervisor = the_supervisor;
}
string administrator::get_title()
{
return title;
}
string administrator::get_responsibility()
{
return area_responsibility;
}
string administrator::get_supervisor()
{
return supervisor;
}
//-----------------------------------------------------------------------------
//This is the main testing file for the administrator class
#include <iostream>
#include <string>
using namespace std;
using namespace employeessavitch;
int main()
{
system("pause");
return 0;
}
//-----------------------------------------------------------------------------
//This is the header file employee.h.
//This is the interface for the class Employee.
//This is primarily intended to be used as a base class to derive
//classes for different kinds of employees.
#ifndef EMPLOYEE_H
#define EMPLOYEE_H
#include <string>
using namespace std;
namespace employeessavitch
{
class Employee
{
public:
Employee( );
Employee(string the_name, string the_ssn);
string get_name( ) const;
string get_ssn( ) const;
double get_net_pay( ) const;
void set_name(string new_name);
void set_ssn(string new_ssn);
void set_net_pay(double new_net_pay);
void print_check( ) const;
private:
string name;
string ssn;
double net_pay;
};
}//employeessavitch
#endif //EMPLOYEE_H
//-----------------------------------------------------------------------------
//This is the file: employee.cpp.
//This is the implementation for the class Employee.
//The interface for the class Employee is in the header file employee.h.
#include <string>
#include <cstdlib>
#include <iostream>
#include "employee.h"
using namespace std;
namespace employeessavitch
{
Employee::Employee( ) : name("No name yet"), ssn("No number yet"), net_pay(0)
{
//deliberately empty
}
Employee::Employee(string the_name, string the_number)
: name(the_name), ssn(the_number), net_pay(0)
{
//deliberately empty
}
string Employee::get_name( ) const
{
return name;
}
string Employee::get_ssn( ) const
{
return ssn;
}
double Employee::get_net_pay( ) const
{
return net_pay;
}
void Employee::set_name(string new_name)
{
name = new_name;
}
void Employee::set_ssn(string new_ssn)
{
ssn = new_ssn;
}
void Employee::set_net_pay (double new_net_pay)
{
net_pay = new_net_pay;
}
void Employee::print_check( ) const
{
cout << "\nERROR: print_check FUNCTION CALLED FOR AN \n"
<< "UNDIFFERENTIATED EMPLOYEE. Aborting the program.\n"
<< "Check with the author of the program about this bug.\n";
exit(1);
}
}//employeessavitch
//----------------------------------------------------------------------------
//This is the header file salariedemployee.h.
//This is the interface for the class SalariedEmployee.
#ifndef SALARIEDEMPLOYEE_H
#define SALARIEDEMPLOYEE_H
#include <string>
#include "employee.h"
using namespace std;
namespace employeessavitch
{
class SalariedEmployee : public Employee
{
public:
SalariedEmployee( );
SalariedEmployee (string the_name, string the_ssn,
double the_weekly_salary);
double get_salary( ) const;
void set_salary(double new_salary);
void print_check( );
private:
double salary;//weekly
};
}//employeessavitch
#endif //SALARIEDEMPLOYEE_H
//-----------------------------------------------------------------------------
//This is the file salariedemployee.cpp.
//This is the implementation for the class SalariedEmployee.
//The interface for the class SalariedEmployee is in
//the header file salariedemployee.h.
#include <iostream>
#include <string>
#include "salariedemployee.h"
using namespace std;
using namespace employeessavitch;
namespace employeessavitch
{
SalariedEmployee::SalariedEmployee( ) : Employee( ), salary(0)
{
//deliberately empty
}
SalariedEmployee::SalariedEmployee(string the_name, string the_number,
double the_weekly_salary)
: Employee(the_name, the_number), salary(the_weekly_salary)
{
//deliberately empty
}
double SalariedEmployee::get_salary( ) const
{
return salary;
}
void SalariedEmployee::set_salary(double new_salary)
{
salary = new_salary;
}
void SalariedEmployee::print_check( )
{
set_net_pay(salary);
cout << "\n__________________________________________________\n";
cout << "Pay to the order of " << get_name( ) << endl;
cout << "The sum of " << get_net_pay( ) << " Dollars\n";
cout << "_________________________________________________\n";
cout << "Check Stub NOT NEGOTIABLE \n";
cout << "Employee Number: " << get_ssn( ) << endl;
cout << "Salaried Employee. Regular Pay: "
<< salary << endl;
cout << "_________________________________________________\n";
}
}//employeessavitch