?? main.cpp
字號:
#include <iostream>#include <fstream>#include <stdexcept>#include "employee.h"#include "hourly.h"#include "salaried.h"#include "emp_exceptions.h"using namespace std;int main() { // Read hourly employee from standard input cout << "Enter info for hourly employee" << endl; Hourly h; try { cin >> h; } catch(runtime_error e) { // Display error message stored in e cout << "Runtime error: " << e.what() << endl; } // Read salaried employee from a file Salaried s; do { cout << "Enter filename for salaried employee: "; string fname; cin >> fname; // Construct input file stream object from name ifstream ifs( fname.c_str() ); // Make sure the file exists if ( ifs.fail() ) { cout << "File not found!" << endl; continue; } // Read employee from file ifs >> s; break; } while ( true ); try { // Setting hours this low will cause an exception h.SetHoursPerWeek( 5 ); } catch(LazyException e) { // This will retrieve info about the error that // caused this exception to be thrown e.Complain(); } // Now, write both employee objects to a file cout << "Enter output filename: "; string oname; cin >> oname; // This creates the output file if it doesn't exist, // and overwrites it if it does! ofstream os( oname.c_str() ); try { // Output the Hourly object os << "Hourly Employee:\n================\n"; os << h << endl; } catch(OverpaidException e) { // First see if an OverpaidException has been // thrown, and if so, display error info, e.Complain(); // and add "specialized error handling" cout << "Earn your zillion nickels!" << endl; } catch(LazyException e) { // An OverpaidException has not been thrown, so // check for a LazyException e.Complain(); cout << "Double shift for you!" << endl; } catch(...) { // This catches anything else cout << "Some other exception was thrown" << endl; } try { // Then output the Salaried object os << "Salaried Employee:\n==================\n"; os << s; } catch(EmpException& e) { // This will catch any object derived from // EmpException, but we need to use a reference // because EmpException is an abstract base class e.Complain(); } return 0;}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -