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

📄 cpp06.cpp

📁 C++参考书
💻 CPP
字号:

// Coded by plusir -- Jan.08.2003.
// Standard C++ Bible -- (P425-13-28)

#include "wagedemployee.h"
#include "salariedemployee.h"
#include "contractor.h"
using namespace std ;

string readString( const string & ) ;
Date readDate( const string & ) ;
Money readMoney( const string & ) ;
void personInput( Person * ) ;
void employeeInput( Employee * ) ;
void wagedEmployeeInput( WagedEmployee * ) ;
void salariedEmployeeInput( SalariedEmployee * ) ;
void contractorInput( Contractor * ) ;

int main()
{
	Person *pPerson = NULL ;
	cout << "1 - Salaried employee" << endl ;
	cout << "2 - Waged employee" << endl ;
	cout << "3 - Contractor" << endl ;
	cout << "Enter selection: " ;

	int sel ;
	cin >> sel ;

	switch ( sel ) {

		case 1:
			pPerson = new SalariedEmployee ;
			personInput( pPerson ) ;
			employeeInput( ( Employee* )pPerson ) ;
			salariedEmployeeInput( ( SalariedEmployee* )pPerson ) ;
			cout << endl ;
			break ;

		case 2:
			pPerson = new WagedEmployee ;
			personInput( pPerson ) ;
			employeeInput( ( Employee* )pPerson ) ;
			wagedEmployeeInput( ( WagedEmployee* )pPerson ) ;
			cout << endl ;
			break ;

		case 3:
			pPerson = new Contractor ;
			personInput( pPerson ) ;
			contractorInput( ( Contractor* )pPerson ) ;
			cout << endl ;
			break ;

		default:
			cerr << "\aIncorrect entry" << endl ;
			break ;
	}

	if ( pPerson != NULL ) {
		pPerson->formattedDisplay( cout ) ;
		delete pPerson ;
	}

	return 0 ;
}

string readString( const string &prompt )
{
	string str ;
	cout << prompt << ": " ;
	getline( cin, str ) ;

	return str ;
}

Date readDate( const string &prompt )
{
	Date dt ;
	cout << prompt << "( mm dd yyyy ): " ;
	cin >> dt ;

	return dt ;
}

Money readMoney( const string &prompt )
{
	double mn ;
	cout << prompt << ": " ;
	cin >> mn ;

	return mn ;
}

void personInput( Person *pPerson )
{
	static string str ;
	getline( cin, str ) ;

	pPerson->setName( readString( "Name" ) ) ;
	pPerson->setAddress( readString( "Address" ) ) ;
	pPerson->setPhone( readString( "Phone" ) ) ;
	pPerson->setDob( readDate( "Date of birth" ) ) ;

	long int ssn ;
	cout << "SSN: " ;
	cin >> ssn ;
	pPerson->setSSN( SSN( ssn ) ) ;

	char sx ;
	do {
		cout << "Sex( m/f ): " ;
		cin >> sx ;
	} while ( sx != 'm' && sx != 'f' ) ;
	pPerson->setSex( sx == 'm' ? Person::MALE : Person::FEMALE ) ;
}

void employeeInput( Employee *pEmployee )
{
	pEmployee->setDateHired( readDate( "Date hired" ) ) ;
}

void wagedEmployeeInput( WagedEmployee *pWagedEmployee )
{
	pWagedEmployee->setHourlyWage( readMoney( "Hourly wage" ) ) ;
}

void salariedEmployeeInput( SalariedEmployee *pSalariedEmployee )
{
	pSalariedEmployee->setSalary( readMoney( "Salary" ) ) ;
}

void contractorInput( Contractor *pContractor )
{
	pContractor->setStartDate( readDate( "Start date" ) ) ;
	pContractor->setEndDate( readDate( "End date" ) ) ;
	pContractor->setHourlyRate( readMoney( "Hourly rate" ) ) ;
}

⌨️ 快捷键说明

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