Code /* * class2.cpp * CIS 250 * David Klick * 2018-01-12 * * Demonstration usage of basic detailed class structure. * * Compile using: g++ -Wall -std=c++11 -o class2 class2.cpp * Run using: ./class2 */ #include #include #include /* Since no constructor is specified, a default constructor is automatically created for this class. Likewise, a destructor, copy constructor, and assignment operator overload are also created automatically. */ class date { public: int month; int day; int year; }; class Employee { private: // The following static variable will only have a single copy, // created when the class is loaded, and shared by all of the // objects of this class. It must be initialized outside of the // class declaration. static int count; // Each object gets a separate copy of each of the rest of these. std::string lastname; unsigned long id; double rate; date hiredate; void init(std::string nm, unsigned long id, double rt, int y, int m, int d); public: Employee(); // Default constructor Employee(const Employee& emp); // Copy constructor Employee(std::string nm, unsigned long id, double rt, int y, int m, int d); // Accessor methods static int getCount(); unsigned long getId() const; double getRate() const; date getHireDate() const; // Mutator methods void setLastName(std::string nm); void setId(unsigned long id); void setRate(double rt); void setHireDate(date dt); void setHireDate(int y, int m, int d); // Destructor ~Employee(); friend std::ostream& operator<<(std::ostream& strm, const Employee& emp); }; // Initialization of static variable. int Employee::count = 0; void Employee::init(std::string nm, unsigned long id, double rt, int y, int m, int d) { setLastName(nm); this->id = id; // this pointer needed because of variable name duplication setRate(rt); setHireDate(y, m, d); count++; } Employee::Employee() { init("Unknown", 0, 0.0, 1970, 1, 1); } Employee::Employee(const Employee& emp) { init(emp.lastname, emp.id, emp.rate, emp.hiredate.year, emp.hiredate.month, emp.hiredate.day); } Employee::Employee(std::string nm, unsigned long id, double rt, int y, int m, int d) { init(nm, id, rt, y, m, d); } void Employee::setLastName(std::string nm) { lastname = nm; } void Employee::setId(unsigned long id) { this->id = id; } void Employee::setRate(double rt) { if (rt < 0.0) rate = 0.0; else rate = rt; } void Employee::setHireDate(date dt) { setHireDate(dt.year, dt.month, dt.day); } void Employee::setHireDate(int y, int m, int d) { hiredate.year = y; hiredate.month = m; hiredate.day = d; } int Employee::getCount() { return count; } unsigned long Employee::getId() const { return id; } double Employee::getRate() const { return rate; } date Employee::getHireDate() const { return hiredate; } Employee::~Employee() { count--; } std::ostream& operator<<(std::ostream& strm, const Employee& emp) { strm << std::setw(15) << emp.lastname << " ID=" << std::setfill('0') << std::setw(6) << emp.id << std::setfill(' ') << " rate=$" << std::setw(6) << std::fixed << std::showpoint << std::setprecision(2) << emp.rate << " hired=" << std::setw(2) << std::setfill('0') << emp.hiredate.month << "/" << std::setw(2) << emp.hiredate.day << '/' << std::setw(2) << (emp.hiredate.year % 100) << std::setfill(' '); return strm; } int main(void) { Employee emp[5] = { {"Smithson", 1786, 32.00, 1996, 3, 3}, {"Guimond", 3654, 17.95, 2004, 4, 2}, {"Crenshaw", 9812, 14.00, 2000, 11, 1}, {"Lovitz", 17543, 27.25, 1999, 7, 10}, {}}; // use assignment operator emp[4] = emp[0]; for (int i=0; i<5; i++) std::cout << emp[i] << std::endl; return 0; } /* Sample output: Smithson ID=001786 rate=$ 32.00 hired=03/03/96 Guimond ID=003654 rate=$ 17.95 hired=04/02/04 Crenshaw ID=009812 rate=$ 14.00 hired=11/01/00 Lovitz ID=017543 rate=$ 27.25 hired=07/10/99 Smithson ID=001786 rate=$ 32.00 hired=03/03/96 */