/* * demostruct1.cpp * demonstration usage of struct * CIS 250 * David Klick * 1/24/05 * */ #include #include #include using std::strcpy; struct emptype { char lastname[15]; unsigned long id; double rate; }; void printemp(emptype e); void change1(emptype e); void change2(emptype& e); int main(void) { emptype emp1, emp2; // set members of emp1 and display strcpy(emp1.lastname, "Jackson"); emp1.id = 123456; emp1.rate = 27.50; printemp(emp1); // use assignment operator emp2 = emp1; printemp(emp2); change1(emp1); printemp(emp1); change2(emp1); printemp(emp1); return 0; } void printemp(emptype e) { 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) << e.lastname << " ID=" << setfill('0') << setiosflags(ios::fixed | ios::scientific) << setprecision(6) << setw(6) << e.id << setfill(' ') << " rate=$" << setw(6) << fixed << showpoint << setprecision(2) << e.rate << endl; } void change1(emptype e) { e.id = 27; } void change2(emptype& e) { e.id = 42; } /* Sample output: Jackson ID=123456 rate=$ 27.50 Jackson ID=123456 rate=$ 27.50 Jackson ID=123456 rate=$ 27.50 Jackson ID=000042 rate=$ 27.50 */