Hello again fellow programmers. I'm taking a part two class of C++ for those who remember me last semester.
I've finally understood the concepts of classes fairly well, but I'm not understanding why my program is not working. Before I spill code, know that the method used is as followed:
* Header File
* Implementation File
* Test file (test methods)
XCode isn't friendly and will not let me have 'manager.h' and 'manager.cpp' in the same project. Oh well. I think my problem deals with Inheritance between two header files 'employee.h' and 'manager.h'.
Here's the implementation file:
Header File 'manager.h'
Finally, the test file 'testmanager.cpp'
Sorry for the indentations but you can thank the forum for that. 😀 Any suggestions would be appreciated.
I've finally understood the concepts of classes fairly well, but I'm not understanding why my program is not working. Before I spill code, know that the method used is as followed:
* Header File
* Implementation File
* Test file (test methods)
XCode isn't friendly and will not let me have 'manager.h' and 'manager.cpp' in the same project. Oh well. I think my problem deals with Inheritance between two header files 'employee.h' and 'manager.h'.
Here's the implementation file:
Code:
// manager.cpp // implements Manager class
// 01/29/2007
#include <iostream>
#include <string>
#include "manager.h" // include declaration of Manager class
#include "employee.h"
using namespace std;
Manager::Manager(int theId, string theName)
{
// Initialize fields inherited from parent class Employee
// Initialize fields of class Manager
hrsWorked = 0;
for(int i=0; i<5; i++)
hours[i] = 0;
}
void Manager::setHours(int theHours[])
{
for(int i=0; i<5; i++)
{
hours[i] = theHours[i];
hrsWorked += hours[i];
}
}
int Manager::getTotalHours()
{
return hrsWorked;
}
void Manager::setRate(double payRate)
{
ratePerHour = payRate;
}
double Manager::getRate()
{
return ratePerHour;
}
void Manager::setBonus(double bonus)
{
overtimePay = bonus;
}
double Manager::getBonus()
{
return overtimePay;
}
void Manager::calcSalary()
{
salary = hrsWorked * ratePerHour + overtimePay;
}
void Manager::printInfo()
{
cout << "*********************************" << endl;
cout << "Hours Worked: " << hrsWorked << endl;
cout << "Rate per Hour: " << ratePerHour << endl;
cout << "Overtime Pay: " << overtimePay << endl;
cout << "Total Salary: " << salary << endl;
cout << "*********************************" << endl;
}
Header File 'manager.h'
Code:
// manager.h // declares Manager class
// 01/29/2007
#ifndef MANAGER_H
#define MANAGER_H
#include <string>
#include "employee.h" // include declaration of Employee class
using namespace std;
class Manager : public Employee
{
protected:
int hours[5]; // number of hours worked per day
int hrsWorked; // total hours worked
double ratePerHour; // pay rate per hour
double overtimePay; // bonus
public:
// Postcondition: this name has been initialized from the given parameters
Manager(int theId, string theName);
// Postcondition: the number of hours worked per day has been set from the given array.
// the total hours worked has been computed.
void setHours(int theHours[]);
// Precondition: the hoursWorked has been computed.
// Postcondition: returns the total number of hours worked.
int getTotalHours();
// Postcondition: the pay rate per hour has been set from the given value.
void setRate(double payRate);
// Postcondition: returns the payRate.
double getRate();
// Postcondition: the bonus pay has been set.
void setBonus(double bonus);
// Postcondition: returns the bonus.
double getBonus();
// Postcondition: the salary has been computed.
void calcSalary();
// Postcondition: All information about this employee has been displayed.
void printInfo();
// For Extra Credits
// Postcondition: The current manager (calling object) contains a copy of otherManager.
// void operator= (const Manager& otherManager);
// if you would allow the assignment like a = b = c; use the following solution
// const Manager& operator= (const Manager& otherManager);
}; // Manager
#endif
Finally, the test file 'testmanager.cpp'
Code:
// testmanager.cpp
// Jonathan Andrew Scott
// CSCI 2010
// 2/5/2007
#include <iostream>
#include "manager.h"
using namespace std;
int main()
{
int hrsMgr1[5] = {5, 6, 7, 3, 6};
int hrsMgr2[5] = {4, 5, 4, 7, 5};
Manager mgr1(101, "Andrew Scott");
Manager mgr2(102, "Kyle Gregory");
cout << "Test all methods \n";
mgr1.setHours(hrsMgr1);
mgr1.setRate(7.00);
mgr1.setBonus(150.00);
mgr1.calcSalary();
cout << "ID: " << mgr1.getID() << endl;
cout << "Name: " << mgr1.getName() << endl;
cout << "Total Hours Worked Per Week: " << mgr1.getTotalHours() << endl;
cout << "Hourly Rate of Pay: " << mgr1.getRate() << endl;
cout << "Bonus Pay This Week: " << mgr1.getBonus() << endl;
cout << "Gross Pay: " << mgr1.getSalary() << endl;
cout << endl;
return 0;
}
Sorry for the indentations but you can thank the forum for that. 😀 Any suggestions would be appreciated.