Monday, August 1, 2011

EmployeeDepartment Classes

So I got everything to work finally. What I have here is a slightly simplified version of what we were working on class.



So here is the Employee class. It is pretty much the same as what we were doing, except fewer fields and gets and sets.

Here is the header:



#pragma once
#include

using std::string;

class Employee
{
public:
Employee(void);
~Employee(void);

//gets
string GetName();
string GetHireDate();
//sets
void SetName(string, string);
void SetHireDate(string);


private:
string lastname;
string firstname;
string hireDate;
};


Here is the cpp file


#include "Employee.h"


Employee::Employee(void)
{
}


Employee::~Employee(void)
{
}

//gets

string Employee::GetName()
{
return lastname + ", " + firstname;
}

string Employee::GetHireDate()
{
return hireDate;
}

//sets

void Employee::SetName(string fName, string lName)
{
lastname=lName;
firstname=fName;
}

void Employee::SetHireDate(string hDate)
{
hireDate=hDate;
}


For the Department Class, notice the AddEmployee method and the declaration of the EmployeeList array.

The AddEmployee now takes two arguments a pointer to an object of type Employee and an integer counter. The counter is to say which array index the employee being passed should be added to.

The declaration of the EmployeeList is now also a pointer. The array is empty. This generates a warning but not an error. The constructor for the Department class takes in an int which we will use for the size of a dynamically initialized EmployeeList.

Here is the header file:



#pragma once
#include "Employee.h"

class Department
{
public:
Department(int);
~Department(void);
void AddEmployee(Employee * e, int counter);
string GetEmployee();

private:
int counter;
int size;
void InitializeArray();
Employee * EmployeeList[];

};


Here is the cpp file. Notice how the array is initialized.


#include "Department.h"


Department::Department(int size)
{
this->size=size;
//counter=0;
InitializeArray();
}


Department::~Department(void)
{

}

void Department::InitializeArray()
{
*EmployeeList = new Employee[size];
}

void Department::AddEmployee(Employee *e,int counter)
{

EmployeeList[counter]=e;


}

string Department::GetEmployee()
{
string departmentMember="";
for(int i=0;i < size;i++)
{
departmentMember += EmployeeList[i]->GetName() + "\t" + EmployeeList[i]->GetHireDate() + "\n";
}

return departmentMember;
}


Now here is the program file. I used the "new" key word to dynamically initialize the classes.


#include <iostream>
#include "Department.h"
#include "Employee.h"

void Display();

using namespace std;

int main()
{
Display();
char c;
cin >>c;
}

void Display()
{
int number=0;
cout << "Enter how many employees you want to enter" <<endl;
cin >> number;
cin.ignore();

Department * dept = new Department(number);

for (int i=0; i<number;i++)
{
Employee * emp=new Employee;
cout << "Enter First Name" << endl;
string fname;
getline(cin, fname);
cout << "Enter last name" << endl;
string lname;
getline(cin, lname);
emp->SetName(fname, lname);
cout << "Enter Hire Date" <<endl;
string hdate;
getline(cin, hdate);
emp->SetHireDate(hdate);

dept->AddEmployee(emp, i);


}

cout << "the department includes the following employees" << endl;
cout << "****************************************" << endl;

cout << dept->GetEmployee();


//delete dept;
}


Here is something I got off the web for converting doubles to strings. The c1 and c2 are double variables.


1.Use a stringstream:

std::ostringstream s; s << "(" << c1 << ", " << c2 << ")";
storedCorrect[count] = s.str()

No comments:

Post a Comment