⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.cpp

📁 斯坦福Energy211/CME211课《c++编程——地球科学科学家和工程师》的课件
💻 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 + -