Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Problem: It is instructed that take the value from user in setvalue function then what is the use of overloaded constructor in this program?

You are required to implement Employee Management System. There will be an employee class having following attributes: Name(String), Father_Name(String), Emp_id(int), dob(date), bps(1-22), job_Type(Private, public or autonomous) It should have two constructors i.e., default constructor and an overloaded constructor. The attributes in the constructors must be initialized through initializer list. The Employee class should be having following functions i.e., setRecord and showRecord. In the setEmployeeRecord, the data input should be taken from the user while showRecord should print the employee details.

What I have tried:

I tried but it give following error.
1. type 'int' unexpected
2. 'emp_id': undeclared identifier
3. 'bps': undeclared identifier

#include<iostream>
#include<string>
using namespace std;
class Employee 
{
	string name;
	string father_name;
	int emp_id;
	string date;
	int bps;
	string job_Type;
public:
	Employee();
	Employee(string nam, string f_name, int id, string dt, int b, string job) :name(nam), father_name(f_name), emp_id(id), bps(b), job_Type(job) {}
	void setRecord(string nam, string f_name, int id, string dt, int b, string job);
	void showRecord();
};
Employee::Employee() { /*cout << "Default Constructor" << endl;*/ }
void Employee::setRecord(string nam, string f_name, int id, string dt, int b, string job)
{
	cout << "Name of Employee: ";
	cin >> nam;
	cin.ignore();
	cout << "Father Name: ";
	cin >> f_name;
	cout << "Employee ID: ";
	cin >> id;
	cout << "Date of Birth: ";
	//cin.ignore();
	cin >> dt;
	cout << "Basic Pay Scale: ";
	cin >> b;
	cout << "Job Type: ";
	cin.ignore();
	cin >> job;
}
void Employee::showRecord()
{
	cout << "Name: " << name << endl;
	cout << "Father Name: " << father_name << endl;
	cout << "Employee ID: " << emp_id << endl;
	cout << "Date of Birth: " << date << endl;
	cout << "Basic Pay Scale: " << bps << endl;
	cout << "Job Type: " << job_Type << endl;
}

int main()
{
	string nam, f_name, job, dt,
	int id, b;
	Employee A;
	A.setRecord(nam, f_name, id, dt, b, job);
	system("pause");
	return 0;
}
Posted
Updated 8-May-18 4:59am

The purpose of the overloaded constructor is to give you two ways to construct the object. You can either have it initialized to default values or you can construct it with specific values.
 
Share this answer
 
Write the Employee class in extra files, separated in header (employee.h) and implemention (employee.cpp) files. It is standard in coding to do this.

tip: in the default constructor you MUST also set the values to the member. Like that:
C++
Employee():name(""), father_name(""), emp_id(0), bps(0), job_Type("") {} 

The SetRecord is wrong. Rewrite the function with the members of the object.
C++
void Employee::setRecord()
{
	cout << "Name of Employee: ";
	cin >> name;//correct, but some like more typing with "this->name" !!!
 
Share this answer
 
Comments
Orjan Westin 8-May-18 10:46am    
No need to initialise the string members - they are already "". To say they MUST be set is misleading.
Member 13817351 8-May-18 11:01am    
@Orjan Westin Is my default constructor is correct.
Orjan Westin 10-May-18 3:47am    
No, the integer members emp_id and bps should be initialised.
can you review my code to tell me what is error in my code.
 
Share this answer
 
@KarstenK if i write it without passing attributes which i create in main

int main()
{
	string name, father_name, job_type, date,
	int emp_id, bps;
	Employee A;
	A.setRecord(name, father_name, emp_id, date, bps, job_type);

then how can i initialize it through initializer list(overloaded constructor)

Ask about this part of answer:
void Employee::setRecord()
{
	cout << "Name of Employee: ";
	cin >> name;//correct, but some like more typing with "this->name" !!!
 
Share this answer
 
Comments
Wendelius 8-May-18 11:38am    
Use the "Have a question or comment" button to target the question to a specific answer.
Richard MacCutchan 8-May-18 11:44am    
The setRecord method is not a constructor. But you can call the overload just the same as you call any constructor:

// get the parameter values, then:
Employee A(name, father_name, emp_id, date, bps, job_type);

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900