/* * democlass1.cpp * demonstration usage of class * CIS 250 * David Klick * 1/24/05 * */ #include #include #include using std::strcpy; class date { public: int month; int day; int year; }; class Employee { private: char lastname[15]; unsigned long id; double rate; date hiredate; public: void print(); void setfields(char*, unsigned long, double, int, int, int); }; int main(void) { Employee emp[5]; // set members of emp1 and display emp[0].setfields("Smithson", 1786, 32.00, 3, 3, 1996); emp[1].setfields("Guimond", 3654, 17.95, 4, 2, 2004); emp[2].setfields("Crenshaw", 9812, 14.00, 11, 1, 2000); emp[3].setfields("Lovitz", 17543, 27.25, 7, 10, 1999); // use assignment operator emp[4] = emp[0]; for (int i=0; i<5; i++) { emp[i].print(); } return 0; } void Employee::print() { using std::cout; using std::endl; using std::setw; using std::setfill; using std::showpoint; using std::setprecision; using std::setiosflags; using std::ios; using std::fixed; cout << setw(15) << lastname << " ID=" << setfill('0') << setiosflags(ios::fixed | ios::scientific) << setprecision(6) << setw(6) << id << setfill(' ') << " rate=$" << setw(6) << fixed << showpoint << setprecision(2) << rate << " hired=" << setw(2) << setfill('0') << hiredate.month << "/" << setw(2) << hiredate.day << '/' << setw(2) << (hiredate.year % 100) << setfill(' ') << endl; } void Employee::setfields(char* nm, unsigned long id, double hrate, int m, int d, int y) { strcpy(lastname, nm); this->id = id; rate = hrate; hiredate.month = m; hiredate.day = d; hiredate.year = y; } /* 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 */