I'm stuck on this assignment

Ok here is the taskings: The objective of the lab is to take the UML Class diagram and enhance last week's Employee class by making the following changes:

Create a static variable called numEmployees that holds an int and initialize it to zero. This will allow us to count all the Employee objects created in the main class.
Increment numEmployees in all of the constructors
Add overloaded versions of setDependents and setAnnualSalary that accept strings. This way, we will have two "set" methods for both dependents and annual salary; one that accepts a string, and one that accepts its default data type.

this is the code i got so far
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#include <iostream>
#include <string>
#include <stdlib.h>
#include <iomanip>

using namespace std;

    const double MIN_SALARY = 50000;
    const double MAX_SALARY = 250000;
    const int MAX_DEPENDENTS = 10;
    const int MIN_DEPENDENTS = 0;
    const char DEFAULT_GENDER = 'N'; //N stands for not identified
    const int NUMBER_WEEKS = 52;

//Employee class creation
class Employee
{
    string firstName;
    string lastName;
    char gender;
    int dependents;
    double annualSalary;
    static int numEmployees; //wk3 adds

    public:

        Employee() // Default constructor created
        {
            firstName = "";
            lastName = "";
            gender = 'N';
            annualSalary = 50000;
        }


        Employee(string firstName, string lastName, char gender, int dependents, double salary)
        {
            this->firstName = firstName;
            this->lastName = lastName;
            this->gender = gender;
            this->dependents = dependents;
            this->annualSalary = annualSalary;
        }
        //Setters and Getters for each class attributes

        string getFirstName()
        {
            return firstName;
        }
        void setFirstName(string name)
        {
            firstName = name;
        }
        string getLastName()
        {
            return lastName;
        }
        void setLastName(string name)
        {
            lastName = name;
        }
        char getGender()
        {
            return gender;
        }
        void setGender(char gen)
        {
            switch (gen)
            {
                case'f': case'F': case'M': case'm':
                gender = gen;
                break;
                default:
                gender = DEFAULT_GENDER;
            }
        }
        int getDependents()
        {
            return dependents;
        }
        void setDependents(int dep)
        {
            if (dep >= MIN_DEPENDENTS && dep <= MAX_DEPENDENTS)
            {
                dependents = dep;
            }
            else if (dep < MIN_DEPENDENTS)
            {
                dep = MIN_DEPENDENTS;
            }
            else
            {
                dependents = MAX_DEPENDENTS;
            }
        }
        double getAnnualSalary()
        {
            return annualSalary;
        }
        void setAnnualSalary(double salary)
        {
            if(salary >= MIN_SALARY && salary <= MAX_SALARY)
            {
                annualSalary = salary;
            }
            else if (salary < MIN_SALARY)
            {
                annualSalary = MIN_SALARY;
            }
            else
            {
                annualSalary = MAX_SALARY;
            }
        }
        //calculate employee salary
        double calculatePay()
        {
            return annualSalary/NUMBER_WEEKS;
        }
        // function to display employee info
        void displayEmployee()
        {
            cout<<"First Name:\t"<<firstName<<endl; // show the first name of employee
            cout<<"Last Name:\t"<<lastName<<endl; //show the last name of employee
            cout<<"Gender:\t"<<gender<< "\n"; //show the gender of employee
            cout<<"Dependents:\t"<<dependents<< "\n"; // show the number of dependents employee have
            cout<<"Annual Salary:\t"<</* setprecision(2)<<showpoint<<fixed<<*/annualSalary << "\n"; //show annual salary for employee
            cout<<"Weekly Salary:\t"<</* setprecision(2)<<showpoint<<fixed<<*/calculatePay(); //show weekly salary for employee
        }


};//end of employee class

        //Functions for displaying application info
        void DisplayApplicationInformation()
        {
            cout<<"Welcome to my Employee Class Design"<<endl;
            cout<<"Wright, Christopher"<<endl;
            cout<<"CIS247C Week Two Lab"<<endl;
        }//end of void DisplayApplicationInformation function

        //Function for display divider
        void DisplayDivider(string message)
        {
            cout<<"\n************ " + message + " ************\n";
        }//end of DisplayDivider function

        //Function to get user input
        string GetUserInput(string message)
        {
            string mystring;
            cout<<"Please enter your "<<message;
            getline(cin, mystring);
            return mystring;

        }//end of GetUserInput

        //Function to terminate application
        void TerminateApplication()
        {
            cout<<"\nThanks for using my class design. \n";
        }//end of TerminateApplication


int main()
{
    //employee objectss
    Employee employee1;
    char gender;
    string str;

    //Application Information
    DisplayApplicationInformation();

    //Screen organizer
    DisplayDivider("Employee1");

    //Get the user information
    employee1.setFirstName(GetUserInput("First Name "));
    employee1.setLastName(GetUserInput("Last Name "));

    str = GetUserInput("Gender ");
    gender = str.at(0);
    employee1.setGender(gender);

    employee1.setDependents(atoi(GetUserInput("Dependents ").c_str()));
    employee1.setAnnualSalary(atoi(GetUserInput("Annual Salary ").c_str()));
    employee1.displayEmployee();

    DisplayDivider("Employee 2");
    //second employee object
    Employee employee2("Nathalie", "Delicia", 'F', 2, 150000);
    employee2.displayEmployee();

    TerminateApplication();

    return 0;

}//end of int main function 
Was there a question?

One thing I notice:

There is a convention that one should name the member variables with a leading m_ , so you could do this:

m_FirstName = firstName;

Rather than:

this->firstName = firstName;

Also, you code hard wires 2 employees, it would be better if you used a vector of employees, then you could use a loop to enter their info, and an iterator to do things with them. You should always avoid repeating code to deal with multiple objects.

Another convention is to put the class declaration into it's own header file, with the definitions of the functions in it's own .cpp file. Then include the header file in whichever file needs to use the class. If you are using an IDE, it can do this for you - plus a whole lot of other stuff as well.

With functions that are used by main, declare these before main, then provide definitions of them after main. Consider making the GetUserInput a class function, because it is about the class info.

HTH - look forward to seeing what your question was. :)

the question is how can i add the numEmployee and make it initialize to zero and also when the program runs how can i make it count how many people i used in the program. the hard coded personel are intended
Make numEmployee a static member variable, increment it in the constructor.

HTH
Topic archived. No new replies allowed.