00001 #include "employee.h"
00002
00003 #include <iostream>
00004 #include <cstring>
00005
00006 using namespace std;
00007
00008 void allocate_name(char * & name, const char to[], const char which[]) {
00009 if (strlen(to) == 0) {
00010 name = NULL;
00011 }
00012 else {
00013 name = new char [strlen(to)+1];
00014 if (name != NULL) {
00015 strcpy(name, to);
00016 }
00017 else {
00018 cout << "Failed to allocate " << which << " name for "
00019 << to << "!" << endl;
00020 }
00021 }
00022 return;
00023 }
00024
00025 Employee::Employee(const char first[] ,
00026 const char last[] )
00027 : firstName(NULL), lastName(NULL)
00028 {
00029 allocate_name(firstName, first, "first");
00030 allocate_name(lastName, last, "last");
00031 }
00032
00033 Employee::Employee(const Employee & emp)
00034 : firstName(NULL), lastName(NULL)
00035 {
00036 allocate_name(firstName, emp.firstName, "first");
00037 allocate_name(lastName, emp.lastName, "last");
00038 }
00039
00040 Employee & Employee::operator = (const Employee & emp) {
00041 if (this != &emp)
00042 {
00043 set_first(emp.firstName);
00044 set_last(emp.lastName);
00045 }
00046 return *this;
00047 }
00048
00049 void Employee::set_first(const char first[]) {
00050 delete [] firstName;
00051 allocate_name(firstName, first, "first");
00052 return;
00053 }
00054
00055 void Employee::set_last(const char last[]) {
00056 delete [] lastName;
00057 allocate_name(lastName, last, "last");
00058 return;
00059 }
00060
00061 Employee::~Employee() {
00062 delete [] firstName;
00063 delete [] lastName;
00064 }
00065
00066 void Employee::print(ostream & out) const {
00067 if (firstName != NULL) {
00068 out << firstName << ' ';
00069 }
00070 if (lastName != NULL) {
00071 out << lastName;
00072 }
00073 return;
00074 }