Exemplo n.º 1
0
int main(int argc, const char* argv[]) {
    // set floating-point output formatting
    std::cout << std::fixed << std::setprecision(2);

    // create derived-class objects
    SalariedEmployee salariedEmployee("John", "Smi.hpp", "111-11-1111", 800);
    CommissionEmployee commissionEmployee("Sue", "Jones", "333-33-3333", 10000,
                                          .06);
    BasePlusCommissionEmployee basePlusCommissionEmployee(
        "Bob", "Lewis", "444-44-4444", 5000, .04, 300);

    std::cout << "Employees processed individually using static binding:\n\n";

    // output each Employee's information and earnings using static binding
    salariedEmployee.print();
    std::cout << "\nearned $" << salariedEmployee.earnings() << "\n\n";
    commissionEmployee.print();
    std::cout << "\nearned $" << commissionEmployee.earnings() << "\n\n";
    basePlusCommissionEmployee.print();
    std::cout << "\n\nearned $" << basePlusCommissionEmployee.earnings()
              << "\n\n";

    // create vector of three base-class pointers
    std::vector<Employee*> employees(3);

    // initialise vector with Employees
    employees[0] = &salariedEmployee;
    employees[1] = &commissionEmployee;
    employees[2] = &basePlusCommissionEmployee;

    std::cout << "Employees processed polymorphically via dynamic binding:\n\n";

    // call virtualViaPointer to print each Employee's information
    // and earnings using dynamic binding
    std::cout << "Virtual function call made off base-class pointers:\n\n";

    for (size_t i = 0; i < employees.size(); ++i) {
        virtualViaPointer(employees[i]);
    }

    // call virtualViaReference to print each Employee's information
    // and earnings using dynamic binding
    std::cout << "Virtual function calls made off base-class references:\n\n";

    for (size_t i = 0; i < employees.size(); ++i) {
        virtualViaReference(*employees[i]);  // not dereferencing
    }

    return 0;
}
int main()
{
	// set floating-point output formatting
	cout << fixed << setprecision(2);

	// create vector of base-class pointers
	vector < Employee * > employees(3);

	// initialize vector with Employees
	employees[0] = new SalariedEmployee("John", "Smith", "111-11-1111", 7, 10, 1957, 800);
	employees[1] = new CommissionEmployee("Sue", "Jones", "333-33-3333", 3, 8, 1971, 10000, .06); // brithday month so should get bonus
	employees[2] = new BasePlusCommissionEmployee("Bob", "Lewis", "444-44-4444", 9, 25, 1968, 5000, .04, 300);

	int month = determineMonth();
	
	cout << "Employees processed polymorphically using dynamic binding" << "\n\n";
	
	cout << "Virtual function calls made off base-class pointers: \n\n";
	
	// polymorphically process each element in vector employees
	for (size_t i = 0; i < employees.size(); ++i)
	{
		virtualViaPointer(employees[i]); // output employee information
		
		// get current employee's birthday
		Date birthday = employees[i]->getBirthDate();

		// if current month is employee's birthday month, add $100 bonus to payroll amount
		if (birthday.getMonth() == month)
			cout << "\nHappy Birthday!\nYou recieved an extra $100 bonus \nearned with bonus $"
			<< (employees[i]->earnings() + 100.0) << endl << endl;
		else
			cout << endl << endl;

	}
	
	cout << "Virtual function calls made off base-class references: \n\n";
	
	for (size_t i = 0; i < employees.size(); ++i)
	{
		virtualViaReference(*employees[i]); // output employee information

		// get current employee's birthday
		Date birthday = employees[i]->getBirthDate();

		// if current month is employee's birthday month, add $100 bonus to payroll amount
		if (birthday.getMonth() == month)
			cout << "\nHappy Birthday!\nYou recieved an extra $100 bonus \nearned with bonus $"
			<< (employees[i]->earnings() + 100.0) << endl << endl;
		else
			cout << endl << endl;

	}
	
	cout << endl;

	// 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];
	}
	
	getch();

	return 0;
}