Write a Class Named Employee That Has the Following Member Variables C++
#1
- New D.I.C Head
Reputation: 0
- Posts: 9
- Joined: 25-November 08
Employee class
Posted 13 April 2010 - 09:01 AM
Write a class named Employee that has the following member variables:
• name. A string that holds the employee's name.
• idNumber. An int variable that holds the employee's ID number.
• department. A string that holds the name of the department where the employee works.
• position. A string that holds the employee's job title.
The class should have the following constructors:
• A constructor that accepts the following values as arguments and assigns them to the appropriate member variables: employee's name, employee's ID number, department, and position.
• A constructor that accepts the following values as arguments and assigns them to the appropriate member variables: employee's name and ID number. The department and position fields should be assigned an empty string (" ");
• A default constructor that assigns empty strings (" ") to the name, department, and position member variables, and 0 to the idNumber member variable.
Write appropriate mutator functions that store values in these member variables and accessor functions that return the values in these member variables. Once you have written the class, create a three Employee objects (3 separate instances), in your main function, to hold the following data.
Name ID Number Department Position
Susan Meyers 47899 Accounting Vice President
Mark Jones 39119 IT Programmer
Joy Rogers 81774 Manufacturing Engineer
The program should store this data in three objects and then display the data for each employee on screen.
I need comment.. I need your help.
#include <iostream> #include <iomanip> #include <string> class Employee { private: char Name; int IdNumber; char Department; char Position; char NUM_WORKER; public: void setName(char Name); void setDepartment(char Dept); void setIdNumber(int ID); void setPosition(char POS); } Employee() { Name=" "; IdNumber=0; Department=" "; Position=" "; }; void Employee::setName(int Name) { Name = Name; } void Employee::setIdNumber(int ID) { IdNumber = ID; } void Employee::setDepartment(int Dept) { Department = Dept; } void Employee::setPosition (int POS) { Position = POS; } int Employee::getName() const { return Name; } int Employee::getIdNumber() const { return IdNumber; } int Employee::getDepartment() const { return Department; } int Employee::getPosition() const { return Position; } int main() { const int NUM_WORKERS = 3; Employee[NUM_WORKERS] = { Employee("Susan Meyers", 47899, "Accounting", "Vice President"), Employee("Mark Jones", 39119, "IT", "Programmer"), Employee("Joy Rogers", 81774, "Manufacturing", "Engineer")}; cout << " NAME ID NUMBER DEPARTMENT POSITION\n"; cout << "-------------------------------------------\n"; for (int i = 0; i < NUM_WORKERS; i++) { cout << setw(8) << Employeei].getName(); cout << setw(8) << Employee[i].getIdnumber(); cout << setw(8) << Employee[i].getDepartment(); cout << setw(8) << Empoyee[i].getPosition() << endl; } return 0; }
Admin Edit: Please use code tags when posting your code. Code tags are used like so =>
Thanks,
PsychoCoder
Is This A Good Question/Topic? 0
#2 PsychoCoder
Reputation: 1663
- Posts: 19,853
- Joined: 26-July 07
Re: Employee class
Posted 13 April 2010 - 09:10 AM
Are you receiving any errors? Does this code not work that way you intended it? When asking for help there are a couple items that are vital in order for someone to properly help you:
- Post the code you're having problems with (DONE)
- Post the exact error you're receiving, if you are receiving one
- If no error explain what the code is doing versus what you want it to do
- Post your question in the body of your post, not the description field
#3 JackOfAllTrades
Reputation: 6260
- Posts: 24,030
- Joined: 23-August 08
Re: Employee class
Posted 13 April 2010 - 09:12 AM
char Name; char Department; char Position;
You do realize those three variables will only hold a SINGLE character, not a STRING of characters, right?
#4 masoug
- D.I.C Head
Reputation: 22
- Posts: 181
- Joined: 30-December 09
Re: Employee class
Posted 13 April 2010 - 09:36 AM
You got quite some stuff in your code to fix...
void Employee::setName(int Name) { Name = Name; }
Technically, that would mean you input an integer (1, 2, 3, 4, 5, 6, ...) as someone's name?
Should be:
void Employee::setName(const char* Name) { Name = Name; }
and this;
Quote
char Name; char Department; char Position;
You do realize those three variables will only hold a SINGLE character, not a STRING of characters, right?
So that means you should write them as:
char *Name; char *Department; char *Position;
Anyway:
#include <iostream> #include <iomanip> #include <string> class Employee { private: char *Name; //Set them as pointers... int IdNumber; char *Department; char *Position; //char NUM_WORKER; //What is this for? public: void setName(char Name); void setDepartment(char Dept); void setIdNumber(int ID); void setPosition(char POS); }; //Don't forget the semicolon!!! Employee::Employee() { Name=" "; IdNumber=0; Department=" "; Position=" "; }; void Employee::setName(const char* Name) { Name = Name; } void Employee::setIdNumber(int ID) { IdNumber = ID; } void Employee::setDepartment(const char* Dept) //WTF? why integer? Keep as character string! { Department = Dept; } void Employee::setPosition (const char* POS) //Same thing here! { Position = POS; } char Employee::getName() //const //What are these for? { return Name; } int Employee::getIdNumber() //const //What are these for? Plus, get your return types right! { return IdNumber; } char Employee::getDepartment() //const //What are these for? Plus, get your return types right! { return Department; } char Employee::getPosition() //const //What are these for? Plus, get your return types right! { return Position; } int main() { //const int NUM_WORKERS = 3; //Why bother? //Employee[2] = // { Employee[0]("Susan Meyers", 47899, "Accounting", "Vice President"), //Your constructor does not take all these arguments! // Employee[1]("Mark Jones", 39119, "IT", "Programmer"), // Employee[2]("Joy Rogers", 81774, "Manufacturing", "Engineer")}; //How about Employee SusanMeyer(), MarkJones(), JoyRogers(); SusanMeyer.setPosition("Vice President"); //etc.. for all the other guys... cout << " NAME ID NUMBER DEPARTMENT POSITION\n"; cout << "-------------------------------------------\n"; //Writing it out MIGHT be easier... //for (int i = 0; i < NUM_WORKERS; i++) // { // cout << setw(8) << Employee[i].getName(); // cout << setw(8) << Employee[i].getIdnumber(); // cout << setw(8) << Employee[i].getDepartment(); // cout << setw(8) << Empoyee[i].getPosition() << endl; // } return 0; }
There, try that... It might work a little better...
-Masoug
#5 shannon212
- New D.I.C Head
Reputation: 0
- Posts: 9
- Joined: 25-November 08
Re: Employee class
Posted 13 April 2010 - 11:33 AM
I have 14 errors.
1>.\Employee.cpp(21) : error C2600: 'Employee::Employee' : cannot define a compiler-generated special member function (must be declared in the class first)
1>.\Employee.cpp(28) : error C2511: 'void Employee::setName(const char *)' : overloaded member function not found in 'Employee'
1> .\Employee.cpp(6) : see declaration of 'Employee'
1>.\Employee.cpp(37) : error C2511: 'void Employee::setDepartment(const char *)' : overloaded member function not found in 'Employee'
1> .\Employee.cpp(6) : see declaration of 'Employee'
1>.\Employee.cpp(41) : error C2511: 'void Employee::setPosition(const char *)' : overloaded member function not found in 'Employee'
1> .\Employee.cpp(6) : see declaration of 'Employee'
1>.\Employee.cpp(45) : error C2039: 'getName' : is not a member of 'Employee'
1> .\Employee.cpp(6) : see declaration of 'Employee'
1>.\Employee.cpp(47) : error C2065: 'Name' : undeclared identifier
1>.\Employee.cpp(49) : error C2039: 'getIdNumber' : is not a member of 'Employee'
1> .\Employee.cpp(6) : see declaration of 'Employee'
1>.\Employee.cpp(51) : error C2065: 'IdNumber' : undeclared identifier
1>.\Employee.cpp(53) : error C2039: 'getDepartment' : is not a member of 'Employee'
1> .\Employee.cpp(6) : see declaration of 'Employee'
1>.\Employee.cpp(55) : error C2065: 'Department' : undeclared identifier
1>.\Employee.cpp(57) : error C2039: 'getPosition' : is not a member of 'Employee'
1> .\Employee.cpp(6) : see declaration of 'Employee'
1>.\Employee.cpp(59) : error C2065: 'Position' : undeclared identifier
1>.\Employee.cpp(70) : error C2228: left of '.setPosition' must have class/struct/union
1>.\Employee.cpp(74) : error C2065: 'cout' : undeclared identifier
1>Build log was saved at "file://f:\Employee.cpp\Employee.cpp\Debug\BuildLog.htm"
1>Employee.cpp - 14 error(s), 0 warning(s)
#6 tauit_dnmd
- D.I.C Head
Reputation: 8
- Posts: 129
- Joined: 25-March 10
Re: Employee class
Posted 13 April 2010 - 12:03 PM
you dont declare contructor for this class:
Code:
class Employee { private: char *Name; //Set them as pointers... int IdNumber; char *Department; char *Position; //char NUM_WORKER; //What is this for? public: Employee(); //contructor void setName(char Name); void setDepartment(char Dept); void setIdNumber(int ID); void setPosition(char POS); };
And
Employee::Employee() { Name=" "; IdNumber=0; Department=" "; Position=" "; };//dont use character ';' in here.delete it
#7 tauit_dnmd
- D.I.C Head
Reputation: 8
- Posts: 129
- Joined: 25-March 10
Re: Employee class
Posted 13 April 2010 - 12:12 PM
You declare :
void setName(char Name); void setDepartment(char Dept); void setIdNumber(int ID); void setPosition(char POS);
-But use do this:
void Employee::setName(const char* Name) //const char* Name and char Name are dif { Name = Name; } void Employee::setIdNumber(int ID) { IdNumber = ID; } void Employee::setDepartment(const char* Dept) //here too { Department = Dept; } void Employee::setPosition (const char* POS) //here too { Position = POS; }
And more:
char Employee::getName() //const //What are these for? { return Name; } int Employee::getIdNumber() //const //What are these for? Plus, get your return types right! { return IdNumber; } char Employee::getDepartment() //const //What are these for? Plus, get your return types right! { return Department; } char Employee::getPosition() //const //What are these for? Plus, get your return types right! { return Position; }
--->you dont declare for these function
This post has been edited by tauit_dnmd: 13 April 2010 - 12:18 PM
#8 JackOfAllTrades
Reputation: 6260
- Posts: 24,030
- Joined: 23-August 08
Re: Employee class
Posted 13 April 2010 - 01:01 PM
A word about this:
char Employee::getName() //const //What are these for?
The presence of the const qualifier on the methods is a contract that the object will not be changed by calls to these methods. It's good coding practice to do this for all methods which will not modify the object, but are often seen on accessors as noted here.
#9 shannon212
- New D.I.C Head
Reputation: 0
- Posts: 9
- Joined: 25-November 08
Re: Employee class
Posted 13 April 2010 - 02:38 PM
I'm confused. Its not workiing and I have still 14 errors. How? I'm tried of errors.
#include <iostream> #include <iomanip> #include <string> class Employee { private: char *Name; //Set them as pointers... int IdNumber; char *Department; char *Position; public: void setName(char Name); void setDepartment(char Dept); void setIdNumber(int ID); void setPosition(char POS); } Employee::Employee() { Name=" "; IdNumber=0; Department=" "; Position=" "; }; void Employee::setName(const char* Name) { Name = Name; } void Employee::setIdNumber(int ID) { IdNumber = ID; } void Employee::setDepartment(const char* Dept) //WTF? why integer? Keep as character string! { Department = Dept; } void Employee::setPosition (const char* POS) //Same thing here! { Position = POS; } char Employee::getName() //const //What are these for? { return Name; } int Employee::getIdNumber() //const //What are these for? Plus, get your return types right! { return IdNumber; } char Employee::getDepartment() //const //What are these for? Plus, get your return types right! { return Department; } char Employee::getPosition() //const //What are these for? Plus, get your return types right! { return Position; } int main() { Employee SusanMeyer(), MarkJones(), JoyRogers(); SusanMeyer.setPosition("Vice President"); //etc.. for all the other guys... cout << " NAME ID NUMBER DEPARTMENT POSITION\n"; cout << "-------------------------------------------\n"; return 0; }
#10 n8wxs
Reputation: 972
- Posts: 3,878
- Joined: 07-January 08
Re: Employee class
Posted 13 April 2010 - 03:01 PM
1: missing semi-colon:
... void setPosition(char POS); } <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< Employee::Employee() { Name=" "; ...
2: missing constructor declaration:
... public: Employee(); void setName(char Name); ...
3: definitions do not match declarations:
... void setName(char Name); void setDepartment(char Dept); void setPosition(char POS); ... void Employee::setName(const char* Name) ... void Employee::setDepartment(const char* Dept) ... void Employee::setPosition (const char* POS) ...
4: undeclared methods:
... char Employee::getName() ... int Employee::getIdNumber() ... char Employee::getDepartment() ... char Employee::getPosition() ...
5: Declartions of objects are not functions:
Employee SusanMeyer(), MarkJones(), JoyRogers(); // lose the parens
6: missing using namespace std;. Or qualify:
std::cout << " NAME ID NUMBER DEPARTMENT POSITION\n";
Other than that...
This post has been edited by n8wxs: 13 April 2010 - 03:02 PM
Write a Class Named Employee That Has the Following Member Variables C++
Source: https://www.dreamincode.net/forums/topic/167809-employee-class/
0 Response to "Write a Class Named Employee That Has the Following Member Variables C++"
Post a Comment