int main() { // set floating-point output formatting cout << fixed << setprecision( 2 ); // create vector of four base-class pointers vector < Employee * > employees( 4 ); // initialize vector with various kinds of Employees employees[ 0 ] = new SalariedEmployee( "John", "Smith", "111-11-1111", 800 ); employees[ 1 ] = new HourlyEmployee( "Karen", "Price", "222-22-2222", 16.75, 40 ); employees[ 2 ] = new CommissionEmployee( "Sue", "Jones", "333-33-3333", 10000, .06 ); employees[ 3 ] = new BasePlusCommissionEmployee( "Bob", "Lewis", "444-44-4444", 5000, .04, 300 ); // polymorphically process each element in vector employees for ( size_t i = 0; i < employees.size(); i++ ) { employees[ i ]->print(); // output employee information cout << endl; // downcast pointer BasePlusCommissionEmployee *derivedPtr = dynamic_cast < BasePlusCommissionEmployee * > ( employees[ i ] ); // determine whether element points to base-salaried // commission employee if ( derivedPtr != 0 ) // 0 if not a BasePlusCommissionEmployee { double oldBaseSalary = derivedPtr->getBaseSalary(); cout << "old base salary: $" << oldBaseSalary << endl; derivedPtr->setBaseSalary( 1.10 * oldBaseSalary ); cout << "new base salary with 10% increase is: $" << derivedPtr->getBaseSalary() << endl; } // end if cout << "earned $" << employees[ i ]->earnings() << "\n\n"; } // end for // release objects pointed to by vector’s elements for ( size_t j = 0; j < employees.size(); j++ ) { // output class name cout << "deleting object of " << typeid( *employees[ j ] ).name() << endl; delete employees[ j ]; } // end for return 0; } // end main
int main() { // set floating-point output formatting cout << fixed << setprecision( 2 ); // create vector of four base-class pointers vector < Employee * > employees( 4 ); // initialize vector with Employees employees[ 0 ] = new SalariedEmployee( "John", "Smith", "111-11-1111", 6, 15, 1944, 800 ); employees[ 1 ] = new HourlyEmployee( "Karen", "Price", "222-22-2222", 12, 29, 1960, 16.75, 40 ); employees[ 2 ] = new CommissionEmployee( "Sue", "Jones", "333-33-3333", 9, 8, 1954, 10000, .06 ); employees[ 3 ] = new BasePlusCommissionEmployee( "Bob", "Lewis", "444-44-4444", 3, 2, 1965, 5000, .04, 300 ); int month = determineMonth(); cout << "Employees processed polymorphically via dynamic binding:\n\n"; for ( size_t i = 0; i < employees.size(); i++ ) { employees[ i ]->print(); // output employee information cout << endl; // downcast pointer BasePlusCommissionEmployee *derivedPtr = dynamic_cast < BasePlusCommissionEmployee * > ( employees[ i ] ); // determine whether element points to base-salaried // commission employee if ( derivedPtr != 0 ) // 0 if not a BasePlusCommissionEmployee { double oldBaseSalary = derivedPtr->getBaseSalary(); cout << "old base salary: $" << oldBaseSalary << endl; derivedPtr->setBaseSalary( 1.10 * oldBaseSalary ); cout << "new base salary with 10% increase is: $" << derivedPtr->getBaseSalary() << endl; } // end if // get current employee's birthday Date birthday = employees[ i ]->getBirthDate(); // if current month is employee's birthday month, add $100 to salary if ( birthday.getMonth() == month ) cout << "HAPPY BIRTHDAY!\nearned $" << ( employees[ i ]->earnings() + 100.0 ) << endl; else cout << "earned $" << employees[ i ]->earnings() << endl; cout << endl; } // end for // release objects pointed to by vector’s elements for ( size_t j = 0; j < employees.size(); j++ ) { // output class name cout << "deleting object of " << typeid( *employees[ j ] ).name() << endl; delete employees[ j ]; } // end for } // end main