I am doing an assignment and I can not figure out pointers. We are required to send a pointer to the function displayEmployee(), but I am only getting one array. I need three different workers to be displayed. Only one is displayed and I can not seem to do a for loop to fix it because it says that it can not change its data type from Employee to Employee. How to I avoid this?
Below is my assignment guidelines.
Write a class named Employee, with the class declaration in a file called Employee.h and the implementation in a file called Employee.cpp. The class should have the following data members:
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 status
The class must have the following constructors:
A constructor that accepts the following values as arguments and assigns them to the appropriate number 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 variable: employee’s name, employee’s ID number. The department and position fields should be assigned an empty string (“ “).
A default constructor that assigns empty string (“”) to the name, department and position member variables and 0 to the idNumber member variable. Write the 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, write a separate program that creates 3 instances of the Employee class. Each instance of the class should use a different constructor than the other 2 objects (so all three constructors must be used). Main should use a function called displayEmployee that has one parameter which is a pointer to a constant Employee object. Main will call the function 3 times to display the information for each of the 3 instances of the Employee class.
void displayEmployee(Employee* const e);
The output of the program must be in the form of a table
This is my Empployee.h file
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
|
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
class Employee
{
public:
//CONSTRUCTORS
Employee(); //default constructor
Employee(string n, int id); //constructor with name, ID, and blank dept and pos
Employee(string n, int id, string d, string p); //constructor with name, ID, dept and position
//SET FUNCTIONS
void setName(string);
void setID(int);
void setDept(string);
void setPos(string);
//GET FUNCTIONS
string getName()const;
int getID()const;
string getDept()const;
string getPos()const;
private:
string name, dept, pos;
int num;
};
|
This is my Employee.cpp
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
|
#include "Employee.h"
//declarations //my private section of my class does not apply to this page?
string name, dept, pos; //had to declare it here for my get functions
int num;
//CONSTRUCTORS
//default constructor
Employee::Employee()
{
name = "";
num = 0;
dept = "";
pos = "";
};
//constructor that accepts name and num. Pos and dept should be left as ""
Employee::Employee(string n, int id)
{
name = n;
num = id;
dept = "";
pos = "";
};
//constructor that accepts all
Employee::Employee(string n, int id, string d, string p)
{
name = n;
num = id;
dept = d;
pos = p;
};
//MUTATOR FUNCTIONS
//SET FUNCTIONS
void Employee::setName(string n)
{
name = n;
}
void Employee::setID(int id)
{
num = id;
}
void Employee::setDept(string d)
{
dept = d;
}
void Employee::setPos(string p)
{
pos = p;
}
//GET FUNCTIONS
string Employee::getName() const
{
return name;
}
int Employee::getID() const
{
return num;
}
string Employee:: getDept() const
{
return dept;
}
string Employee:: getPos() const
{
return pos;
}
|
And this is my main.cpp
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
|
#include "Employee.h" //includes iostream, string, and iomanip
string n, d, p;
int id;
void displayEmployee(Employee*, const int);
void main()
{
const int workers = 3;
Employee e[workers];
//get employee informtion
for (int i = 0; i < workers; i++)
{
cout << "Enter the employee name: ";
getline(cin, n);
e[i].setName(n);
cout << "Enter the employee's ID: ";
cin >> id;
e[i].setID(id);
cin.ignore();
cout << "Enter the employee's department: ";
getline(cin, d);
e[i].setDept(d);
cout << "Enter the employee's position: ";
getline(cin, p);
e[i].setPos(p);
}
displayEmployee(e, workers);
system("pause");
}
void displayEmployee (Employee* arr, const int size)
{
//display employee information
cout << endl << left<< setw(20) << "Name" << setw(15) << "ID Number" << setw(20) << "Department" << setw(20) << "Position" << endl;
cout << "----------------------------------------------------------------------------" << endl;
for (int i = 0; i < size; i++)
{
cout << left << setw(20) << arr->getName() << setw(15) << arr->getID() << setw(20);
cout << arr->getDept() << setw(20) << arr->getPos() << endl<<endl;
}
}
|