Question

C++ edit: You start with the code given and then modify it so that it does...

C++

edit: You start with the code given and then modify it so that it does what the following asks for.

  1. Create an EmployeeException class whose constructor receives a String that consists of an employee’s ID and pay rate. Modify the Employee class so that it has two more fields, idNum and hourlyWage. The Employee constructor requires values for both fields. Upon construction, throw an EmployeeException if the hourlyWage is less than $6.00 or over $50.00. Write a program that establishes at least three Employees with hourlyWages that are above, below, and within the allowed range. Save the program as ThrowEmployee.cpp.

  1. Compile and run the EmployeeTest.cpp program. Modify the program to run for 10 employees.

    Starting code:

#include
#include
#include
#include
#include
#include "stdafx.h"
using namespace std;

// Date class definition; Member functions defined in Date.cpp


class Date {
public:
   static const unsigned int monthsPerYear{ 12 }; // months in a year
   explicit Date(unsigned int = 1, unsigned int = 1, unsigned int = 1900);
   std::string toString() const; // date string in month/day/year format
   ~Date(); // provided to confirm destruction order
private:
   unsigned int month; // 1-12 (January-December)
   unsigned int day; // 1-31 based on month
   unsigned int year; // any year


};

// constructor confirms proper value for month; calls

Date::Date(unsigned int mn, unsigned int dy, unsigned int yr)
: month{mn}, day{dy}, year{yr} {
if (mn < 1 || mn > monthsPerYear) { // validate the month
throw invalid_argument("month must be 1-12");
}

// output Date object to show when its constructor is called
cout << "Date object constructor for date " << toString() << endl;
}

// print Date object in form month/day/year
string Date::toString() const {
ostringstream output;
output << month << '/' << day << '/' << year;
return output.str();
}

// output Date object to show when its destructor is called
Date::~Date() {
cout << "Date object destructor for date " << toString() << endl;
}

// Employee class definition showing composition.
// Member functions defined in Employee.cpp.

class Employee {
public:
   Employee(const std::string&, const std::string&,
       const Date&, const Date&);
   std::string toString() const;
   ~Employee(); // provided to confirm destruction order
private:
   std::string firstName; // composition: member object
   std::string lastName; // composition: member object
   const Date birthDate; // composition: member object
   const Date hireDate; // composition: member object
};

// Employee class member-function definitions.

// constructor uses member initializer list to pass initializer
// values to constructors of member objects
Employee::Employee(const string& first, const string& last,
const Date &dateOfBirth, const Date &dateOfHire)
: firstName{first}, // initialize firstName
lastName{last}, // initialize lastName   
birthDate{dateOfBirth}, // initialize birthDate
hireDate{dateOfHire} { // initialize hireDate
// output Employee object to show when constructor is called
cout << "Employee object constructor: "
<< firstName << ' ' << lastName << endl;
}

// print Employee object
string Employee::toString() const {
ostringstream output;
output << lastName << ", " << firstName << " Hired: "
<< hireDate.toString() << " Birthday: " << birthDate.toString();
return output.str();
}

// output Employee object to show when its destructor is called
Employee::~Employee() {
cout << "Employee object destructor: "
<< lastName << ", " << firstName << endl;
}

// Demonstrating composition--an object with member objects.


int main() {
Date birth{7, 24, 1949};
Date hire{3, 12, 1988};
Employee manager{"Bob", "Blue", birth, hire};

cout << "\n" << manager.toString() << endl;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The given program is modified as asked in question and checked for four Employees with hourlyWages that are above, below, and within the allowed range. If any queries post in comment...

#include <iostream>
//#include <string>
#include <sstream>
//#include <stdafx>
using namespace std;

// Date class definition; Member functions defined in Date.cpp


class Date {
public:
static const unsigned int monthsPerYear{ 12 }; // months in a year
explicit Date(unsigned int = 1, unsigned int = 1, unsigned int = 1900);
std::string toString() const; // date string in month/day/year format
~Date(); // provided to confirm destruction order
private:
unsigned int month; // 1-12 (January-December)
unsigned int day; // 1-31 based on month
unsigned int year; // any year


};

// constructor confirms proper value for month; calls

Date::Date(unsigned int mn, unsigned int dy, unsigned int yr)
: month{mn}, day{dy}, year{yr} {
if (mn < 1 || mn > monthsPerYear) { // validate the month
throw invalid_argument("month must be 1-12");
}

// output Date object to show when its constructor is called
//cout << "Date object constructor for date " << toString() << endl;
}

// print Date object in form month/day/year
string Date::toString() const {
ostringstream output;
output << month << '/' << day << '/' << year;
return output.str();
}

// output Date object to show when its destructor is called
Date::~Date() {
//cout << "Date object destructor for date " << toString() << endl;
}

// Employee class definition showing composition.
// Member functions defined in Employee.cpp.

class Employee {
public:
Employee(const std::string&, const std::string&,
const Date&, const Date&, int&, float&);
std::string toString() const;
~Employee(); // provided to confirm destruction order
private:
std::string firstName; // composition: member object
std::string lastName; // composition: member object
const Date birthDate; // composition: member object
const Date hireDate; // composition: member object
int idNum; // composition: member object
float hourlyWage; // composition: member object
};

// Employee class member-function definitions.

// constructor uses member initializer list to pass initializer
// values to constructors of member objects
Employee::Employee(const string& first, const string& last, const Date &dateOfBirth, const Date &dateOfHire, int &idNumber, float &hourlyWages)
: firstName{first}, // initialize firstName
lastName{last}, // initialize lastName   
birthDate{dateOfBirth}, // initialize birthDate
hireDate{dateOfHire}, // initialize hireDate
idNum{idNumber}, // initialize idNum
hourlyWage{hourlyWages}{ // initialize hourlyWage
// output Employee object to show when constructor is called
if(hourlyWage<6 || hourlyWage>50)
{
throw "\nHourly Wages is not in range it should be between $6.0 to $50.0 ";
}
//cout << "Employee object constructor: " << firstName << ' ' << lastName << endl;
}

// print Employee object
string Employee::toString() const {
ostringstream output;
output << lastName << ", " << firstName << " Hired: "
<< hireDate.toString() << " Birthday: " << birthDate.toString() << " ID Num: "<<idNum <<" Hourly Wage: " << hourlyWage ;
return output.str();
}

// output Employee object to show when its destructor is called
Employee::~Employee() {
//cout << "Employee object destructor: " << lastName << ", " << firstName << endl;
}

// Demonstrating composition--an object with member objects.

int main() {
Date birth1{7, 24, 1949};
Date hire1{3, 12, 1988};
Date birth2{12, 21, 1967};
Date hire2{3, 12, 1994};
Date birth3{7, 15, 1961};
Date hire3{5, 17, 1991};
Date birth4{6, 12, 1959};
Date hire4{4, 18, 1992};
int id1 = 1001;
float wage1 = 12.0;
int id2 = 1002;
float wage2 = 5.0;
int id3 = 1003;
float wage3 = 55.0;
int id4 = 1004;
float wage4 = 26.0;
try{
Employee manager1{"Bob", "Blue", birth1, hire1, id1, wage1};
cout << "\n" << manager1.toString() << endl;
} catch (const char* msg) {
cerr << msg << endl;
}

try{
Employee manager2{"Tom", "Blue", birth2, hire2, id2, wage2};
cout << "\n" << manager2.toString() << endl;
} catch (const char* msg) {
cerr << msg << endl;
}   

try{
Employee manager3{"Samule", "T", birth3, hire3, id3, wage3};
cout << "\n" << manager3.toString() << endl;
} catch (const char* msg) {
cerr << msg << endl;
}

try{
Employee manager4{"Herry", "Tin", birth4, hire4, id4, wage4};
cout << "\n" << manager4.toString() << endl;
} catch (const char* msg) {
cerr << msg << endl;
}   
}

Add a comment
Know the answer?
Add Answer to:
C++ edit: You start with the code given and then modify it so that it does...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please implement the following problem in basic C++ code and include detailed comments so that I...

    Please implement the following problem in basic C++ code and include detailed comments so that I am able to understand the processes for the solution. Thanks in advance. // personType.h #include <string> using namespace std; class personType { public: virtual void print() const; void setName(string first, string last); string getFirstName() const; string getLastName() const; personType(string first = "", string last = ""); protected: string firstName; string lastName; }; // personTypeImp.cpp #include <iostream> #include <string> #include "personType.h" using namespace std; void...

  • "Function does not take 0 arguments". I keep getting this error for my last line of...

    "Function does not take 0 arguments". I keep getting this error for my last line of code: cout << "First Name: " << employee1.getfirstName() << endl; I do not understand why. I am trying to put the name "Mike" into the firstName string. Why is my constructor not working? In main it should be set to string firstName, string lastName, int salary. Yet nothing is being put into the string. #include<iostream> #include<string> using namespace std; class Employee {    int...

  • The following C++ code include 3 files: Patient.h, Patient.cpp and Main.cpp. The program basically creates and stores patient records. The original code has everything in a single .cpp file. I tried t...

    The following C++ code include 3 files: Patient.h, Patient.cpp and Main.cpp. The program basically creates and stores patient records. The original code has everything in a single .cpp file. I tried to divide the code in 3 parts (Patient.h, Patient.cpp and Main.cpp), but it is giving me errors. Patient.h #ifndef PATIENT_H #define PATIENT_H #include <string> #include "Patient.cpp" using namespace std; class Patient{ private : string firstname; string lastname; string location; static int cnt; int id; public : Patient(string, string, string);...

  • This is for my c++ class and im stuck. Complete the Multiplex.cpp file with definitions for a fun...

    This is for my c++ class and im stuck. Complete the Multiplex.cpp file with definitions for a function and three overloaded operators. You don't need to change anything in the Multiplex.h file or the main.cpp, though if you want to change the names of the movies or concession stands set up in main, that's fine. Project Description: A Multiplex is a complex with multiple movie theater screens and a variety of concession stands. It includes two vectors: screenings holds pointers...

  • Here is the code from the previous three steps: #include <iostream> using namespace std; class Student...

    Here is the code from the previous three steps: #include <iostream> using namespace std; class Student { private: //class variables int ID; string firstName,lastName; public: Student(int ID,string firstName,string lastName) //constructor { this->ID=ID; this->firstName=firstName; this->lastName=lastName; } int getID() //getter method { return ID; } virtual string getType() = 0; //pure virtual function virtual void printInfo() //virtual function to print basic details of a student { cout << "Student type: " << getType() << endl; cout << "Student ID: " << ID...

  • C++ 1. The copy constructor is used for one the following scenarios: I. Initialize one object...

    C++ 1. The copy constructor is used for one the following scenarios: I. Initialize one object from another of the same type. II. Copy an object to pass it as argument to a function. III. Copy an object to return it from a function. Read the following program and answer questions (20 points) #include <iostream> using namespace std; class Line public: int getLength( void); Line( int len // simple constructor Line( const Line &obj) 1/ copy constructor Line (); //...

  • C++ This exercise will introduce static member variables and static methods in class to you. Class...

    C++ This exercise will introduce static member variables and static methods in class to you. Class Department contain information about universities departments: name students amount in addition it also stores information about overall amount of departments at the university: departments amount class Department { public: Department(string i_name, int i_num_students); ~Department(); int get_students(); string get_name(); static int get_total(); private: string name; int num_students; static int total_departments; }; Carefully read and modify the template. You have to implement private static variable "total...

  • How could I separate the following code to where I have a gradebook.cpp and gradebook.h file?...

    How could I separate the following code to where I have a gradebook.cpp and gradebook.h file? #include <iostream> #include <stdio.h> #include <string> using namespace std; class Gradebook { public : int homework[5] = {-1, -1, -1, -1, -1}; int quiz[5] = {-1, -1, -1, -1, -1}; int exam[3] = {-1, -1, -1}; string name; int printMenu() { int selection; cout << "-=| MAIN MENU |=-" << endl; cout << "1. Add a student" << endl; cout << "2. Remove a...

  • Greetings, everybody I already started working in this program. I just need someone to help me...

    Greetings, everybody I already started working in this program. I just need someone to help me complete this part of the assignment (my program has 4 parts of code: main.cpp; Student.cpp; Student.h; StudentGrades.cpp; StudentGrades.h) Just Modify only the StudentGrades.h and StudentGrades.cpp files to correct all defect you will need to provide implementation for the copy constructor, assignment operator, and destructor for the StudentGrades class. Here are my files: (1) Main.cpp #include <iostream> #include <string> #include "StudentGrades.h" using namespace std; int...

  • In c++ redefine the class personType to take advantage of the new features of object-oriented design...

    In c++ redefine the class personType to take advantage of the new features of object-oriented design that you have learned, such as operator overloading, and then derive the class customerType. personType: #include <string> using namespace std; class personType { public: void print() const; //Function to output the first name and last name //in the form firstName lastName.    void setName(string first, string last); //Function to set firstName and lastName according to the //parameters. //Postcondition: firstName = first; lastName = last...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT