Employee Class

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jul 2009
Posts: 26
Reputation: mandofl is an unknown quantity at this point 
Solved Threads: 0
mandofl mandofl is offline Offline
Light Poster

Employee Class

 
0
  #1
Aug 8th, 2009
Hello once again. I'm trying to do one last project, but unlike the others, that usually i struggle with some common errors, this time i have problems figuring out how its supposed to be created.

This is the question:

Write a class named Employee that has the following member variables:
name, idnumber, department, position

and have the following constructors.
-one that accepts values as arguments and assigns them to the appropiate member variables, employee name, employee id, department, position
-accepts the following values as arguments and assigns them to the appropiate member variables name, id. the department and position fields should be assigned and 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. once you have written the class, write a separete program that creates three employee objects to hold the data :
" susan meyers, 47899, accounting, vice president"
"Mark jones, 39119, IT, programmer"
"Joy Rogers, 81774, manufacturing, Engineer"

The program should store this data in the three objects and then display the data for eac h employee on the screen

This is what i think could be included in the first program:

  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. class Employee
  6. {
  7. private:
  8. string Name;
  9. int IdNumber;
  10. string Department;
  11. string Position;
  12. public:
  13. Employee(string AssignName,int AssignIdNumber,string AssignDepartment,string AssignPosition)
  14. {
  15. Name=AssignName;
  16. IdNumber=AssignIdNumber;
  17. Department=AssignDepartment;
  18. Position=AssignPosition;
  19. }
  20. Employee(string AssignName,int AssignIdNumber)
  21. {
  22. Name=AssignName;
  23. IdNumber= AssignIdNumber;
  24. Department="";
  25. Position="";
  26. }
  27. Employee()
  28. {
  29. Name="";
  30. IdNumber=0;
  31. Department="";
  32. Position="";
  33. }
  34.  
  35. string getName() const;
  36. int getIdNumber() const;
  37. string getDepartment() const;
  38. string getPosition () const;
  39. };
  40.  
  41. string Employee::getName() const
  42. {
  43. return Name;
  44. }
  45. int Employee::getIdNumber() const
  46. {
  47. return IdNumber;
  48. }
  49. string Employee::getDepartment() const
  50. {
  51. return Department;
  52. }
  53. string Employee::getPosition() const
  54. {
  55. return Position;
  56. }

The second part, the second program i think holds something like:

  1. #include <iostream>
  2. #include "EmployeeClass"
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7.  
  8.  
  9. Employee Name[3] ={ "Susan Meyer", "Mark Jones", "Joy Rogers"};
  10. Employee Idnumber[3] ={ 47899, 39119,81774};
  11. Employee Department[3] = { "Accounting", "IT", "Manufacturing"};
  12. Employee Position[3] = {"Vice President", "Programmer", "Engineer"};
  13.  
  14. return 0;
  15.  
  16. }

As to how to have two separete programs combined to make one ???never done anything as advanced as this, PLEASE HELP.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 678
Reputation: Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold 
Solved Threads: 101
Sky Diploma's Avatar
Sky Diploma Sky Diploma is offline Offline
Practically a Master Poster

Re: Employee Class

 
1
  #2
Aug 8th, 2009
Well, You have misunderstood the basic concept of a class
  1. Employee Name[3] ={ "Susan Meyer", "Mark Jones", "Joy Rogers"};
  2. Employee Idnumber[3] ={ 47899, 39119,81774};
  3. Employee Department[3] = { "Accounting", "IT", "Manufacturing"};
  4. Employee Position[3] = {"Vice President", "Programmer", "Engineer"};
So by this code you are actually creating about 12 Employee data types.

Where as you only need to make 3 .......

Just getback to http://cplus.about.com/od/learning1/ss/cppobjects.htm
This small tutorial on classes and i think you will get to know what exactly is going wrong in your code.
1. Please Mark Your Thread as Solved After Getting Your Answers.
2. Please Use CODE TAGS .
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 26
Reputation: mandofl is an unknown quantity at this point 
Solved Threads: 0
mandofl mandofl is offline Offline
Light Poster

Re: Employee Class

 
0
  #3
Aug 8th, 2009
Let me know if im starting to turn towards the right direction. I think this is a good way to just have 3 employee data types. Let me know if i did this correct and how to link both programs. Thanks so much.

  1. #include <iostream>
  2. #include "EmployeeClass"
  3. using namespace std;
  4. int main()
  5. {
  6. const int NUM_WORKERS = 3;
  7. employeedata EMPLOYEE[NUM_WORKERS] ={
  8. employeedata("Susan Meyers", 47899, "Accounting", "Vice President"),
  9. employeedata("Mark Jones", 39119, "IT", "Programmer"),
  10. employeedata("Joy Rogers", 81774, "Manufacturing", "Engineer")};
  11.  
  12. cout << " NAME ID NUMBER DEPARTMENT POSITION\n";
  13. cout << "-------------------------------------------\n";
  14.  
  15. for (int i = 0; i < NUM_WORKERS; i++)
  16. {
  17. cout << setw(8) << EMPLOYEE[i].getName();
  18. cout << setw(8) << EMPLOYEE[i].getIdnumber();
  19. cout << setw(8) << EMPLOYEE[i].getDepartment();
  20. cout << setw(8) << EMPLOYEE[i].getPosition() << endl;
  21. }
  22. return 0;
  23. }
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 678
Reputation: Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold 
Solved Threads: 101
Sky Diploma's Avatar
Sky Diploma Sky Diploma is offline Offline
Practically a Master Poster

Re: Employee Class

 
0
  #4
Aug 8th, 2009
Are you getting an error?

What is the name of the file in which the class declaration is stored?
1. Please Mark Your Thread as Solved After Getting Your Answers.
2. Please Use CODE TAGS .
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 26
Reputation: mandofl is an unknown quantity at this point 
Solved Threads: 0
mandofl mandofl is offline Offline
Light Poster

Re: Employee Class

 
0
  #5
Aug 8th, 2009
I finally got the file to open i think i no longer get that error now these are the errors i get:
error C2065: 'employeedata' : undeclared identifier
error C2146: syntax error : missing ';' before identifier 'EMPLOYEE'
error C2065: 'EMPLOYEE' : undeclared identifier
error C2059: syntax error : '{'
error C2143: syntax error : missing ';' before '{'
error C2143: syntax error : missing ';' before '}'
error C3861: 'employeedata': identifier not found
error C3861: 'employeedata': identifier not found
error C3861: 'employeedata': identifier not found
error C2065: 'EMPLOYEE' : undeclared identifier
C2228: left of '.getName' must have class/struct/union
error C2065: 'EMPLOYEE' : undeclared identifier
error C2228: left of '.getIdnumber' must have class/struct/union error C2065: 'EMPLOYEE' : undeclared identifier
error C2228: left of '.getDepartment' must have class/struct/union
error C2065: 'EMPLOYEE' : undeclared identifier
error C2228: left of '.getPosition' must have class/struct/union


This program is called Data Two:
  1. #include <iostream>
  2. #include <iomanip>
  3. #include "Employee Class.cpp"
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. const int NUM_WORKERS = 3;
  9. employeedata EMPLOYEE[NUM_WORKERS] ={
  10. employeedata("Susan Meyers", 47899, "Accounting", "Vice President"),
  11. employeedata("Mark Jones", 39119, "IT", "Programmer"),
  12. employeedata("Joy Rogers", 81774, "Manufacturing", "Engineer")};
  13.  
  14. cout << " NAME ID NUMBER DEPARTMENT POSITION\n";
  15. cout << "-------------------------------------------\n";
  16.  
  17. for (int i = 0; i < NUM_WORKERS; i++)
  18. {
  19. cout << setw(8) << EMPLOYEE[i].getName();
  20. cout << setw(8) << EMPLOYEE[i].getIdnumber();
  21. cout << setw(8) << EMPLOYEE[i].getDepartment();
  22. cout << setw(8) << EMPLOYEE[i].getPosition() << endl;
  23. }
  24. return 0;
  25. }

This is the Other file and what i have changed it those far. This one is called Employee Class

  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. class Employee
  6. {
  7. private:
  8. string Name;
  9. int IdNumber;
  10. string Department;
  11. string Position;
  12. public:
  13. Employee(string AssignName,int AssignIdNumber,string AssignDepartment,string AssignPosition)
  14. {
  15. Name=AssignName;
  16. IdNumber=AssignIdNumber;
  17. Department=AssignDepartment;
  18. Position=AssignPosition;
  19. }
  20. Employee(string AssignName,int AssignIdNumber)
  21. {
  22. Name=AssignName;
  23. IdNumber= AssignIdNumber;
  24. Department="";
  25. Position="";
  26. }
  27. Employee()
  28. {
  29. Name="";
  30. IdNumber=0;
  31. Department="";
  32. Position="";
  33. }
  34. void setName(string);
  35. void setIdNumber(int);
  36. void setDepartment(string);
  37. void setPosition(string);
  38. string getName() const;
  39. int getIdNumber() const;
  40. string getDepartment() const;
  41. string getPosition () const;
  42. };
  43.  
  44. void Employee::setName(string NA)
  45. {
  46. Name = NA;
  47. }
  48.  
  49. void Employee::setIdNumber(int ID)
  50. {
  51. IdNumber = ID;
  52. }
  53. void Employee::setDepartment(string DEP)
  54. {
  55. Department = DEP;
  56. }
  57. void Employee::setPosition (string POS)
  58. {
  59. Position = POS;
  60. }
  61.  
  62. string Employee::getName() const
  63. {
  64. return Name;
  65. }
  66. int Employee::getIdNumber() const
  67. {
  68. return IdNumber;
  69. }
  70. string Employee::getDepartment() const
  71. {
  72. return Department;
  73. }
  74. string Employee::getPosition() const
  75. {
  76. return Position;
  77. }

Hopefully this can give you guys some info as to how clustered my mind is right now. That is why i need the help BAD. Thanks.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,425
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 184
firstPerson's Avatar
firstPerson firstPerson is offline Offline
Nearly a Posting Virtuoso

Re: Employee Class

 
1
  #6
Aug 8th, 2009
#include "Employee Class.cpp"

There should be no space between the name , it should be

#include "EmployeeClass.cpp"
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 26
Reputation: mandofl is an unknown quantity at this point 
Solved Threads: 0
mandofl mandofl is offline Offline
Light Poster

Re: Employee Class

 
0
  #7
Aug 8th, 2009
If i take out the space then it won't open the file, I have to leave the space, because that is how i named it. And that part works, but after it opens it is when i get those tons of errors.
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 26
Reputation: mandofl is an unknown quantity at this point 
Solved Threads: 0
mandofl mandofl is offline Offline
Light Poster

Re: Employee Class

 
0
  #8
Aug 9th, 2009
Could some one give some more pointers as to how to do this. Thanks
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,721
Reputation: adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of 
Solved Threads: 498
Moderator
adatapost's Avatar
adatapost adatapost is offline Offline
Posting Maven

Re: Employee Class

 
0
  #9
Aug 9th, 2009
Fields must be initialized with constructor.
  1. int main()
  2. {
  3. Employee a[3]={Employee("A",1,"A","A"),Employee("B",2,"B","B"),Employee("C",3,"C","C")};
  4. for(int i=0;i<3;i++)
  5. {
  6. cout << "\n" << a[i].getName();
  7. }
  8. return 0;
  9. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 26
Reputation: mandofl is an unknown quantity at this point 
Solved Threads: 0
mandofl mandofl is offline Offline
Light Poster

Re: Employee Class

 
0
  #10
Aug 9th, 2009
I have been working on this all night, i'm like right there in getting it to work, could you guys tell me what these errors mean, and how to fix them so that i can get this program working. Thanks you.


This is what i have for the file employee class:
  1. #ifndef EmployeeClass
  2. #define EmployeeClass
  3. #include < cstring>
  4.  
  5. const int DEFAULT_SIZE = 51;
  6.  
  7. class EmployeeData
  8. {
  9. private:
  10. char *name;
  11. int idnumber;
  12. char department;
  13. char position;
  14.  
  15. //private member function
  16. void createName(int size, char *value)
  17. {//Allocate the default amount of memory for name
  18. name = new char [size];
  19.  
  20. //store a value in the memory
  21. strcpy(name, value);}
  22.  
  23. public:
  24. //Constructor #1
  25. EmployeeData()
  26. {//Store an empty string in the name attribute.
  27. createName(DEFAULT_SIZE, "");
  28.  
  29. //initialize idnumber department position
  30. idnumber = 0;
  31. department = 0;
  32. position = 0;}
  33.  
  34. //Constructor #2
  35. EmployeeData(char *nam)
  36. {//allocate memory and store the description.
  37. createName(strlen(nam), nam);
  38.  
  39. //initialize idnumber department position
  40. idnumber = 0;
  41. department = 0;
  42. position = 0;}
  43.  
  44. //Constructor #3
  45. EmployeeData(char *nam, int id, char d, char p)
  46. {//Allocate memory and store the description.
  47. createName(strlen(nam), nam);
  48.  
  49. //Assign values to id deparment position
  50. idnumber = id;
  51. department = d;
  52. position = p;}
  53.  
  54. //Destructor
  55. ~EmployeeData()
  56. { delete[] name;}
  57.  
  58. //mutator functions
  59. void setName(char *n)
  60. {strcpy(name, n);}
  61.  
  62. void setIdnumber(int id)
  63. {idnumber = id;}
  64.  
  65. void setDepartment(char d)
  66. {department = d;}
  67.  
  68. void setPosition(char p)
  69. {position =p;}
  70.  
  71. //Accessor functions
  72. const char *getName() const
  73. {return name;}
  74.  
  75. int getIdnumber() const
  76. {return idnumber;}
  77.  
  78. char getDepartment() const
  79. {return department;}
  80.  
  81. char getPosition() const
  82. {return position;}
  83. };
  84. #endif

This is what i have for the file Data Two:
  1. #include <iostream>
  2. #include <iomanip>
  3. #include "Employee Class.cpp"
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. const int NUM_WORKERS = 3;
  9. EmployeeData EMPLOYEE[NUM_WORKERS] ={
  10. EmployeeData("Susan Meyers", 47899, "Accounting", "Vice President"),
  11. EmployeeData("Mark Jones", 39119, "IT", "Programmer"),
  12. EmployeeData("Joy Rogers", 81774, "Manufacturing", "Engineer")};
  13.  
  14. cout << " NAME ID NUMBER DEPARTMENT POSITION\n";
  15. cout << "-------------------------------------------\n";
  16.  
  17. for (int i = 0; i < NUM_WORKERS; i++)
  18. {
  19. cout << setw(8) << EMPLOYEE[i].getName();
  20. cout << setw(8) << EMPLOYEE[i].getIdnumber();
  21. cout << setw(8) << EMPLOYEE[i].getDepartment();
  22. cout << setw(8) << EMPLOYEE[i].getPosition() << endl;
  23. }
  24. return 0;
  25. }

The ERRORS:
warning C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. : see declaration of 'strcpy'
warning C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead.see declaration of 'strcpy'
error C2665: 'EmployeeData::EmployeeData' : none of the 4 overloads could convert all the argument types cpp(50): could be 'EmployeeData::EmployeeData(char *,int,char,char)'
while trying to match the argument list '(const char [13], int, const char [11], const char [15])'
'EmployeeData::EmployeeData' : none of the 4 overloads could convert all the argument types
: could be 'EmployeeData::EmployeeData(char *,int,char,char) while trying to match the argument list '(const char [11], int, const char [3], const char [11])'
: error C2665: 'EmployeeData::EmployeeData' : none of the 4 overloads could convert all the argument typesclass.cpp(50): could be 'EmployeeData::EmployeeData(char *,int,char,char)'
while trying to match the argument list '(const char [11], int, const char [14], const char [9])'
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC