Stumped

Pages: 12
I have been working on this for hours and cannot find the problem, I know it has to be something stupid. Everything displays correctly except employee 1's benefit information, it gives me the default info and not the info that is input in when prmopted. Also, any advice or suggestions on the code itself (comments, organization,ect)would be welcomed.

Benefit.h
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
// Preprocessor Directives
#pragma once
#include <string>
using namespace std;
//----------------------------------------------------------------------------------------------
// Declare Prototypes
	string getHealthInsurance(string);
	string setHealthInsurance(string);
	double getLifeInsurance(double);
	double setLifeInsurance(double);
	int getVacation(int);
	int setVacation(int);
	void displayBenefits();
//----------------------------------------------------------------------------------------------
// Define Benefit Class
class Benefit
{
public:
	// Constructors and Destructor
	Benefit();
	Benefit(string, double, int);
	~Benefit();

	// Methods to Access Attributes
	void displayBenefits();
	
	// Getters and Setters
	string getHealthInsurance();
	void setHealthInsurance(string);
	double getLifeInsurance();
	void setLifeInsurance(double);
	int getVacation();
	void setVacation(int);

private:
	// Declare Data Members
	string healthInsurance;
	double lifeInsurance;
	int vacation;
};
//---------------------------------------------------------------------------------------------- 


Benefit.cpp
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
// Preprocessor Directives
#include "stdafx.h"
#include "Employee.h"
#include "Benefit.h"
#include <iostream>
#include <iomanip>
//----------------------------------------------------------------------------------------------
// Default Benefit Constructor
Benefit::Benefit() : healthInsurance("not provided"), lifeInsurance(0.0), vacation(14)
{	
}
//----------------------------------------------------------------------------------------------
// Benefit Deconstructor
Benefit::~Benefit()
{
}
//----------------------------------------------------------------------------------------------
// Multi-Arg Benefit Constructor
Benefit::Benefit(string health, double life, int vac)
{
	healthInsurance = health;
	lifeInsurance = life;
	vacation = vac;
}
//----------------------------------------------------------------------------------------------
// DisplayBenefits Function  
void Benefit::displayBenefits()
{
	cout << "Benefit Information" << "\n";
	cout << "________________________________________________________________" << "\n";
	cout << "Health Insurance: " << healthInsurance << "\n";
	cout << "Life Insurance: " << lifeInsurance << "\n";
	cout << "Vacation: " << vacation << " days" << "\n";
}
//----------------------------------------------------------------------------------------------
// Define GetHealthInsurance and SetHealthInsurance
string Benefit::getHealthInsurance()
{
	return healthInsurance;
}

void Benefit::setHealthInsurance(string newHealthInsurance)
{
	healthInsurance = newHealthInsurance;
}
//----------------------------------------------------------------------------------------------
// Define GetLifeInsurance and SetLifeInsurance
double Benefit::getLifeInsurance()
{
	return lifeInsurance;
}

void Benefit::setLifeInsurance(double newLifeInsurance)
{
	lifeInsurance = newLifeInsurance;
}
//----------------------------------------------------------------------------------------------
// Define GetVacation and SetVacation
int Benefit::getVacation()
{
	return vacation;
}


void Benefit::setVacation(int newVacation)
{
	vacation = newVacation;
}
//---------------------------------------------------------------------------------------------- 


Employee.h
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
// Preprocessor Directives
#pragma once
#include "Benefit.h"
#include <string>
using namespace std;
//----------------------------------------------------------------------------------------------
// Declare Prototypes
string getFirstName(string);
string getLastName(string);
char getGender(char);
int getDependents(int);
int getNumEmployee(int);
double getAnnualSalary(double);
void displayFirstName(string);
void displayLastName(string);
void displayGender(char);
void displayDependents(int);
void displayAnnualSalary(double);
void DislayApplicationInformation();
void DisplayDivider(string);
void DisplayDivider2(string);
string GetInput (string);
void TerminateApplication();
//----------------------------------------------------------------------------------------------
// Define iEmployee Class
class iEmployee
{
public:
	virtual double calculatePay()= 0;
};
//----------------------------------------------------------------------------------------------
// Define Employee Class
class Employee : public iEmployee
{
public:
	// Constructors and Destructor
	Employee();
	Employee(string first, string last, char gen, int dep, double salary, Benefit);
	~Employee();

	// Methods to Access Attributes
	double calculatePay();
	void displayEmployee();
	Benefit benefit;

	// Getters and Setters
	string getFirstName();
	void setFirstName(string);
	string getLastName();
	void setLastName(string);
	char getGender();
	void setGender(char);
	int getDependents();
	void setDependents(int);
	void setDependents(string);
	double getAnnualSalary();
	void setAnnualSalary(double);
	void setAnnualSalary(string);
	static int getNumEmployees();

private:
	// Declare Data Members
	string firstName;
	string lastName;
	char gender;
	int dependents;
	static int numEmployees;
	double annualSalary;	
};
//---------------------------------------------------------------------------------------------- 
Last edited on
Employee.cpp
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
// Preprocessor Directives
#include "stdafx.h"
#include "Employee.h"
#include "Benefit.h"
#include <iostream>
#include <iomanip>
//----------------------------------------------------------------------------------------------
// Declare Variable
int Employee::numEmployees = 0;
//----------------------------------------------------------------------------------------------
// Default Employee Constructor
Employee::Employee() : firstName("not given"), lastName("not given"), gender('U'), dependents(0), annualSalary(20000.0)
{
	numEmployees++;
}
//----------------------------------------------------------------------------------------------
// Employee Deconstructor
Employee::~Employee()
{
	numEmployees--;
}
//----------------------------------------------------------------------------------------------
// Multi-Arg Employee Constructor
Employee::Employee(string first, string last, char gen, int dep, double salary, Benefit ben)
{
firstName = first;
lastName = last;
gender = gen;
dependents = dep;
annualSalary = salary;
benefit = ben;
numEmployees++;
}
//----------------------------------------------------------------------------------------------
// Define CalculatePay Function
double Employee::calculatePay()
{
return (annualSalary/52);
}
//----------------------------------------------------------------------------------------------
// DisplayEmployee Function
void Employee::displayEmployee()
{
cout << "Employee Name:\t\t" << firstName << " " << lastName << "\n";
cout << "Employee Gender:\t" << gender << "\n";
cout << "Employee Dependents:\t" << dependents << "\n";
cout << "Employee Annual Salary:\t$" <<
setprecision(2) << showpoint << fixed << annualSalary << "\n";
cout << "Employee Weekly Pay:\t$" << calculatePay() << "\n";
benefit.displayBenefits();
cout << "\n--- Number of Employee Objects Created ----" << "\n";
cout << "Number of employees: " << Employee::getNumEmployees() << "\n\n"; 
}
//----------------------------------------------------------------------------------------------
// Define GetFirstName and SetFirstName
string Employee::getFirstName()
{
	return firstName;
}

void Employee::setFirstName(string newFirstName)
{
	firstName = newFirstName;
}

string Employee::getLastName()
{
	return lastName;
}

void Employee::setLastName(string newLastName)
{
	lastName = newLastName;
}
//----------------------------------------------------------------------------------------------
// Define GetGender and SetGender
char Employee::getGender()
{
	return gender;
}

void Employee::setGender(char newGender)
{
	gender = newGender;
}
//----------------------------------------------------------------------------------------------
// Define GetDependents and SetDependents (overloaded)
int Employee::getDependents()
{
	return dependents;
}

void Employee::setDependents(int newDependents)
{
	dependents = newDependents;
}

void Employee::setDependents(string newDependents)
{
	dependents = atoi(newDependents.c_str());
}
//----------------------------------------------------------------------------------------------
// Define GetAnnualSalary and SetAnnualSalary (overloaded)
double Employee::getAnnualSalary()
{
	return annualSalary;
}

void Employee::setAnnualSalary(double newAnnualSalary)
{
	annualSalary = newAnnualSalary;
}

void Employee::setAnnualSalary(string newAnnualSalary)
{
	annualSalary = atof(newAnnualSalary.c_str());
}
//----------------------------------------------------------------------------------------------
// Define GetNumEmployees
int Employee::getNumEmployees()
{
	return numEmployees;
}


Main.cpp
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
// Preprocessor Directives
#include "stdafx.h"
#include "Employee.h"
#include "Benefit.h"
#include <iostream>
#include <string>
using namespace std;
//----------------------------------------------------------------------------------------------
// Main Function
int main()
{
	// Declare Function Variables
	string tempString;
	char gender;
	int dependents = 0;
	double annualSalary;
	double lifeInsurance;
	int vacation;

	// Call DisplayApplicationInformation and DisplayDivider Proceedures
	DislayApplicationInformation();
	DisplayDivider("Employee 1");

	// Construct Employee1 Object
	Employee employee1;
	Benefit benefit;

	//Getting Employee1 Data
	string firstName = GetInput("First Name");
	employee1.setFirstName(firstName);

	string lastName = GetInput("Last Name");
	employee1.setLastName(lastName);

	tempString = GetInput("Gender");
	gender = tempString.c_str()[0];
	employee1.setGender(gender);

	tempString = GetInput("Number of Dependents");
	dependents = atoi(tempString.c_str());
	employee1.setDependents(dependents);

	tempString = GetInput("Annual Salary");
	annualSalary = atof(tempString.c_str());
	employee1.setAnnualSalary(annualSalary);

	string healthInsurance = GetInput("Health Insurance");
	benefit.setHealthInsurance(healthInsurance);

	tempString = GetInput("Life Insurance");
	lifeInsurance = atoi(tempString.c_str());
	benefit.setLifeInsurance(lifeInsurance);

	tempString = GetInput("Vacation");
	vacation = atoi(tempString.c_str());
	benefit.setVacation(vacation);


	// Calling DisplayDivider2 and DisplayEmployee Proceedures 
	DisplayDivider2("Employee Information"); 
	employee1.displayEmployee(); //Display Employee1 Data
	cout << endl;

	// Construct Employee2 and Benefit1 Objects
	Benefit benefit1("United Healthcare", 150000, 21);
	Employee employee2("Mary", "Noia", 'F', 5, 24000.0, benefit1);
	

	// Calling DisplayDivider, DisplayDivider2 and DisplayEmployee Proceedures
	DisplayDivider("Employee 2");
	DisplayDivider2("Employee Information");
	employee2.displayEmployee(); // Display Employee2 Data

	// Call TerminateApplication Proceedure
	TerminateApplication(); 

	// Pause System
	system("pause");

	return 0;
}
//----------------------------------------------------------------------------------------------
// DisplayApplicationInformation Procedure
void DislayApplicationInformation()
{
	// Display Program Header
	cout << "Welcome to your first Object Oriented Program" << "\n";
	cout << "Employee Class CIS247C, Week 2 Lab" << "\n";
	cout << "Name: Jim Stevens" << "\n";
}
//----------------------------------------------------------------------------------------------
// DisplayDivider Prodedure
void DisplayDivider(string outputTitle)
{
	// Display Divider with Output Title
	cout << "\n************************" << outputTitle <<"************************" << "\n";
}
//----------------------------------------------------------------------------------------------
// DisplayDivider2 Prodedure
void DisplayDivider2(string outputTitle)
{
	// Display Divider with Output Title
	cout << "\n" << outputTitle << endl;
	cout << "\n________________________________________________________________" << "\n";
}
//----------------------------------------------------------------------------------------------
// GetInput Function
string GetInput (string inputType)
{ 
	// Declare Function Varaible
	string input;
	// Prompt User for Input
	cout<<"Please enter your "<< inputType <<": ";
	getline(cin, input);
	return input;
}
//----------------------------------------------------------------------------------------------
// TerminateApplication Procedure
void TerminateApplication()
{
	// Display Termination Message
	cout << "\nThank you for using the Employee Class program\n";
}
//---------------------------------------------------------------------------------------------- 
Just one comment - there is no need to declare class functions - that is the whole idea of class declarations.

Try using the debugger to keep an eye on the variable values - should be easy if you have an IDE.

HTH
Well you don't have a setBenefit() function. Just because you set up benefit for employee1, doesn't mean employee1 will automatically use it.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// this code was broken for clarity
	// Construct Employee1 Object
	Employee employee1;
	Benefit benefit;

	//Getting Employee1 Data
	employee1.setFirstName(firstName);
	employee1.setLastName(lastName);
	employee1.setGender(gender);
	employee1.setDependents(dependents);
	employee1.setAnnualSalary(annualSalary);

	benefit.setHealthInsurance(healthInsurance);
	benefit.setLifeInsurance(lifeInsurance);
	benefit.setVacation(vacation);

// oops, no connection between employee1 and benefit

	// Calling DisplayDivider2 and DisplayEmployee Proceedures 
	DisplayDivider2("Employee Information"); 
	employee1.displayEmployee(); //Display Employee1 Data
	cout << endl;

Minor points:
1) You don't need to separate the newline from the rest of the C string:
1
2
cout << "Name: Jim Stevens" << "\n"; // same effect as
cout << "Name: Jim Stevens\n";


2) Interesting style...
gender = tempString.c_str()[0];

Alternatives:
1
2
gender = tempString.front(); // C++11
gender = *tempString.begin(); // C++98, C++03 


3) You can use initialization lists in any of your constructors.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Employee::Employee(string first, string last, char gen, int dep, double salary, Benefit ben)
{
firstName = first;
lastName = last;
gender = gen;
dependents = dep;
annualSalary = salary;
benefit = ben;
numEmployees++;
}

Employee::Employee(string first, string last, char gen, int dep, double salary, Benefit ben):
    firstName(first),
    lastName(last),
    gender(gen),
    dependents(dep),
    annualSalary(salary),
    benefit(ben)
{
    numEmployees++;
}

OK, Figured out the employee1 and benefit issue. Seems everything is working. Also made some chnages in the code and lay-out. Better or worse??

Benefit.h
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
// Preprocessor Directives
#pragma once
#include <string>
using namespace std;
//----------------------------------------------------------------------------------------------
// Declare Prototypes
	string getHealthInsurance(string);
	string setHealthInsurance(string);
	double getLifeInsurance(double);
	double setLifeInsurance(double);
	int getVacation(int);
	int setVacation(int);
	void displayBenefits();
//----------------------------------------------------------------------------------------------
// Define Benefit Class
class Benefit
{
public:
	// Constructors and Destructor
	Benefit();
	Benefit(string, double, int);
	~Benefit();

	// Methods to Access Attributes
	void displayBenefits();
	
	// Getters and Setters
	string getHealthInsurance();
	void setHealthInsurance(string);
	double getLifeInsurance();
	void setLifeInsurance(double);
	int getVacation();
	void setVacation(int);

private:
	// Declare Data Members
	string healthInsurance;
	double lifeInsurance;
	int vacation;
};
//---------------------------------------------------------------------------------------------- 


Benefit.cpp
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
// Preprocessor Directives
#include "stdafx.h"
#include "Employee.h"
#include "Benefit.h"
#include <iostream>
#include <iomanip>
//----------------------------------------------------------------------------------------------
// Default Benefit Constructor
Benefit::Benefit() : healthInsurance("not provided"), lifeInsurance(0.0), vacation(14)
{	
}
//----------------------------------------------------------------------------------------------
// Benefit Deconstructor
Benefit::~Benefit()
{
}
//----------------------------------------------------------------------------------------------
// Multi-Arg Benefit Constructor
Benefit::Benefit(string health, double life, int vac) :
	healthInsurance(health),
	lifeInsurance(life),
	vacation(vac)
{
}
//----------------------------------------------------------------------------------------------
// DisplayBenefits Function  
void Benefit::displayBenefits()
{
	cout << "Benefit Information" << "\n";
	cout << "________________________________________________________________" << "\n";
	cout << "Health Insurance: " << healthInsurance << "\n";
	cout << "Life Insurance: " << lifeInsurance << "\n";
	cout << "Vacation: " << vacation << " days" << "\n";
}
//----------------------------------------------------------------------------------------------
// Define GetHealthInsurance and SetHealthInsurance
string Benefit::getHealthInsurance()
{
	return healthInsurance;
}

void Benefit::setHealthInsurance(string newHealthInsurance)
{
	healthInsurance = newHealthInsurance;
}
//----------------------------------------------------------------------------------------------
// Define GetLifeInsurance and SetLifeInsurance
double Benefit::getLifeInsurance()
{
	return lifeInsurance;
}

void Benefit::setLifeInsurance(double newLifeInsurance)
{
	lifeInsurance = newLifeInsurance;
}
//----------------------------------------------------------------------------------------------
// Define GetVacation and SetVacation
int Benefit::getVacation()
{
	return vacation;
}


void Benefit::setVacation(int newVacation)
{
	vacation = newVacation;
}
//---------------------------------------------------------------------------------------------- 


Employee.h
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
// Preprocessor Directives
#pragma once
#include "Benefit.h"
#include <string>
using namespace std;
//----------------------------------------------------------------------------------------------
// Declare Prototypes
void DislayApplicationInformation();
void DisplayDivider(string);
void DisplayDivider2(string);
string GetInput (string);
void TerminateApplication();
//----------------------------------------------------------------------------------------------
// Define iEmployee Class
class iEmployee
{
public:
	virtual double calculatePay()= 0;
};
//----------------------------------------------------------------------------------------------
// Define Employee Class
class Employee : public iEmployee
{
private:
	// Declare Data Members
	string firstName;
	string lastName;
	char gender;
	int dependents;
	double annualSalary;
	static int numEmployees;

public:
	// Constructors and Destructor
	Employee(void);
	Employee(string, string, char, int, double, Benefit);
	~Employee(void);
	
	// Methods to Access Attributes
	double calculatePay(void);
	void displayEmployee();
	Benefit benefit;

	// Getters and Setters
	string Employee::getFirstName();
	void Employee::setFirstName(string );
	void Employee::setLastName(string );
	string Employee::getLastName();
	char Employee::getGender();
	void Employee::setGender(char );
	int Employee::getDependents();
	void Employee::setDependents(int );
	void Employee::setDependents(string);  
	double Employee::getAnnualSalary();
	void Employee::setAnnualSalary(double );
	void Employee::setAnnualSalary(string);  
	static int getNumEmployees();
};


Employee.cpp
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
// Preprocessor Directives
#include "stdafx.h"
#include "Employee.h"
#include <iostream>
#include <iomanip>
//----------------------------------------------------------------------------------------------
	// Declare Variables
	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';
	const int NUMBER_WEEKS = 52;
	int Employee::numEmployees = 0;
//----------------------------------------------------------------------------------------------
// Default Employee Constructor
Employee::Employee() : firstName("not given"), lastName("not given"), gender('N'), dependents(0), annualSalary(20000.0)
{
	numEmployees++;
}
//----------------------------------------------------------------------------------------------
// Multi-Arg Employee Constructor
Employee::Employee(string first, string last, char gen, int dep, double salary, Benefit ben) :

	firstName(first),
	lastName(last),
	gender(gen),
	dependents(dep),
	annualSalary(salary)
{
	numEmployees++;
}
//----------------------------------------------------------------------------------------------
// Employee Deconstructor
Employee::~Employee()
{
	numEmployees--;
}
//----------------------------------------------------------------------------------------------
// Define CalculatePay Function
double Employee::calculatePay()
{
	return (annualSalary/NUMBER_WEEKS);
}
//----------------------------------------------------------------------------------------------
// DisplayEmployee Function
void Employee::displayEmployee()
{
cout << "Employee Name:\t\t" << firstName << " " << lastName << "\n";
cout << "Employee Gender:\t" << gender << "\n";
cout << "Employee Dependents:\t" << dependents << "\n";
cout << "Employee Annual Salary:\t$" << setprecision(2) << showpoint << fixed << annualSalary << "\n";
cout << "Employee Weekly Pay:\t$" << setprecision(2) << showpoint << fixed << calculatePay() << "\n";
benefit.displayBenefits();
cout << "\n--- Number of Employee Objects Created ----" << "\n";
cout << "Number of employees: " << Employee::getNumEmployees() << "\n\n"; 
}
//----------------------------------------------------------------------------------------------
// Define GetFirstName and SetFirstName
	string Employee::getFirstName()
	{
		return firstName;
	}
	void Employee::setFirstName(string name)
	{
		firstName = name;
	}

	string Employee::getLastName()
	{
		return lastName;
	}
	void Employee::setLastName(string name)
	{
		lastName = name;
	}
//----------------------------------------------------------------------------------------------
// Define GetGender and SetGender
	char Employee::getGender()
	{
		return gender;
	}

	void Employee::setGender(char gen)
	{
		switch (gen)
		{
		case 'f':case 'F': case 'M':case 'm':
				gender = gen;
				break;
			default:
				gender = DEFAULT_GENDER;
		}
	}
//----------------------------------------------------------------------------------------------
// Define GetDependents and both SetDependents (overloaded)
	int Employee::getDependents()
	{
		return dependents;
	}

	void Employee::setDependents(int dep)
	{
		if (dep >= MIN_DEPENDENTS && dep <= MAX_DEPENDENTS)
		{
			dependents = dep;
		}
		else if (dep < MIN_DEPENDENTS)
		{
			dep = MIN_DEPENDENTS;
		}
		else
		{
			dependents = MAX_DEPENDENTS;
		}
	}

	void Employee::setDependents(string dep)
	{
		Employee::setDependents(atoi(dep.c_str()));
	}
//----------------------------------------------------------------------------------------------
// Define GetAnnualSalary and both SetAnnualSalary (overloaded)
	double Employee::getAnnualSalary()
	{
		return annualSalary;
	}
	void Employee::setAnnualSalary(double salary)
	{
		if (salary >= MIN_SALARY && salary <= MAX_SALARY)
		{
			annualSalary = salary;
		}
		else if (salary < MIN_SALARY)
		{
			annualSalary = MIN_SALARY;
		}
		else
		{
			annualSalary = MAX_SALARY;
		}
	}

	void Employee::setAnnualSalary(string sal)
	{
		Employee::setAnnualSalary( atof(sal.c_str())); 
	}
//----------------------------------------------------------------------------------------------
// Define GetNumEmployees
int Employee::getNumEmployees()
	{
		return Employee::numEmployees; 
		
	}
//---------------------------------------------------------------------------------------------- 


Main.cpp
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
// Preprocessor Directives
#include "stdafx.h"
#include "Employee.h"
#include <iostream>
#include <string>
using namespace std;
//----------------------------------------------------------------------------------------------
// Main Function
int main()
{
	// Call DisplayApplicationInformation and DisplayDivider Proceedures
	DislayApplicationInformation();
	DisplayDivider("Employee 1");

	// Construct Employee1 Object
	Employee employee1;  
	char gender;
	string tempString;

	// Getting Employee1 Data
	employee1.setFirstName(GetInput("First Name "));
	employee1.setLastName(GetInput("Last Name "));

	tempString = GetInput("Gender ");
	gender = tempString.at(0);
	employee1.setGender(gender);  

	employee1.setDependents(GetInput("Dependents "));
	employee1.setAnnualSalary(GetInput("Annual Salary ")); 

	// Construct Benefit Object
	Benefit benefit;
	double lifeInsurance;
	int vacation;

	// Get Employee1 Benefit Data
	employee1.benefit.setHealthInsurance(GetInput("Health Insurance"));

	tempString = GetInput("Life Insurance");
	lifeInsurance = atoi(tempString.c_str());
	employee1.benefit.setLifeInsurance(lifeInsurance);

	tempString = GetInput("Vacation");
	vacation = atoi(tempString.c_str());
	employee1.benefit.setVacation(vacation);

	// Calling DisplayDivider2 and DisplayEmployee Proceedures 
	DisplayDivider2("Employee Information"); 
	employee1.displayEmployee(); //Display Employee1 Data
	cout << endl;

	// Construct Employee2 and Benefit1 Objects
	Benefit benefit1("United Healthcare", 150000, 21);
	Employee employee2("Mary", "Noia", 'F', 5, 24000.0, benefit1);
	
	// Calling DisplayDivider, DisplayDivider2 and DisplayEmployee Proceedures
	DisplayDivider("Employee 2");
	DisplayDivider2("Employee Information");
	employee2.displayEmployee(); // Display Employee2 Data

	// Call TerminateApplication Proceedure
	TerminateApplication();

	return 0;
}
//----------------------------------------------------------------------------------------------
// DisplayApplicationInformation Procedure
void DislayApplicationInformation()
{
	// Display Program Header
	cout << "Welcome to your first Object Oriented Program" << "\n";
	cout << "Employee Class CIS247C, Week 4 Lab" << "\n";
	cout << "Name: Jim Stevens" << "\n";
}
//----------------------------------------------------------------------------------------------
// DisplayDivider Prodedure
void DisplayDivider(string outputTitle)
{
	// Display Divider with Output Title
	cout << "\n************************" << outputTitle <<"************************" << "\n";
}
//----------------------------------------------------------------------------------------------
// DisplayDivider2 Prodedure
void DisplayDivider2(string outputTitle)
{
	// Display Divider with Output Title
	cout << "\n" << outputTitle << endl;
	cout << "\n________________________________________________________________" << "\n";
}
//----------------------------------------------------------------------------------------------
// GetInput Function
string GetInput (string inputType)
{ 
	// Declare Function Varaible
	string input;
	// Prompt User for Input
	cout<<"Please enter your "<< inputType <<": ";
	getline(cin, input);
	return input;
}
//----------------------------------------------------------------------------------------------
// TerminateApplication Procedure
void TerminateApplication()
{
	// Display Termination Message
	cout << "\nThank you for using the Employee Class program\n";
}
//---------------------------------------------------------------------------------------------- 


And many thanks for the help and suggestions!!!
Yuck. Namespaces.
Please don't import an entire namespace.
If you're going to use just the string, import just the string. We don't want name conflicts.
using std::string;
Or better yet, just use the scope resolution operator to resolve namespaces, i.e. std::string
Guess I must be doing better if namespace is the only thing that is brought up. I have been practicing using the scope resolution operator to resolve namespaces, but for homework, they like to see the using namepsace. Not sure if it is because they want us concentrating on getting the code right and program working properly now and that will come later or what. Thanks for the help, especially you Catfish.
Guess I must be doing better if namespace is the only thing that is brought up.


TheIdeasMan wrote:
Just one comment - there is no need to declare class functions - that is the whole idea of class declarations.


I think what TIM meant is...

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
class Employee : public iEmployee
{
	// ...

public:
	// Constructors and Destructor
	Employee(void);
	Employee(string, string, char, int, double, Benefit);
	~Employee(void);
	
	// Methods to Access Attributes
	double calculatePay(void);
	void displayEmployee();
	Benefit benefit;

	// Getters and Setters
	string Employee:: getFirstName();
	void Employee:: setFirstName(string );
	void Employee:: setLastName(string );
	string Employee:: getLastName();
	char Employee:: getGender();
	void Employee:: setGender(char );
	int Employee:: getDependents();
	void Employee:: setDependents(int );
	void Employee:: setDependents(string);  
	double Employee:: getAnnualSalary();
	void Employee:: setAnnualSalary(double );
	void Employee:: setAnnualSalary(string);  
	static int getNumEmployees();
};


Then there's my own personal preferences in style: I would define the member functions inside the class body, rather than splitting the class into two files, .h and .cpp. But they probably want to see how you handle that, splitting the "interface" from the "implementation".

And there's little C++ bits, like passing function parameters by reference instead of value, const member functions, but there's no rush. Just stick around and ask questions as needed.

http://www.parashift.com/c++-faq/const-member-fns.html
1
2
3
4
5
6
7
8
9
10
11
//These are functions
	string getHealthInsurance(string); 
	string setHealthInsurance(string);

class Benefit
{
public:
//These are methods (aka member functions)
	string getHealthInsurance();
	void setHealthInsurance(string);
}
They are different. The methods can be seen as functions that receives an extra parameter, the object that makes the call.
string getHealthInsurance(Benefit *this);

Get rid of those function declarations, you don't {use,define,want} them


Getters and setters are evil http://www.javaworld.com/javaworld/jw-09-2003/jw-0905-toolbox.html
(¿does any of your function do something? I've got tired to look)


1
2
3
4
5
6
// Define iEmployee Class
class iEmployee
{
public:
	virtual double calculatePay()= 0;
};
should have a virtual destructor


Edit:gender = tempString.c_str()[0];
Alternative gender = tempString[0];
Last edited on
Catfish,

If you look at the first code, I had a ton of function prototypes with the getters/setters like you have just shown. By using the Employee:: before them I was able to get rid of most of those prototypes. And yes, that is why they have us splitting the files, I origianllly had everything in one file. Thnaks for the tips!

ne, I went back and got rid of those prototyes as well after I posted, just didn't catch it at first. Why would you need a virtual destructor for this interface?
Why would you need a virtual destructor for this interface?

It has to do with polymorphism, ensuring that the derived class' destructor is called. In general, it's just good practice to make base class' destructor virtual.
http://en.wikipedia.org/wiki/Virtual_function#Virtual_destructors
http://www.parashift.com/c++-faq/virtual-dtors.html
Just some thoughts on getters / setters:

Obviously, one has to have a way to get all the info in (Both initially & changing it later), and ways of getting it out (Either making use of the values, or outputting them). We need to avoid having a getter / setter for each member variable, because if we provide public functions to do this, then we may as well make all the member variables public!!!

There are several different things that can be done.

A simple idea is to think about how things happen in the real world, and provide functions that reflect this. For example, if someone wants to change their address, then provide a function to change ALL the address variables, not a function for each variable. Remember that a member function has direct access to the member variables.

The same idea for outputting data - quite often one might want to print out all the variable values. So provide a function that does this, rather than a get function for each member variable.

The other thing is when one has to make use of a member variable value to calculate some other value. For example, a health insurance policy might depend on someone's age to calculate the premium. You should minimise the number of public functions for this purpose. A better way might be to make use of friend functions or classes.

http://www.cplusplus.com/doc/tutorial/inheritance/


Another idea is to provide an interface to classes with virtual and / or pure virtual functions - which is what ne555 was talking about.

Hope all goes well.





Last edited on
> Another idea is to provide an interface to classes with virtual and / or pure virtual functions - which is what ne555 was talking about.
I was not. ¿was I?
Considering your example. I suppose that the person knows its age, so let it calculate the premium.
Last edited on
Btw this is what I meant about function declarations:

1
2
3
4
5
6
7
8
// Declare Prototypes
	string getHealthInsurance(string);
	string setHealthInsurance(string);
	double getLifeInsurance(double);
	double setLifeInsurance(double);
	int getVacation(int);
	int setVacation(int);
	void displayBenefits();


These functions are mostly declared in the class. Except displayBenefits, which should be a class function any way.

With these functions:

1
2
3
4
5
6
// Declare Prototypes
void DislayApplicationInformation();
void DisplayDivider(string);
void DisplayDivider2(string);
string GetInput (string);
void TerminateApplication();


They could be in their own class, or at least in their own header file, which would be included by which ever .cpp file needs it, rather than tying it to a different class.

With these global variables:

1
2
3
4
5
6
7
8
	// Declare Variables
	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';
	const int NUMBER_WEEKS = 52;
	int Employee::numEmployees = 0;


They should be set in the code for the default constructor, with declaration of the new member variables in the class declaration.

The main idea is to have variables that are related to a class, are in the class, not outside of it. If you need to make a new class to accommodate some variables, then do it , as long as it makes sense in terms of the whole design.

I look forward to seeing your new code. (8+D
@ne555

> Another idea is to provide an interface to classes with virtual and / or pure virtual functions - which is what ne555 was talking about.
I was not. ¿was I?


I just meant you had an example of a pure virtual function in your post, and you were saying it needed a virtual destructor.

Considering your example. I suppose that the person knows its age, so let it calculate the premium.


I disagree - An Insurance company, asks me for my DOB, then it calculates my age to help determine the premium. I don't calculate the premium - it's not my job (however much I would like my premium to be $0.01). Also there may be other factors (that I don't know about) which determine the premium.
@ TIM: are you thinking about Tell Don't Ask?

(A technique which, as I understand it, consists as adding functionality to a class so that you can tell objects what to do, instead of asking objects for data for you to work with.)
You are not `person' for the insurance company, xP
Really, you may be `customer' instead.

> Also there may be other factors (that I don't know about) which determine the premium.
Those factors are likely linked to the person or its environment. The company may put something too, but that probably threats all persons in the same way (they can be passed or adjust later)
So let the object that has the information to make the computation.

Just the way I see it.
@Catfish2

Well there is that idea as well, I was just try to get the OP to think about different ways of dealing with data.

Pages: 12