943,704 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 2642
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Aug 8th, 2009
0

Employee Class

Expand Post »
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:

C++ Syntax (Toggle Plain Text)
  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:

C++ Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 33
Solved Threads: 0
Light Poster
mandofl is offline Offline
26 posts
since Jul 2009
Aug 8th, 2009
1

Re: Employee Class

Well, You have misunderstood the basic concept of a class
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 673
Solved Threads: 125
Practically a Posting Shark
Sky Diploma is offline Offline
818 posts
since Mar 2008
Aug 8th, 2009
0

Re: Employee Class

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.

C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 33
Solved Threads: 0
Light Poster
mandofl is offline Offline
26 posts
since Jul 2009
Aug 8th, 2009
0

Re: Employee Class

Are you getting an error?

What is the name of the file in which the class declaration is stored?
Reputation Points: 673
Solved Threads: 125
Practically a Posting Shark
Sky Diploma is offline Offline
818 posts
since Mar 2008
Aug 8th, 2009
0

Re: Employee Class

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:
C++ Syntax (Toggle Plain Text)
  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

C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 33
Solved Threads: 0
Light Poster
mandofl is offline Offline
26 posts
since Jul 2009
Aug 8th, 2009
1

Re: Employee Class

#include "Employee Class.cpp"

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

#include "EmployeeClass.cpp"
Reputation Points: 840
Solved Threads: 594
Senior Poster
firstPerson is offline Offline
3,862 posts
since Dec 2008
Aug 8th, 2009
0

Re: Employee Class

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.
Reputation Points: 33
Solved Threads: 0
Light Poster
mandofl is offline Offline
26 posts
since Jul 2009
Aug 9th, 2009
0

Re: Employee Class

Could some one give some more pointers as to how to do this. Thanks
Reputation Points: 33
Solved Threads: 0
Light Poster
mandofl is offline Offline
26 posts
since Jul 2009
Aug 9th, 2009
0

Re: Employee Class

Fields must be initialized with constructor.
C++ Syntax (Toggle Plain Text)
  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. }
Moderator
Reputation Points: 2136
Solved Threads: 1228
Posting Genius
adatapost is offline Offline
6,527 posts
since Oct 2008
Aug 9th, 2009
0

Re: Employee Class

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:
C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  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])'
Reputation Points: 33
Solved Threads: 0
Light Poster
mandofl is offline Offline
26 posts
since Jul 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Bank program help!
Next Thread in C++ Forum Timeline: IsAdmin?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC