📄 fig13_10.cpp
字号:
// Fig. 13.10: fig13_10.cpp
// Introducing polymorphism, virtual functions and dynamic binding.
#include <iostream>
using std::cout;
using std::endl;
using std::fixed;
#include <iomanip>
using std::setprecision;
// include class definitions
#include "CommissionEmployee.h"
#include "BasePlusCommissionEmployee.h"
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 using static binding
cout << "Invoking print function on base-class and derived-class "
<< "\nobjects with static binding\n\n";
commissionEmployee.print(); // static binding
cout << "\n\n";
basePlusCommissionEmployee.print(); // static binding
// output objects using dynamic binding
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;
cout << "\n\nCalling virtual function print with base-class pointer"
<< "\nto base-class object invokes base-class "
<< "print function:\n\n";
commissionEmployeePtr->print(); // invokes base-class print
// aim derived-class pointer at derived-class object and print
basePlusCommissionEmployeePtr = &basePlusCommissionEmployee;
cout << "\n\nCalling virtual function print with derived-class "
<< "pointer\nto derived-class object invokes derived-class "
<< "print function:\n\n";
basePlusCommissionEmployeePtr->print(); // invokes derived-class print
// aim base-class pointer at derived-class object and print
commissionEmployeePtr = &basePlusCommissionEmployee;
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();
cout << endl;
return 0;
} // end main
/**************************************************************************
* (C) Copyright 1992-2005 by Deitel & Associates, Inc. and *
* Pearson Education, Inc. All Rights Reserved. *
* *
* DISCLAIMER: The authors and publisher of this book have used their *
* best efforts in preparing the book. These efforts include the *
* development, research, and testing of the theories and programs *
* to determine their effectiveness. The authors and publisher make *
* no warranty of any kind, expressed or implied, with regard to these *
* programs or to the documentation contained in these books. The authors *
* and publisher shall not be liable in any event for incidental or *
* consequential damages in connection with, or arising out of, the *
* furnishing, performance, or use of these programs. *
**************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -