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
|
// decleration in Emplyee.cpp
void Employee::displayEmployee(Employee* emp)
{
cout << "First name: " << emp->fName << endl;
cout << "Last name: " << emp->lName << endl;
cout << "SSN: " << emp->ssn << endl;
cout << "Phone number: " << emp->phone << endl;
cout << "Weekly pay: " << emp->calculatePay() << endl;
Hourly* hry = dynamic_cast<Hourly*>(emp); // try to convert Employee parent object to a Hourly child object
if (hry != NULL) // if the hry is not NULL, then we have a Hourly object!
{
cout << "Hours: $" << hry->getHours() << endl;
cout << "Pay rate: $" << hry->getRate() << endl;
}
Manager* mgr = dynamic_cast<Manager*>(emp); // try to convert Employee parent object to a Manager child object
if (mgr != NULL) // if the mgr is not NULL, then we have a Manager object!
{
cout << "Bonus: $" << mgr->getBonus() << endl;
}
Salary* sal = dynamic_cast<Salary*>(emp); // try to convert Employee parent object to a Salary child object
if (sal != NULL) // if the sal is not NULL, then we have a Salary object!
{
cout << "Annual salary: $" << sal->getAnnualSalary() << endl;
}
}
//getters and setters in Employee.h
//getters and setters
virtual float calculatePay();
virtual string toString();
void displayEmployee(Employee* emp);
};
// source.cpp
#include <iostream>
#include <string>
#include <conio.h>
#include "Employee.h"
#include "Hourly.h"
#include "Salary.h"
#include "Manager.h"
using namespace std;
int main()
{
//create three objects using each class
Hourly emp1("bob", "smith", "123456789", "5553243", 40.0f, 12.50f);
Salary emp2("shannon", "ray", "785426258", "5552685", 52000.00);
Manager emp3("fred", "hope", "846258456", "5558486", 60000.00, 10000.00);
//display the size of the Hourly employee
cout << "The size of the hourly employee is: " << sizeof(emp1) << endl;
cout << " The size of the pointer to the hourly employee is: " << sizeof(&emp1) << endl;
displayEmployee(emp1);
cout << "Press any key to continue...";
_getch();
return 0;
}
|