Trouble with classes and inheritance

I'm currently a DeVry University student studying Game Simulation and Programming and my assignment, for this week, has me stumped. I have a number of questions and problems with my code that I need help with. I'm not asking for the answers but an explanation using my code or code in general, I'm a visual learner with things like this, to point me in the right direction.

The main issue I'm having is being able to use my Benefits class correctly. With it being protected I can't access it the way I have it yet. Another is how to properly inherit the employee class variables to the other classes.
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#include <iostream>
#include <string>
#include <cstring>
#include <iomanip>

using namespace std;

/***********************************
* Function Prototypes *
***********************************/
void DisplayApplicationInformation();
void DisplayDivider(string);
string GetInput(string);
void TerminateApplication();

/***********************************
* Global Variables *
***********************************/
string input;

/***********************************
* Benefit Class Definition *
***********************************/
class Benefit: public Employee{
private:
	string healthInsurance;
	double lifeInsurance;
	int vacation;
public:
	void benefit();
	void benefit(string health, double life, int vacation);
	void displayBenefits();
	string getHealthInsurance();
	void setHealthInsurance(string hIns);
	double getLifeInsurance();
	void setLifeInsurance(double lifeIns);
	int getVacation();
	void setVacation(int vaca);
};

/***********************************
* iEmployee Interface Class *
***********************************/
class iEmployee:public Employee{
public:
	virtual double calculateWeeklyPay(double) = 0;
};

/***********************************
* Employee Class Definition *
***********************************/
class Employee{
protected:
	string firstName;
	string lastName;
	char gender;
	int dependents;
	double annualSalary;
	Benefit benefit;
private:
	static int numEmployees;
public:
	void employee();
	void employee(string first, string last, char gen, int dep, double salary);
	double calculateWeeklyPay(double);
	void displayEmployee();
	string getFirstName();
	void setFirstName(string first);
	string getLastName();
	void setLastName(string last);
	char getGender();
	void setGender(char gen);
	int getDependents();
	void setDependents(int dep);
	double getAnnualSalary();
	void setAnnualSalary(double salary);
	static int getNumEmployees();
	void setDependents(string dependents);
	void setAnnualSalary(string salary);
};

/***********************************
* Salaried Class Definition *
***********************************/
class Salaried : public Employee{
private:
	const int MIN_MANAGEMENT_LEVEL;
	const int MAX_MANAGEMENT_LEVEL;
	const double BONUS_PERCENT;
	int managementLevel;
public:
	void salaried();
	void salaried(string first, string last, char gen, int dep, double salary, Benefit ben, int manLevel);
	void salaried(double salary, int manLevel);
	double Calculate();
	void displayEmployee();
	int getManagementLevel();
	void setManagementLevel(int manLevel);
	void setAnnualSalary(string salary);
};

/***********************************
* Hourly Class Definition *
***********************************/
class Hourly:public Employee{
private:
	const double MIN_WAGE;
	const double MAX_WAGE;
	const double MIN_HOURS;
	const double MAX_HOURS;
	double wage;
	double hours;
	string category;
public:
	void hourly();
	void hourly(double wage, double hours, string category);
	void hourly(string first, string last, char gen, int dep, double salary, Benefit ben, int manLevel);
	double calculatePay();
	void setAnnualSalary();
	void displayEmployee();
	double getWage();
	void setWage(double wage);
	double getHours();
	void setHours(double hours);
	string getCategory();
	void setCategory(string category);
};

/***********************************
* Employee Class Methods *
***********************************/
void Employee::employee(){
	firstName = "Not given";
	lastName = "Not given";
	gender = 'U';
	dependents = 0;
	annualSalary = 20,000;
	numEmployees++;
}
void Employee::employee(string first, string last, char gen, int dep, double salary){
	first = firstName;
	last = lastName;
	gen = gender;
	dep = dependents;
	salary = annualSalary;
	numEmployees++;
}
double Employee::calculateWeeklyPay(double){
	return annualSalary/52;
}
void Employee::displayEmployee(){
	numEmployees++;
	cout << "Name: " << lastName << ", " << firstName << endl;
    cout << "Gender: " << gender << endl;
    cout << "Dependents: " << dependents << endl;
    cout << "Annual Salary:\t" << setprecision(2) << showpoint << fixed << annualSalary << "\n";
	cout << "Weekly Pay:\t" << setprecision(2) << showpoint << fixed << annualSalary/52 << "\n";
}
string Employee::getFirstName(){
	return firstName;
}
void Employee::setFirstName(string first){
	firstName = GetInput("Employee's First Name");
}
string Employee::getLastName(){
	return lastName;
}
void Employee::setLastName(string last){
	lastName = GetInput("Employee's Last Name");
}
char Employee::getGender(){
	return gender;
}
void Employee::setGender(char gen){
	input = GetInput("Employee's Gender");
		gender = input[0];
}
int Employee::getDependents(){
	return dependents;
}
void Employee::setDependents(int dep){
	input = GetInput("Number of Employee's Dependents");
		dependents = atoi(input.c_str());
}
double Employee::getAnnualSalary(){
	return annualSalary;
}
void Employee::setAnnualSalary(double salary){
	input = GetInput("Employee's Salary");
		annualSalary = atof(input.c_str());
}
int Employee::numEmployees = 0;
int Employee::getNumEmployees(){
	return numEmployees;
}
void Employee::setDependents(string dep){
	input = GetInput("Number of Employee's Dependents");
		dependents = atoi(input.c_str());
}
void Employee::setAnnualSalary(string salary){
	input = GetInput("Employee's Salary");
		annualSalary = atoi(input.c_str());
}

/***********************************
* Benefit Class Methods *
***********************************/
void Benefit::benefit(){
	healthInsurance = "Not given";
	lifeInsurance = 0;
	vacation = 0;
}
void Benefit::benefit(string health, double life, int vaca){
	health = healthInsurance;
	life = lifeInsurance;
	vaca = vacation;
}
void Benefit::displayBenefits(){
	cout << "Health Insurance: " << healthInsurance << endl;
	cout << "Life Insurance: " << lifeInsurance << endl;
	cout << "Vacation: " << vacation << endl;
}
string Benefit::getHealthInsurance(){
	return healthInsurance;
}
void Benefit::setHealthInsurance(string hIns){
	input = GetInput("Health Insurance");
	healthInsurance = input;
}
double Benefit::getLifeInsurance(){
	return lifeInsurance;
}
void Benefit::setLifeInsurance(double lifeIns){
	input = GetInput("Life Insurance");
	lifeInsurance = atoi(input.c_str());
}
int Benefit::getVacation(){
	return vacation;
}
void Benefit::setVacation(int vaca){
	input = GetInput("Vacation Time");
	vacation = atoi(input.c_str());
}
Last edited on
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
/***********************************
* Salaried Class Methods *
***********************************/
void Salaried::salaried(){
	const int MIN_MANAGEMENT_LEVEL = 0;
	const int MAX_MANAGEMENT_LEVEL = 3;
	const double BONUS_PERCENT = 10;
	int managementLevel = 0;
}
void Salaried::salaried(string first, string last, char gen, int dep, double salary, Benefit ben, int manLevel){
	manLevel = managementLevel;
}
void Salaried::salaried(double salary, int manLevel){
	salary = annualSalary;
	
}
double Salaried::Calculate(){
	double bonusPercentage;
	bonusPercentage = managementLevel * .1;
	annualSalary = bonusPercentage + annualSalary;
	return annualSalary;
}
void Salaried::displayEmployee(){

}
int Salaried::getManagementLevel(){
	return managementLevel;
}
void Salaried::setManagementLevel(int manLevel){

}
void Salaried::setAnnualSalary(string salary){

}

/***********************************
* Hourly Class Methods *
***********************************/
void Hourly::hourly(){
	const double MIN_WAGE = 10;
	const double MAX_WAGE = 75;
	const double MIN_HOURS = 0;
	const double MAX_HOURS = 50;
	double wage = 0.0;
	double hours = 0.0;
	string category = "X";
}
void Hourly::hourly(double wage, double hours, string category){
	//initialize all attributes using values passed in parameter list
}
void Hourly::hourly(string first, string last, char gen, int dep, double salary, Benefit ben, int manLevel){
	//invoke appropriate base constructor and pass arguments to the base class constructor
}
double Hourly::calculatePay(){
	//override calculatePay() by multiplying wages by the hours
	return 0;
}
void Hourly::setAnnualSalary(){
	//overload Employee::setAnnualSalary() and set annual salary by multiplying weekly pay by 50. Must call calculatePay() to get the weekly pay
}
void Hourly::displayEmployee(){
	//overload displayEmployee() to add the category to hourly employee information
}
double Hourly::getWage(){
	return wage;
}
void Hourly::setWage(double wage){
	//assign value to wage
}
double Hourly::getHours(){
	return hours;
}
void Hourly::setHours(double hours){
	//assign value to hours
}
string Hourly::getCategory(){
	return category;
}
void Hourly::setCategory(string category){
	//assign category, valid types are "temporary", "part time" and "full time"
}

/***********************************
* Main Function *
***********************************/
int main(){

	Employee employeeObject;
	DisplayApplicationInformation();
	
	employeeObject.employee();
	DisplayDivider("Employee Information");
	employeeObject.setFirstName("");
	employeeObject.setLastName("");
	employeeObject.setGender('g');
	employeeObject.setDependents('d');
	employeeObject.setAnnualSalary('s');
	DisplayDivider("Benefit Information");
	employeeObject.benefit.setHealthInsurance("");
	employeeObject.benefit.setLifeInsurance('l');
	employeeObject.benefit.setVacation('v');
	DisplayDivider("Employee Information Results");
	employeeObject.displayEmployee();
	employeeObject.getFirstName();
	employeeObject.getLastName();
	employeeObject.getGender();
	employeeObject.getDependents();
	employeeObject.getAnnualSalary();
	employeeObject.calculateWeeklyPay('w');
	DisplayDivider("Benefits Results");
	employeeObject.benefit.displayBenefits();

	Employee employeeObject2;

	employeeObject2.employee();
	DisplayDivider("Employee Information");
	employeeObject2.setFirstName("");
	employeeObject2.setLastName("");
	employeeObject2.setGender('g');
	employeeObject2.setDependents('d');
	employeeObject2.setAnnualSalary('s');
	DisplayDivider("Benefit Information");
	employeeObject2.benefit.setHealthInsurance("");
	employeeObject2.benefit.setLifeInsurance('l');
	employeeObject2.benefit.setVacation('v');
	DisplayDivider("Employee Information Results");
	employeeObject2.displayEmployee();
	employeeObject2.getFirstName();
	employeeObject2.getLastName();
	employeeObject2.getGender();
	employeeObject2.getDependents();
	employeeObject2.getAnnualSalary();
	employeeObject2.calculateWeeklyPay('w');
	DisplayDivider("Benefits Results");
	employeeObject2.benefit.displayBenefits();

	DisplayDivider("Number of Employee's Entered");
	Employee::getNumEmployees();

	TerminateApplication();


	system("Pause");
	return 0;
}
void DisplayApplicationInformation(){
	cout << "Welcome to the Basic User Interface Program" << endl;
	cout << "CIS247, Week 5 Lab" << endl;
	cout << "Name: Walter Butler" << endl;
	cout << "This program creates a database for employee information" << endl;
}
void DisplayDivider(string outputTitle){
	cout << "***************" + outputTitle + "***************" << endl;
}
string GetInput(string inputType){
	cout << "Enter the " << inputType << " ";
	cin >> input;

	return input;
}
void TerminateApplication(){
	
	cout << "Thank you for using the Basic User Interface Program" << endl;
}
Last edited on
I find your class hierarchy a bit odd. Inheritance models an "isA" relationship, but in your hierarchy it says "Benefit isA employee"? Seems more of a "hasA" relationship.

For "inheriting", you need to call the proper constructor of the base class inside the constuctor of the derived class (usually by means of the initializer list). For example:
1
2
3
4
5
6
7
8
class Shape {
    string name;
    Shape Shape(string n) : name(n) {}
}
class Triangle : Shape {
    int size;
    Triangle Triangle(string n, int s) : Shape(n), size(s) {}
}

See how the derived class' constructor calls the base class' constructor.

Now, to "use" it: any Triangle object can use any functions of the Triangle class, as well as those of the Shape class [with some exceptions]. Use it as if you copypasted the members of the base class into the derived class.
I realized and fixed that mistake. I fixed it in the above but my next biggest isue is using the salaried and hourly classes.I can't seem how to overload the setAnnualSalary function that is used in the Hourly class. Right now I have this:

1
2
3
void Hourly::setAnnualSalary(){
return setAnnualSalary(calculateWeeklyPay()*annualSalary);
}


I'm not sure if it's right or not though.

I'm stil having the issue of the Benefits class working properly though. If it were under public instead of protected I know it would work but it's giving me errors for being protected.
Last edited on
Topic archived. No new replies allowed.