Write a console program that implements these classes. In main, write a “driver
ID: 3621542 • Letter: W
Question
Write a console program that implements these classes. In main, write a “driver program” that fully tests the functionality of each class.Your employees have various Set() functions that validate their data, and raise particular types of exceptions for bad data. To keep it simple, just check for null arguments. In real life, you could check the data more thoroughly.
In main, create several Employees. Initialize some at creation with the parameterized constructor, and initialize some after you create them using the Set...() functions.
Put an exception handler in main with catch blocks for EmpNameException, EmpAddressException, EmpPhoneException, EmpException, and a catch(...) default catch block. Make your program throw exceptions by calling the Set() functions with null string arguments. Also test the default catch block by throwing an integer exception with some value if SetName() is called with a value of “Bob”.
getMonthsPay()should throw a EmpException if any data is missing (name, address, or phone), and the message should say which piece of data is missing.
class Employee {
private:
char name[80];
char address[80];
char phone[80];
public:
Employee();
Employee(char * Name, char * Address, char * Phone);
void SetName(char *); // throws EmpNameException
void SetAddress(char *); // throws EmpAddressException
void SetPhone(char *); // throws EmpPhoneException
int GetID();
double getMonthsPay(){return 5000;} // throws EmpException
};
class EmpException {
public:
char msg[80];
EmpException(char *); // use to initialize msg
};
class EmpNameException : public EmpException {
};
class EmpAddressException : public EmpException {
};
class EmpPhoneException : public EmpException {
};
Explanation / Answer
Hope this helps. Let me know if you have any questions. Please rate. :) #include #include using namespace std; class Employee { private: char name[80]; char address[80]; char phone[80]; public: Employee(); Employee(char * Name, char * Address, char * Phone); void SetName(char *); // throws EmpNameException void SetAddress(char *); // throws EmpAddressException void SetPhone(char *); // throws EmpPhoneException int GetID(); double getMonthsPay(); // throws EmpException }; class EmpException { public: char msg[80]; EmpException(); EmpException(char *); // use to initialize msg }; EmpException::EmpException() { // nothing to do - default constructor still required } EmpException::EmpException(char * m) { strncpy(msg, m, 80); msg[79] = ''; } class EmpNameException : public EmpException { public: EmpNameException(char * m) { strncpy(msg, m, 80); msg[79] = ''; } }; class EmpAddressException : public EmpException { public: EmpAddressException(char * m) { strncpy(msg, m, 80); msg[79] = ''; } }; class EmpPhoneException : public EmpException { public: EmpPhoneException(char * m) { strncpy(msg, m, 80); msg[79] = ''; } }; Employee::Employee() { // nothing really to initialize } Employee::Employee(char * Name, char * Address, char * Phone) { strncpy(name, Name, 80); name[79] = ''; strncpy(address, Address, 80); address[79] = ''; strncpy(phone, Phone, 80); phone[79] = ''; } void Employee::SetName(char * n) { if (n == NULL) throw EmpNameException("Bad data in SetName"); if (strcmp(n, "Bob") == 0) throw 20; else { strncpy(name, n, 80); name[79] = ''; } } void Employee::SetAddress(char * a) { if (a == NULL) throw EmpAddressException("Bad data in SetAddress"); else { strncpy(address, a, 80); address[79] = ''; } } void Employee::SetPhone(char * p) { if (p == NULL) throw EmpPhoneException("Bad data in SetPhone"); else { strncpy(phone, p, 80); phone[79] = ''; } } int Employee::GetID() { // ?? nothing to do here. There is no ID in the class return 0; } double Employee::getMonthsPay() { char a[] = "Missing address. "; char p[] = "Missing phone. "; char n[] = "Missing name. "; char msg[80]; msg[0] = ''; if (strlen(name) < 1) strcat(msg, n); if (strlen(phone) < 1) strcat(msg, p); if (strlen(address) < 1) strcat(msg, a); if (strlen(msg) > 0) { strcat(msg, "Cannot return months pay."); throw EmpException(msg); } return 5000; } int main() { Employee a, b, c; Employee d("Brian", "1234 Main Ave.", "558-845-4545"); Employee e("Jess", "12345 Main Ave.", "558-554-4236"); char * exc = NULL; try { a.SetName(exc); } catch (EmpNameException e) { cerrRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.