can't access public object inside a class

I declare an public object of 'Benefit' class inside 'Employee' class name 'benefit'. In the code below (bold area) should I be able to acheieve the following.
employee1.benefit.sethealthinsurance(healthinsurance)
???
after typing employee1. the benefit methods does not show up.

anyone can help me with this code.


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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
//Program Name:		Employee Class
//Programmer:		Bilal Hussain
//Class:	        CIS247C, Week 4 iLab

#include <iostream>
#include <string>
#include <stdlib.h>
#include <iomanip>

using namespace std;

void DisplayApplicationInformation();
void DisplayDivider(string);

//Abstract class
class iEmployee
{
public: 
	double calculatePay();
}


class Employee : public iEmployee
{
public:
	string firstName, lastName;
	string gender;
	int dependents;
	double annualSalary;
	static int numEmployees;		//week3 addon
public:
	Employee()
	{
		firstName = "";
		lastName = "";
		gender = 'U';
		dependents = 0;
		annualSalary = 20000;
		numEmployees=numEmployees++;		//week3 addon
	}

	//destructure
	~Employee();

	//Employee(string fname, string lname, char gen, int dept, double salary); //constructor with parameters
	Employee(string firstName, string lastName, string gender, int dependents, double salary)
	{
		this->firstName = firstName;
		this->lastName = lastName;
		this->gender = gender;
		this->dependents = dependents;
		this->annualSalary = salary;
		numEmployees=numEmployees++;		//week3 addon
	}

	double calculatePay();
	void displayEmployee();
	string getFirstName();
	void setFirstName(string fname);
	string getLastName();
	void setLastName(string lname);
	string getGender();
	void setGender(char gen);
	int getDependents();
	void setDependents(int dept);
	double getannualSalary();
	void setannualSalary(double salary);
	static int getNumEmployees();
	void setDependents(string);
	void setAnnualSalary(string);

	/*Week 4 addon*/
	 Benefit benefit;

};

/*Week 4 addon start*/
class Benefit
{
public:
	string healthinsurance;
	double lifeinsurance;
	int vacation;
public:
	Benefit()
	{

	};
	Benefit(string health, double life, int vacation)
	{
		this->healthinsurance=health;
		this->lifeinsurance=life;
		this->vacation=vacation;
	};

	void displayBenefits();
	string getHealthInsurance();
	void setHealthInsurance(string hins);
	double getLifeInsurance();
	void setLifeInsurance(double lifeIns);
	int getVacation();
	void setVacation(int vaca);

};

/*Week 4 addon end*/ 

//Declaration of methods and function
double Employee::calculatePay()
{
	return annualSalary/52;
}

//setters
void Employee::setFirstName(string fname)
{
	firstName= fname;
}
void Employee::setLastName(string lname)
{
	lastName = lname;
}
void Employee::setGender(char gen)
{
	gender = gen;
}
void Employee::setDependents(int dept)
{
	dependents = dept;
}
void Employee::setannualSalary(double salary)
{
	annualSalary = salary;
}

//getters
string Employee::getFirstName()
{
	return firstName;
}
string Employee::getLastName()
{
	return lastName;
}
string Employee::getGender()
{
	return gender;
}
int Employee::getDependents()
{
	return dependents;
}
double Employee::getannualSalary()
{
	return annualSalary;
}
void Employee::displayEmployee()
{
	cout << "Name: \t\t\t" << firstName << " " << lastName << endl;
	cout << "Gender: \t\t\t" << gender << endl;
	cout << "Dependents: \t\t\t" << dependents << endl;
	cout << "Annual Salary: \t\t\t" << setprecision(2) << showpoint << fixed << annualSalary << endl;
	//cout << "Weekly Salary: \t\t\t" << firstName << endl;
}

int Employee::getNumEmployees()			//week3 addon
{
	return numEmployees;
}



Employee::~Employee () {
	firstName, lastName = "";
	gender="";
	dependents=0;
	annualSalary=0;
}

// week 4 addon start
// Benefit class methods and functions

//setters
void Benefit::setHealthInsurance(string hins)
{
	healthinsurace = hins;
}
void Benefit::setLifeInsurance(double lifeIns)
{

	lifeinsurace= lifeins;
}
void Benefit::setVacation(int vaca)
{
	vacation = vaca;
}

//getters
string Benefit::getHealthInsurance()
{
	return healthinsurance:
}
double Benefit::getLifeInsurance()
{
	return lifeinsurance;
}
int Benefit::getVacation()
{
	return vacation;
}
void Benefit::displayBenefits()
{
	cout << "Health Insurance: \t\t\t" << healthinsurance << endl;
	cout << "Life Insurance: \t\t\t" << lifeinsurance << endl;
	cout << "Vacation: \t\t\t" << vacation << endl;
}

// week 4 addon ends

int Employee::numEmployees = 0;		//week3 addon

int main()
{
	int dependents;
	string fn, ln;
	char gender;
	double annualSalary; 
	string healthinsurance;
	double lifeinsurance;
	int vacation;

	Employee employee1;
	DisplayApplicationInformation();
	DisplayDivider("Employee 1");
	cout <<"Please Enter your First Name: ";
	cin >>fn;
	employee1.setFirstName(fn);

	cout <<"Please Enter your Last Name: ";
	cin >>ln;
	employee1.setLastName(ln);

	cout << "Please Enter your Gender(M/F): ";
	cin >> gender;
	employee1.setGender(gender);

	cout << "Please Enter your Dependents: ";
	cin >> dependents;
	employee1.setDependents(dependents);

	cout << "Please Enter your Annual Salary: ";
	cin >> annualSalary;
	employee1.setannualSalary(annualSalary);
	// week 4 addon start
	cout << "Please enter your Health Insurace: ";
	cin >> healthinsurace;
	employee1. //employee1.benefit.sethealthinsurace(healthinsurance);// I CANNOT ACCESS THIS. IT DOES NOT POP UP.
	// week 4 addon end



	cout << "Employee Information" <<endl;
	cout << "_________________________________________________" << endl;
	employee1.displayEmployee();
	cout << "Weekly Salary: \t\t\t" << employee1.calculatePay() << endl;

//week3 add on////////////////////////////////////////////////////////////
	cout <<"--- Number of Employee Object Created ---" << endl;
	cout <<"Number of employee: "<< Employee::getNumEmployees() <<endl <<endl;
//week3 add on////////////////////////////////////////////////////////////

	DisplayDivider("Employee 2");
	cout << "Employee Information" <<endl;
	cout << "_________________________________________________" << endl;
//	an object with default constructure with parameters
	Employee employee2("Ayesha","Hussain","F",2,16800);
	employee2.displayEmployee();
	cout << "Weekly Salary: \t\t\t" << employee2.calculatePay() << endl;

//week3 add on////////////////////////////////////////////////////////////
	cout <<"--- Number of Employee Object Created ---" << endl;
	cout <<"Number of employee: "<< Employee::getNumEmployees() <<endl <<endl ;
//week3 add on////////////////////////////////////////////////////////////

	DisplayDivider("Employee 3");
	cout << "Employee Information" <<endl;
	cout << "_________________________________________________" << endl;
//	an object with default constructure with parameters
	Employee employee3("Mary","Noia","F",5,24000.00);
	employee3.displayEmployee();
	cout << "Weekly Salary: \t\t\t" << employee2.calculatePay() << endl;

//week3 add on////////////////////////////////////////////////////////////
	cout <<"--- Number of Employee Object Created ---" << endl;
	cout <<"Number of employee: "<< Employee::getNumEmployees() <<endl <<endl ;
//week3 add on////////////////////////////////////////////////////////////
	cout <<"The end of the CIS247C Week3 iLab.";

	system("pause");

}



//Function Declaration
void DisplayApplicationInformation()
{
	cout << "Welcome to my first Object Oriented Program--Employee Class CIS247C, Week 3 iLab.";
	cout << "Name: Bilal Hussain\n";
}
void DisplayDivider(string outputTitle)
{
	cout << "****************" + outputTitle + "****************" << endl;
}


Last edited on
You need to move the definition of class Benefit above the definition of class Enployee.

As it stands, when the compiler bumps into Benefit benefit;, it doesn't know what Benefit is so it should complain, telling you that.
Just a lot of spelling errors and you tried to use Benefit in Employee before it was defined. Search for CHANGED in this code to see what I changed. It compiles for me.
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
//Program Name:		Employee Class
//Programmer:		Bilal Hussain
//Class:	        CIS247C, Week 4 iLab

#include <iostream>
#include <string>
#include <stdlib.h>
#include <iomanip>

using namespace std;

void DisplayApplicationInformation();
void DisplayDivider(string);

//Abstract class
class iEmployee
{
public: 
	double calculatePay();
};


// CHANGED - I moved this so that the definition
//           of the class is before you use it
/*Week 4 addon start*/
class Benefit
{
public:
	string healthinsurance;
	double lifeinsurance;
	int vacation;
public:
	Benefit()
	{

	};
	Benefit(string health, double life, int vacation)
	{
		this->healthinsurance=health;
		this->lifeinsurance=life;
		this->vacation=vacation;
	};

	void displayBenefits();
	string getHealthInsurance();
	void setHealthInsurance(string hins);
	double getLifeInsurance();
	void setLifeInsurance(double lifeIns);
	int getVacation();
	void setVacation(int vaca);

};

/*Week 4 addon end*/ 
class Employee : public iEmployee
{
public:
	string firstName, lastName;
	string gender;
	int dependents;
	double annualSalary;
	static int numEmployees;		//week3 addon
public:
	Employee()
	{
		firstName = "";
		lastName = "";
		gender = 'U';
		dependents = 0;
		annualSalary = 20000;
		numEmployees=numEmployees++;		//week3 addon
	}

	//destructure
	~Employee();

	//Employee(string fname, string lname, char gen, int dept, double salary); //constructor with parameters
	Employee(string firstName, string lastName, string gender, int dependents, double salary)
	{
		this->firstName = firstName;
		this->lastName = lastName;
		this->gender = gender;
		this->dependents = dependents;
		this->annualSalary = salary;
		numEmployees=numEmployees++;		//week3 addon
	}

	double calculatePay();
	void displayEmployee();
	string getFirstName();
	void setFirstName(string fname);
	string getLastName();
	void setLastName(string lname);
	string getGender();
	void setGender(char gen);
	int getDependents();
	void setDependents(int dept);
	double getannualSalary();
	void setannualSalary(double salary);
	static int getNumEmployees();
	void setDependents(string);
	void setAnnualSalary(string);

	/*Week 4 addon*/
	 Benefit benefit;

};


//Declaration of methods and function
double Employee::calculatePay()
{
	return annualSalary/52;
}

//setters
void Employee::setFirstName(string fname)
{
	firstName= fname;
}
void Employee::setLastName(string lname)
{
	lastName = lname;
}
void Employee::setGender(char gen)
{
	gender = gen;
}
void Employee::setDependents(int dept)
{
	dependents = dept;
}
void Employee::setannualSalary(double salary)
{
	annualSalary = salary;
}

//getters
string Employee::getFirstName()
{
	return firstName;
}
string Employee::getLastName()
{
	return lastName;
}
string Employee::getGender()
{
	return gender;
}
int Employee::getDependents()
{
	return dependents;
}
double Employee::getannualSalary()
{
	return annualSalary;
}
void Employee::displayEmployee()
{
	cout << "Name: \t\t\t" << firstName << " " << lastName << endl;
	cout << "Gender: \t\t\t" << gender << endl;
	cout << "Dependents: \t\t\t" << dependents << endl;
	cout << "Annual Salary: \t\t\t" << setprecision(2) << showpoint << fixed << annualSalary << endl;
	//cout << "Weekly Salary: \t\t\t" << firstName << endl;
}

int Employee::getNumEmployees()			//week3 addon
{
	return numEmployees;
}



Employee::~Employee () {
	firstName, lastName = "";
	gender="";
	dependents=0;
	annualSalary=0;
}

// week 4 addon start
// Benefit class methods and functions

//setters
void Benefit::setHealthInsurance(string hins)
{
// CHANGED - Spelling
	healthinsurance = hins;
}
void Benefit::setLifeInsurance(double lifeIns)
{

// CHANGED - Spelling
	lifeinsurance= lifeIns;
}
void Benefit::setVacation(int vaca)
{
	vacation = vaca;
}

//getters
string Benefit::getHealthInsurance()
{
// CHANGED - Spelling
	return healthinsurance;
}
double Benefit::getLifeInsurance()
{
	return lifeinsurance;
}
int Benefit::getVacation()
{
	return vacation;
}
void Benefit::displayBenefits()
{
	cout << "Health Insurance: \t\t\t" << healthinsurance << endl;
	cout << "Life Insurance: \t\t\t" << lifeinsurance << endl;
	cout << "Vacation: \t\t\t" << vacation << endl;
}

// week 4 addon ends

int Employee::numEmployees = 0;		//week3 addon

int main()
{
	int dependents;
	string fn, ln;
	char gender;
	double annualSalary; 
	string healthinsurance;
	double lifeinsurance;
	int vacation;

	Employee employee1;
	DisplayApplicationInformation();
	DisplayDivider("Employee 1");
	cout <<"Please Enter your First Name: ";
	cin >>fn;
	employee1.setFirstName(fn);

	cout <<"Please Enter your Last Name: ";
	cin >>ln;
	employee1.setLastName(ln);

	cout << "Please Enter your Gender(M/F): ";
	cin >> gender;
	employee1.setGender(gender);

	cout << "Please Enter your Dependents: ";
	cin >> dependents;
	employee1.setDependents(dependents);

	cout << "Please Enter your Annual Salary: ";
	cin >> annualSalary;
	employee1.setannualSalary(annualSalary);
	// week 4 addon start
	cout << "Please enter your Health Insurace: ";
	cin >> healthinsurance;
        // CHANGE - Spelling
	employee1.benefit.setHealthInsurance(healthinsurance);// I CANNOT ACCESS THIS. IT DOES NOT POP UP.
	// week 4 addon end



	cout << "Employee Information" <<endl;
	cout << "_________________________________________________" << endl;
	employee1.displayEmployee();
	cout << "Weekly Salary: \t\t\t" << employee1.calculatePay() << endl;

//week3 add on////////////////////////////////////////////////////////////
	cout <<"--- Number of Employee Object Created ---" << endl;
	cout <<"Number of employee: "<< Employee::getNumEmployees() <<endl <<endl;
//week3 add on////////////////////////////////////////////////////////////

	DisplayDivider("Employee 2");
	cout << "Employee Information" <<endl;
	cout << "_________________________________________________" << endl;
//	an object with default constructure with parameters
	Employee employee2("Ayesha","Hussain","F",2,16800);
	employee2.displayEmployee();
	cout << "Weekly Salary: \t\t\t" << employee2.calculatePay() << endl;

//week3 add on////////////////////////////////////////////////////////////
	cout <<"--- Number of Employee Object Created ---" << endl;
	cout <<"Number of employee: "<< Employee::getNumEmployees() <<endl <<endl ;
//week3 add on////////////////////////////////////////////////////////////

	DisplayDivider("Employee 3");
	cout << "Employee Information" <<endl;
	cout << "_________________________________________________" << endl;
//	an object with default constructure with parameters
	Employee employee3("Mary","Noia","F",5,24000.00);
	employee3.displayEmployee();
	cout << "Weekly Salary: \t\t\t" << employee2.calculatePay() << endl;

//week3 add on////////////////////////////////////////////////////////////
	cout <<"--- Number of Employee Object Created ---" << endl;
	cout <<"Number of employee: "<< Employee::getNumEmployees() <<endl <<endl ;
//week3 add on////////////////////////////////////////////////////////////
	cout <<"The end of the CIS247C Week3 iLab.";

	system("pause");

}



//Function Declaration
void DisplayApplicationInformation()
{
	cout << "Welcome to my first Object Oriented Program--Employee Class CIS247C, Week 3 iLab.";
	cout << "Name: Bilal Hussain\n";
}
void DisplayDivider(string outputTitle)
{
	cout << "****************" + outputTitle + "****************" << endl;
}
Thanks you guyz. Your prompt help saved me a lot of time :) ...will keep in touch. I still have more addon coming into this class..
Topic archived. No new replies allowed.