I have tried this too long , but haven\'t found solution yet. This is what I am
ID: 667895 • Letter: I
Question
I have tried this too long , but haven't found solution yet.
This is what I am doing as an assignment below.
Write the classes indicated for the following class diagram:
Employee class:
The Employee class represents an employee of a store and keeps track of the number of hours worked for each day of a week. The hours worked can be set and retrieved for any day of the week. The name cannot be changed. Various other derived information is available: the number of days worked and the total number of hours worked.
hours – An instance variable that is a an array of 7 elements. It holds the number of hours worked for each day of the week. Assume the week starts on Monday, e.g. position 0 is hours worked on Monday, position 1 is hours worked on Tuesday, etc.
name – An instance variable that holds the name of the employee.
Employee(name:string) – A constructor that accepts a name as an argument.
getHours(day:int):double – A method which returns the number of hours worked for day, where day takes on values 0, 1, ..., 6. For example, getHours(4) would return the number of hours worked on Friday.
setHours(day:int, hours:double) – A method which sets the number of hours worked for the indicated day. The day must be a value between 0 and 6, otherwise, the method should do nothing. For example, setHours(2,6.5) would set the total hours worked for Wednesday to 6.5.
numDaysWorked():int – A method which returns the total number of days worked. If a day has 0 hours, then it is not counted. For example, if hours held the values: 8,8,8,0,8,4,0 then this method would return 5.
totalHours():double – A method which returns the total hours worked for the week. For example, if hours held the values: 8,8,8,0,8,4,0 then this method would return 36.0
toString( ):string – A method which returns a message like this:
Dave Gibson worked 5 days for a total of 36.0 hours.
EmployeeTester class:
Write a main method that produces a dialog like this:
-->Enter a name : Dave
-->Enter hours worked (7 values separated by spaces): 5 2 1 0 0 3 3
Output:
Dave worked for 5 day(s) for a total of 14.0 hours.
-->Enter a day of the week (0-6): 2
Output:
Hours worked on day 2 is 1.0
Explanation / Answer
The problem with your assumption is, you are trying to declare the main() function inside a class which is not correct in C++. In C++ the main() function should be in global scope. So, the code should look like this:
#include <iostream>
#include <string>
#include <stdio.h>
#include <sstream>
using namespace std;
class Employee
{
private:
double hours[7];
string name;
public:
Employee(); //This is a constructor.
double getHours(int day);
void setHours(int day, double hrs);
int numDaysWorked();
double totalHours();
string toString();
};
Employee::Employee() //Constructor Definition.
{
cout<<"Enter a name: ";
cin>>name;
}
double Employee::getHours(int day) //getHours() Function.
{
return hours[day];
}
void Employee::setHours(int day, double hrs) //setHours() Function.
{
hours[day] = hrs;
}
int Employee::numDaysWorked() //numDayWorked() Function.
{
int count = 0;
for(int i=0;i<7;i++)
if(hours[i] != 0)
count++;
return count;
}
double Employee::totalHours() //totalHours() Function.
{
double hrs=0.0;
for(int i=0;i<7;i++)
hrs += hours[i];
return hrs;
}
string Employee::toString() //toString() Function.
{
std::stringstream ss;
ss<<name<<" worked for "<<numDaysWorked()<<" day(s) for a total of "<<totalHours()<<" hours.";
std::string strng = ss.str();
return strng;
}
//class EmployeeTester : public Employee
// {
// public:
int main()
{
double hours;
int day;
Employee e;
cout<<"Enter hours worked(7 values separated by spaces: )";
for(int i=0;i<7;i++)
{
cin>>hours;
e.setHours(i,hours);
}
cout <<e.toString();
cout<<"Enter a day of the week(0-6): ";
cin>>day;
cout<<"Hours worked on day "<<day<<" is "<<e.getHours(day)<<endl;
return 1;
}
//};
The code is verymuch self explanatory. You can also call the remaining member functions when and where necessary accordingly.