problem with overloading constructors

Hello all, thanks for your time.

I'm writing a program that reads employee data from a bin file, and creates an ADT using a CLASS to represent the data for the employees.

Your class must include accessor/mutator methods, contructor and destructor method.

Once you declare and define your class, you declare an array of employee data named BayWestIndustries[100]

You are also to use a structure definition to represent the same data type. Also, declare a single object variable and name it employeerecord. Then write a looping control structure to read the data, one record a atime, into employeerecord.

Once the data has been read into employeerecord, you are to transfer the data into the respective members of the current element of the array of employee objects using an OVERLOADED Constructor, defined in your employee class.

There's more to it, but i'm not quite sure if i'm on the right path, and what I need to do next...

Any helps or tips are appreciated


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
const int MAX = 100;
const int SIZE = 15;

class empclass
{
	private:
	string fname;
	string lname;
	string empid;
	string hiredate;
	double prate;
	double hworked;

	public:
	void setfname(string);
	void setfirstn(string);
	void setlastn(string);
	void setempid(string);
	void sethired(string);
	void setrate(double);
	void sethours(double);
	string getfname();
	string getfirstn();
	string getlastn();
	string getempid();
	string gethired();
	double getrate();
	double gethours();


	//overloaded functions tryout
	empclass()
	{

	}


	empclass(string name, string lastnam)
	{

	}	
};

struct employeedata
{
    //data members
	char firstname[SIZE]; 
	char lastname[SIZE];
	char idnumber[SIZE]; 
	char datehired[SIZE];
	char fullname[31];
	double payrate;
	double hoursworked;
	double grosspay;


};






int main ()
{
	empclass BayWestEmployees[MAX];
	int index = 0 , count = 0;




	fstream labfile;
	labfile.open("Employees.bin", ios::in);
	

	if(labfile.fail())
	{
		cout << "Unable to open the data file!!!" <<endl;
		cout << "Please make sure your employees.csv file is in the same location as the program!"<<endl<<endl<<endl;
		system("PAUSE");
		return 999;

	}


	while(!labfile.eof())
	{
		employeedata EmployeeRecord;

		char temp[SIZE];
		labfile.getline(EmployeeRecord.firstname,SIZE,',');
		labfile.getline(EmployeeRecord.lastname,SIZE,',');
		labfile.getline(EmployeeRecord.idnumber,SIZE,',');
		labfile.getline(EmployeeRecord.datehired,SIZE,',');
		labfile.getline(temp, SIZE,',');
		EmployeeRecord.payrate = atof(temp);
		labfile.getline(temp, SIZE,'\n');
		EmployeeRecord.hoursworked = atof(temp);

		index++;
	}
}



left out a few things, hopefully it's not too hard to understand.

Basically, I'm unsure how i'm going to transfer the data using an overloaded constructor, and if I even set myself up correctly

Thanks
Last edited on
From what I am seeing you have it basically correct for the constructors, though you have no code to do any of the initialization of the numbers and plug info in the data slots of the class.

Which part of Overloading are we trying to cover here? Operator overloading or Overloading of functions? This is a question I have from looking at what the code is trying to accomplish. I know how I would attack the program using both ideas.

I hope that gives you some food for thought.
Sorry there was a mistake in the wording.

He wants me to pass each member value from the structure(employee data - EmployeeRecord) TO its corresponding member in the current class array element by using a constructor method in my class. The mistake he made was saying that i needed an overloaded constructor to do this.

Basically, i'm confused as how i'm going to pass the data this way and assign it to the proper values.

Gonna read over the chapter again and the link here over classes then give it a try and post how I think this will be done. I also had a classmate help me and from what I gathered, I need to:

in my while(!eof)
I can make a temp array for empclass, so that every time the while loop runs, it makes my constructor transfer the values to its correct members. If this is correct then the only thing i'm unsure about is how the constructor is going to determine whats what when its transfering the values and assigning them to the correct class member.

Thanks for the reply Azagaros.
Last edited on
Well so far I think I'm on the right path, got it to pass the data to my array of BayWestIndustries. Should be ok for now, I'll keep this open till I'm done just in case I have any other questions or someone thinks I should be doing it differently / more efficiently.

Here's what I got. Keep in mind I have the cout>> to make sure the data was passed, I plan on removing that when im closer to being done.


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
const int MAX = 100;
const int SIZE = 15;


struct empdata
{
    //data members
	char firstname[SIZE];
	char lastname[SIZE];
	char idnumber[SIZE];
	char datehired[SIZE];
	char fullname[31];
	double payrate;
	double hoursworked;
	double grosspay;
	void Show();

};


class employeedata
{
	private:
	string fname;
	string lname;
	string fulln;
	string empid;
	string hiredate;
	double prate;
	double hworked;



	public:

    //accessor
	void setfname(string ,string);
	void setfirstn(string);
	void setlastn(string);
	void setempid(string);
	void sethired(string);
	void setrate(double);
	void sethours(double);
    //mutator
	string getfname()const;
	string getfirstn()const;
	string getlastn()const;
	string getempid()const;
	string gethired()const;
	double getrate()const;
	double gethours()const;

    //constructors
	employeedata()
	{

	}
    employeedata(struct empdata& a)
    {
        setfirstn(a.firstname);
        setlastn(a.lastname);
        setfname(a.firstname,a.lastname);
        setempid(a.idnumber);
        sethired(a.datehired);
        setrate(a.payrate);
        sethours(a.hoursworked);
    }

};


void Show(empdata &);

void employeedata::setfirstn(string f)
{
    fname = f;
}

void employeedata::setlastn(string l)
{
    lname = l;
}

void employeedata::setfname(string fi, string la)
{
    string temp1 = la;
    int len = la.length();
	int size = fi.length();
	temp1.insert(len, 1, ',');
	temp1.insert( len + 1, " ");
	temp1.insert( len + 2, fi );
	fulln = temp1;
}

void employeedata::setempid(string id)
{
    empid = id;
}

void employeedata::sethired(string hi)
{
    hiredate = hi;
}

void employeedata::setrate(double rat)
{
    prate = rat;
}

void employeedata::sethours(double ho)
{
    hworked = ho;
}

string employeedata::getfirstn() const
{
    return fname;
}

string employeedata::getlastn() const
{
    return lname;
}

string employeedata::getfname() const
{
    return fulln;
}


//********************************************************************************************************************
int main ()
{
	employeedata BayWestIndustries[MAX];
	int index = 0 , count = 0;





	fstream labfile;
	labfile.open("Employees.bin", ios::in);


	if(labfile.fail())
	{
		cout << "Unable to open the data file!!!" <<endl;
		cout << "Please make sure your employees.csv file is in the same location as the program!"<<endl<<endl<<endl;
		system("PAUSE");
		return 999;

	}

	while(!labfile.eof())
	{
		empdata EmployeeRecord;


		char temp[SIZE];
		labfile.getline(EmployeeRecord.firstname,SIZE,',');
		labfile.getline(EmployeeRecord.lastname,SIZE,',');
		labfile.getline(EmployeeRecord.idnumber,SIZE,',');
		labfile.getline(EmployeeRecord.datehired,SIZE,',');
		labfile.getline(temp, SIZE,',');
		EmployeeRecord.payrate = atof(temp);
		labfile.getline(temp, SIZE,'\n');
		EmployeeRecord.hoursworked = atof(temp);


        employeedata temporary (EmployeeRecord);
        EmployeeRecord.Show();
        BayWestIndustries[index] = EmployeeRecord;
        cout << endl << endl;
        cout << temporary.getfirstn() << " " << temporary.getlastn() << endl << endl << temporary.getfname() << endl << BayWestIndustries[index].getfirstn()<<  endl << endl;
		index++;
		getch();
	}

    labfile.close();


}

//*******************************************************************************************************************
void empdata::Show()
{

	cout<< "Name: " << firstname << " " << lastname<< endl << "Date Hired: "
	<< datehired << endl << "Employee ID: " << idnumber << endl << "Hours @ Pay: "
	<< hoursworked << " @ " << payrate << endl << endl;
	return;
}
Last edited on
Topic archived. No new replies allowed.