Exemplo n.º 1
0
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
Exemplo n.º 2
0
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;
}