| | | | | |

The Static Case: Compile Time Bindings - 1

A Test Client

  1. Declare a static variable of each type in the class hierarchy
  2. Selectively call the class methods for a variable selected by the user
  3. Return, letting the variables go out of scope
int main()
{
  std::cout << "Declaring variables:\n";
  B  b;
  D1 d1;
  D2 d2;
  D3 d3;
  D4 d4;

  int n;
  do
    {
      std::cout << "Enter type number (-1 to quit): ";
      std::cin >> n;
      if (std::cin.fail() || n < 0) break;
      switch(n)
        {
        case 0:
          b.F();
          b.G();
          b.H();
          break;
        case 1:
          d1.F();
          d1.G();
          d1.H();
          break;
        case 2:
          d2.F();
          d2.G();
          d2.H();
          break;
        case 3:
          d3.F();
          d3.G();
          d3.H();
          break;
        case 4:
          d4.F();
          d4.G();
          d4.H();
          break;
        default:
          return EXIT_SUCCESS;
        } // switch
    } // do
  while (1);
  std::cout << "Variables going out of scope:\n";
  return EXIT_SUCCESS;
}

| | Top of Page | 10. C++ Classes Part 4: Polymorphism and OO Programming - 5 of 18