⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chap25.lst

📁 Borland C++ Builder The Complete Reference 例程源代码
💻 LST
字号:
listing 1
// Function template example. 
#include <iostream> 
using namespace std; 
 
// This is a function template. 
template <class X> void swapargs(X &a, X &b) 
{ 
  X temp; 
 
  temp = a; 
  a = b; 
  b = temp; 
} 
 
int main() 
{ 
  int i=10, j=20; 
  float x=10.1, y=23.3; 
  char a='x', b='z'; 
 
  cout << "Original i, j: " << i << ' ' << j << endl; 
  cout << "Original x, y: " << x << ' ' << y << endl; 
  cout << "Original a, b: " << a << ' ' << b << endl; 
 
  swapargs(i, j); // swap integers 
  swapargs(x, y); // swap floats 
  swapargs(a, b); // swap chars 
 
  cout << "Swapped i, j: " << i << ' ' << j << endl; 
  cout << "Swapped x, y: " << x << ' ' << y << endl; 
  cout << "Swapped a, b: " << a << ' ' << b << endl; 
 
  return 0; 
}

listing 2
template <class X> void swapargs(X &a, X &b)

listing 3
#include <iostream> 
using namespace std; 
 
template <class type1, class type2> 
void myfunc(type1 x, type2 y) 
{ 
  cout << x << ' ' << y << endl; 
} 
 
int main() 
{ 
  myfunc(10, "hi"); 
  myfunc(0.23, 10L); 
 
  return 0; 
} 


listing 4
// Overriding a template function. 
#include <iostream> 
using namespace std; 
 
template <class X> void swapargs(X &a, X &b) 
{ 
  X temp; 
 
  temp = a; 
  a = b; 
  b = temp; 
} 
 
// This overrides the generic version of swap(). 
void swapargs(int &a, int &b) 
{ 
  int temp; 
 
  temp = a; 
  a = b; 
  b = temp; 
  cout << "Inside overloaded swapargs(int &, int &).\n"; 
} 
 
int main() 
{ 
  int i=10, j=20; 
  float x=10.1, y=23.3; 
  char a='x', b='z'; 
 
  cout << "Original i, j: " << i << ' ' << j << endl; 
  cout << "Original x, y: " << x << ' ' << y << endl; 
  cout << "Original a, b: " << a << ' ' << b << endl; 
 
  swapargs(i, j); // this calls the explicitly overloaded swapargs() 
  swapargs(x, y); // swap floats 
  swapargs(a, b); // swap chars 
 
  cout << "Swapped i, j: " << i << ' ' << j << endl; 
  cout << "Swapped x, y: " << x << ' ' << y << endl; 
  cout << "Swapped a, b: " << a << ' ' << b << endl; 
 
  return 0; 
}

listing 5
// Use new-style specialization syntax 
template<> void swapargs<int>(int &a, int &b) 
{ 
  int temp; 
 
  temp = a; 
  a = b; 
  b = temp; 
  cout << "Inside specialized swapargs(int &, int &).\n"; 
}

listing 6
// Overload a function template declaration. 
#include <iostream> 
using namespace std; 
 
// First version of f() template. 
template <class X> void f(X a) 
{ 
  cout << "Inside f(X a)\n a = " << a << endl; 
} 
 
// Second version of f() template. 
template <class X, class Y> void f(X a, Y b) 
{ 
  cout << "Inside f(X a, Y b)\n a = " << a << "\n b = " << b << endl; 
} 
 
int main() 
{ 
  f(10);     // calls f(X) 
  f(10, 20); // calls f(X, Y) 
 
  return 0; 
}

listing 7
#include <iostream> 
#include <cmath> 
using namespace std; 
 
void myfunc(int i) 
{ 
  cout << "value is: " << i << "\n"; 
} 
 
void myfunc(double d) 
{ 
  double intpart; 
  double fracpart; 
 
  fracpart = modf(d, &intpart); 
  cout << "Fractional part: " << fracpart; 
  cout << "\n"; 
  cout << "Integer part: " << intpart; 
} 
 
int main() 
{ 
  myfunc(1); 
  myfunc(12.2); 
 
  return 0; 
}

listing 8
// Demonstrate a generic stack class. 
#include <iostream> 
using namespace std; 
 
const int SIZE = 100; 
 
// This creates the generic class stack. 
template <class SType> class stack { 
  SType stck[SIZE]; 
  int tos; 
public: 
  stack(); 
  ~stack(); 
  void push(SType i); 
  SType pop(); 
}; 
 
// stack's constructor function. 
template <class SType> stack<SType>::stack() 
{ 
  tos = 0; 
  cout << "Stack Initialized\n"; 
} 
 
/* stack's destructor function. 
   This function is not required.  It is included 
   for illustration only. */ 
template <class SType> stack<SType>::~stack() 
{ 
  cout << "Stack Destroyed\n"; 
} 
 
// Push an object onto the stack. 
template <class SType> void stack<SType>::push(SType i) 
{ 
  if(tos==SIZE) { 
    cout << "Stack is full.\n"; 
    return; 
  } 
  stck[tos] = i; 
  tos++; 
} 
 
// Pop an object off the stack. 
template <class SType> SType stack<SType>::pop() 
{ 
  if(tos==0) { 
    cout << "Stack underflow.\n"; 
    return 0; 
  } 
  tos--; 
  return stck[tos]; 
} 
 
int main() 
{ 
  stack<int> a; // create integer stack 
  stack<double> b; // create a double stack 
  stack<char> c; // create a character stack 
 
  int i; 
 
  // use the integer and double stacks 
  a.push(1); 
  b.push(99.3); 
  a.push(2); 
  b.push(-12.23); 
 
  cout << a.pop() << " "; 
  cout << a.pop() << " "; 
  cout << b.pop() << " "; 
  cout << b.pop() << "\n"; 
 
  // demonstrate the character stack 
  for(i=0; i<10; i++) c.push((char) 'A'+i); 
  for(i=0; i<10; i++) cout << c.pop(); 
  cout << "\n"; 
 
  return 0; 
}

listing 9
stack<int> a; // create integer stack 
stack<double> b; // create a double stack 
stack<char> c; // create a character stack

listing 10
stack<char *> chrptrstck;

listing 11
struct addr { 
  char name[40]; 
  char street[40]; 
  char city[30]; 
  char state[3]; 
  char zip[12]; 
}

listing 12
stack<addr> obj;

listing 13
/* This example uses two generic data types in a 
   class definition. 
*/ 
#include <iostream> 
using namespace std; 
 
template <class Type1, class Type2> class myclass 
{ 
  Type1 i; 
  Type2 j; 
public: 
  myclass(Type1 a, Type2 b) { i = a; j = b; } 
  void show() { cout << i << ' ' << j << '\n'; } 
}; 
 
int main() 
{ 
  myclass<int, double> ob1(10, 0.23); 
  myclass<char, char *> ob2('X', "This is a test"); 
 
  ob1.show(); // show int, double 
  ob2.show(); // show char, char * 
  
  return 0; 
}

listing 14
// A simple exception handling example. 
#include <iostream> 
using namespace std; 
 
int main() 
{ 
  cout << "Start\n"; 
 
  try { // start a try block 
    cout << "Inside try block\n"; 
    throw 100; // throw an error 
    cout << "This will not execute"; 
  } 
  catch (int i) { // catch an error 
    cout << "Caught an exception -- value is: "; 
    cout << i << "\n"; 
  } 
 
  cout << "End"; 
 
  return 0; 
}

listing 15
// This example will not work. 
#include <iostream> 
using namespace std; 
 
int main() 
{ 
  cout << "Start\n"; 
 
  try { // start a try block 
    cout << "Inside try block\n"; 
    throw 100; // throw an error 
    cout << "This will not execute"; 
  } 
  catch (double i) { // Won't work for an int exception 
    cout << "Caught an exception -- value is: "; 
    cout << i << "\n"; 
  } 
 
  cout << "End"; 
 
  return 0; 
}

listing 16
/* Throwing an exception from a function outside the 
   try block.  
*/ 
#include <iostream> 
using namespace std; 
 
void Xtest(int test) 
{ 
  cout << "Inside Xtest, test is: " << test << "\n"; 
  if(test) throw test; 
} 
 
int main() 
{ 
  cout << "Start\n"; 
 
  try { // start a try block 
    cout << "Inside try block\n"; 
    Xtest(0); 
    Xtest(1); 
    Xtest(2); 
  } 
  catch (int i) { // catch an error 
    cout << "Caught an exception -- value is: "; 
    cout << i << "\n"; 
  } 
 
  cout << "End"; 
 
  return 0; 
}

listing 17
#include <iostream> 
using namespace std; 
 
// A try/catch can be inside a function other than main(). 
void Xhandler(int test) 
{ 
  try{ 
    if(test) throw test; 
  } 
  catch(int i) { 
    cout << "Caught Exception #: " << i << '\n'; 
  } 
} 
 
int main() 
{ 
  cout << "Start\n"; 
 
  Xhandler(1); 
  Xhandler(2); 
  Xhandler(0); 
  Xhandler(3); 
 
  cout << "End"; 
 
  return 0; 
} 


listing 18
// Catching class type exceptions. 
#include <iostream> 
#include <cstring> 
using namespace std; 
 
class MyException { 
public: 
  char str_what[80]; 
  int what; 
 
  MyException() { *str_what = 0; what = 0; } 
 
  MyException(char *s, int e) { 
    strcpy(str_what, s); 
    what = e; 
  } 
}; 
 
int main() 
{ 
  int i; 
 
  try { 
    cout << "Enter a positive number: "; 
    cin >> i; 
    if(i<0) 
      throw MyException("Not Positive", i);  
  } 
  catch (MyException e) { // catch an error 
    cout << e.str_what << ": "; 
    cout << e.what << "\n"; 
  } 
 
  return 0; 
}

listing 19
#include <iostream> 
using namespace std; 
 
// Different types of exceptions can be caught. 
void Xhandler(int test) 
{ 
  try{ 
    if(test) throw test; 
    else throw "Value is zero"; 
  } 
  catch(int i) { 
    cout << "Caught Exception #: " << i << '\n'; 
  } 
  catch(char *str) { 
    cout << "Caught a string: "; 
    cout << str << '\n'; 
  } 
} 
 
int main() 
{ 
  cout << "Start\n"; 
 
  Xhandler(1); 
  Xhandler(2); 
  Xhandler(0); 
  Xhandler(3); 
 
  cout << "End"; 
 
  return 0; 
}

listing 20
// Catching derived classes. 
#include <iostream> 
using namespace std; 
 
class B { 
}; 
 
class D: public B { 
}; 
 
int main() 
{ 
  D derived; 
 
  try { 
    throw derived; 
  } 
  catch(B b) { 
    cout << "Caught a base class.\n"; 
  } 
  catch(D d) { 
    cout << "This won't execute.\n"; 
  } 
 
  return 0; 
}

listing 21
// This example catches all exceptions. 
#include <iostream> 
using namespace std; 
 
void Xhandler(int test) 
{ 
  try{ 
    if(test==0) throw test; // throw int 
    if(test==1) throw 'a'; // throw char 
    if(test==2) throw 123.23; // throw double 
  } 
  catch(...) { // catch all exceptions 
    cout << "Caught One!\n"; 
  } 
} 
 
int main() 
{ 
  cout << "Start\n"; 
 
  Xhandler(0); 
  Xhandler(1); 
  Xhandler(2); 
 
  cout << "End"; 
 
  return 0; 
} 


listing 22
// Restricting function throw types. 
#include <iostream> 
using namespace std; 
 
// This function can only throw ints, chars, and doubles. 
void Xhandler(int test) throw(int, char, double) 
{ 
  if(test==0) throw test; // throw int 
  if(test==1) throw 'a'; // throw char 
  if(test==2) throw 123.23; // throw double 
} 
 
int main() 
{ 
  cout << "start\n"; 
 
  try{ 
    Xhandler(0); // also, try passing 1 and 2 to Xhandler() 
  } 
  catch(int i) { 
    cout << "Caught an integer\n"; 
  } 
  catch(char c) {  
    cout << "Caught char\n"; 
  } 
  catch(double d) {  
    cout << "Caught double\n"; 
  } 
 
  cout << "end"; 
 
  return 0; 
}

listing 23
// This function can throw NO exceptions! 
void Xhandler(int test) throw() 
{ 
  /* The following statements no longer work.  Instead, 
     they will cause an abnormal program termination. */ 
  if(test==0) throw test;  
  if(test==1) throw 'a';  
  if(test==2) throw 123.23;  
}

listing 24
// Example of "rethrowing" an exception. 
#include <iostream> 
using namespace std; 
 
void Xhandler() 
{ 
  try { 
    throw "hello"; // throw a char * 
  } 
  catch(char *) { // catch a char * 
    cout << "Caught char * inside Xhandler\n"; 
    throw ; // rethrow char * out of function 
  } 
} 
 
int main() 
{ 
  cout << "Start\n"; 
 
  try{ 
    Xhandler(); 
  } 
  catch(char *) { 
    cout << "Caught char * inside main\n"; 
  } 
 
  cout << "End"; 
 
  return 0; 
}

listing 25
// Set a new terminate handler. 
#include <iostream> 
#include <cstdlib> 
#include <exception> 
using namespace std; 
 
void my_Thandler() { 
  cout << "Inside new terminate handler\n"; 
  abort(); 
} 
 
int main() 
{ 
  // set a new terminate handler 
  set_terminate(my_Thandler); 
 
  try {  
    cout << "Inside try block\n"; 
    throw 100; // throw an error 
  } 
  catch (double i) { // won't catch an int exception 
    // ... 
  } 
 
  return 0; 
}

listing 26
#include <iostream> 
using namespace std; 
 
void divide(double a, double b); 
 
int main() 
{ 
  double i, j; 
 
  do { 
    cout << "Enter numerator (0 to stop): "; 
    cin >> i; 
    cout << "Enter denominator: "; 
    cin >> j; 
    divide(i, j); 
  } while(i != 0); 
 
  return 0; 
} 
 
void divide(double a, double b) 
{ 
  try { 
    if(!b) throw b; // check for divide-by-zero    
    cout << "Result: " << a/b << endl; 
  } 
  catch (double b) { 
    cout << "Can't divide by zero.\n"; 
  } 
}

listing 27
// An example that uses typeid. 
#include <iostream> 
#include <typeinfo> 
using namespace std; 
 
class BaseClass { 
  int a, b; 
  virtual void f() {}; // make BaseClass polymorphic 
}; 
 
class Derived1: public BaseClass { 
  int i, j; 
}; 
 
class Derived2: public BaseClass { 
  int k; 
}; 
 
int main() 
{ 
  int i; 
  BaseClass *p, baseob; 
  Derived1 ob1; 
  Derived2 ob2; 
 
  // First, display type name of a built in type. 
  cout << "Typeid of i is "; 
  cout << typeid(i).name() << endl; 
   
  // Demonstrate typeid with polymorphic types. 
  p = &baseob; 
  cout << "p is pointing to an object of type "; 
  cout << typeid(*p).name() << endl; 
 
  p = &ob1; 
  cout << "p is pointing to an object of type "; 
  cout << typeid(*p).name() << endl; 
 
  p = &ob2; 
  cout << "p is pointing to an object of type "; 
  cout << typeid(*p).name() << endl; 
 
  return 0; 
}

listing 28
#include <iostream> 
using namespace std; 
 
#define NUM_EMPLOYEES 4 
 
class employee { 
public: 
  employee() { cout << "Constructing employee\n"; } 
  virtual void print() = 0; 
}; 
 
class programmer : public employee { 
public: 
  programmer() { cout << "Constructing programmer\n"; } 
  void print() { cout << "Printing programmer object\n"; } 
}; 
 
class salesperson : public employee { 
public: 
  salesperson() { cout << "Constructing salesperson\n"; } 
  void print() { cout << "Printing salesperson object\n"; } 
}; 
 
class executive : public employee { 
public: 
  executive() { cout << "Constructing executive\n"; } 
  void print() { cout << "Printing executive object\n"; } 
}; 
 
int main() { 
  programmer prog1, prog2; 
  executive ex; 
  salesperson sp; 
 
  // Initialize the array of employees 
  employee *e[NUM_EMPLOYEES]; 
  e[0] = &prog1; 
  e[1] = &sp; 
  e[2] = &ex; 
  e[3] = &prog2; 
 
  // See which ones are programmers. 
  for(int i = 0; i < NUM_EMPLOYEES; i++) { 
    programmer *pp = dynamic_cast<programmer*>(e[i]); 
    if(pp) { 
      cout << "Is a programmer\n"; 
      pp->print(); 
    } 
    else { 
      cout << "Not a programmer\n"; 
    } 
  } 
}

listing 29
// An example that uses reinterpret_cast. 
#include <iostream> 
using namespace std; 
 
int main() 
{ 
  int i; 
  char *p = "This is a string"; 
 
  i = reinterpret_cast<int> (p); // cast pointer to integer 
 
  cout << i; 
 
  return 0; 
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -