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

📄 employee.cpp

📁 斯坦福Energy211/CME211课《c++编程——地球科学科学家和工程师》的课件
💻 CPP
字号:
// Implementation file for the Employee class#include "employee.h"#include <stdexcept>using namespace std;Employee::Employee() {	m_Name = "";	m_SSN = "";}Employee::Employee( const string& name, const string& ssn ) {	// Store arguments in member variables	cout << "Constructing an employee!" << endl;	m_Name = name;	m_SSN = ssn;	// Instead of throwing exception(), throw object	// that can store an error message	if ( IsValidSSN() == false )		throw runtime_error( "Invalid SSN!" );}Employee::Employee( const Employee& e ) {	// Copy data members from e	m_Name = e.m_Name;	m_SSN = e.m_SSN;}Employee::~Employee() {	// Nothing to destroy here, because the data members	// will be de-allocated automatically	cout << "Destroying an employee!" << endl;}void Employee::SendTo( ostream& out ) const {	// Output data members	out << "Name: " << m_Name << endl;	out << "SSN: " << m_SSN << endl;	out << "Yearly Pay: $" << GetYearlyPay() << endl;}void Employee::GetFrom( istream& in, bool prompt ) {	// Read data members	if ( prompt == true )		cout << "Enter First Name: ";	string firstname;	in >> firstname;	if ( in == false )		return;		if ( prompt == true )		cout << "Enter Last Name: ";	string lastname;	in >> lastname;		// Use an output string stream to "write" the	// first and last name to m_Name	ostringstream os;	os << firstname << " " << lastname;	m_Name = os.str();		if ( prompt == true )		cout << "Enter SSN: ";	in >> m_SSN;	// Make sure SSN has valid format	// Instead of throwing exception(), throw object	// that can store an error message	if ( IsValidSSN() == false )		throw runtime_error( "Invalid SSN!" );}bool Employee::IsValidSSN() const {	// Check SSN format by attempting to "read" a 3-digit	// number, a dash, a 2-digit number, another dash, and	// a 4-digit number from m_SSN	istringstream is( m_SSN );	// Read first number	if ( GetSSNPart( is, 999 ) == false )		return false;	// Check for first dash	char c = is.get();	if ( is == false )		return false;	if ( c != '-' )		return false;	// Read second number	if ( GetSSNPart( is, 99 ) == false )		return false;		// Check for second dash	c = is.get();	if ( is == false )		return false;	if ( c != '-' )		return false;		// Read third number	if ( GetSSNPart( is, 9999 ) == false )		return false;		// Since we made it this far, it's valid!	return true;}bool Employee::GetSSNPart( istringstream& is, int maxval ){	// Read an integer from a string and make sure it's	// non-negative and does not exceed maxval.  We don't	// care what its value is, so we don't return it	int part(-1);	is >> part;	if ( is == false )		return false;	if ( part < 0 || part > maxval )		return false;	return true;}Employee& Employee::operator=( const Employee& e ) {	// We don't need this operator to do anything since	// we never want it to be used	return *this;}ostream& operator<<( ostream& out, const Employee& e ) {	// Regardless of the kind of employee is passed, the	// appropriate SendTo function will be called, because	// it's declared virutal in the Employee class	e.SendTo( out );	return out;}istream& operator>>( istream& in, Employee& e ) {	// Only prompt if we're reading from cin, as opposed	// to a file	bool prompt = ( in == cin );	// Regardless of the kind of employee is passed, the	// appropriate GetFrom function will be called, because	// it's declared virutal in the Employee class	e.GetFrom( in, prompt );	return in;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -