示例#1
0
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
示例#2
0
文件: main.cpp 项目: rosen90/GitHub
int main()
{

	// create base-class object
	CommissionEmployee commissionEmployee(
			"Sue", "Jones", "222-22-2222", 10000, .06);

	//create base-class pointer
	CommissionEmployee *commissionEmployeePtr = 0;

	//create derived-class object
	BasePlusCommissionEmployee basePlusCommissionEmployee(
		"Bob", "Lewis", "333-33-3333", 5000, .04, 300);

	//create derived-class pointer
	BasePlusCommissionEmployee *basePlusCommissionEmployeePtr = 0;

	// set floating - point output formatting
	cout << fixed << setprecision(2);

	//output objects commissionEmployee and basePlusCommissionEmployee
	cout << "Print base-class and derived-class objects:\n\n";

	commissionEmployee.print();
	cout << "\n\n";
	basePlusCommissionEmployee.print();

	// aim base-class pounter at base-class object and print
	commissionEmployeePtr = &commissionEmployee; // perfectly natural
	cout << "\n\n\nCalling print with base-class pointer to "
		<< "\nbase-class object invokes base-class print funciton\n\n";
	commissionEmployeePtr->print(); //invokes base-class print function

	//aim derived-class pointer at derived-class object and print
	basePlusCommissionEmployeePtr = &basePlusCommissionEmployee; // natural
	cout << "\n\n\nCalling print with derived-class pointer to "
		<< "\nderived-class object invokes derived-class "
		<< "print function: \n\n";
	basePlusCommissionEmployeePtr->print();

	//aim base-class pointer at derived-class object and print
	commissionEmployeePtr = &basePlusCommissionEmployee;
	cout << "\n\n\nCalling print with base-class pointer to "
		<< "derived-class object\ninvokes base-class print "
		<< "function on that derived-class object:\n\n";
	commissionEmployeePtr->print();
	cout << endl;


	return 0;
} // end main
int main(int argc, const char *argv[]) {
    // create base-class object
    CommissionEmployee commissionEmployee("Sue", "Jones", "222-22-2222", 10000,
                                          .06);

    // create base-class pointer
    CommissionEmployee *commissionEmployeePtr = 0;

    // create derived-class object
    BasePlusCommissionEmployee basePlusCommissionEmployee(
        "Bob", "Lewis", "333-33-3333", 5000, .04, 300);

    // create dervied-class pointer
    BasePlusCommissionEmployee *basePlusCommissionEmployeePtr = 0;

    // set floating-point output formatting
    std::cout << std::fixed << std::setprecision(2);

    // output objects using static binding
    std::cout << "Invoking print function on base-class and derived-class "
              << "\nobject with static binding\n\n";
    commissionEmployee.print();  // static binding
    std::cout << "\n\n";
    basePlusCommissionEmployee.print();  // static binding

    // output objects with dynamic binding
    std::cout << "\n\n\nInvoking print function on base-class and "
              << "derived-class \nobjects with dynamic binding";

    // aim base-class pointer at base-class object and print
    commissionEmployeePtr = &commissionEmployee;
    std::cout << "\n\nCalling virtual function print with derived-class "
              << "pointer\nto derived-class object invokes derived-class "
              << "print function:\n\n";
    basePlusCommissionEmployeePtr->print();

    // aim base-class pointer at derived-class object and print
    commissionEmployeePtr = &basePlusCommissionEmployee;
    std::cout << "\n\nCalling virtual function print with base-class pointer"
              << "\nto derived-class object invokes derived-class "
              << "print function:\n\n";

    // polymorphism; invokes BasePlusCommissionEmployee's print;
    // base-class pointer to derived-class object
    commissionEmployeePtr->print();

    std::cout << std::endl;

    return 0;
}
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