home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / diverses / tctnt / c.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-27  |  1.9 KB  |  63 lines

  1. /* C.C: Member Data Initialization Using Constructor Initialization List
  2.  
  3.      The following code demonstrates the initialization of a class's
  4.      member data by using the initialization list of a class constructor.
  5.      It also shows how to call a base class constructor from a derived
  6.      class.
  7.  
  8.      Since this code uses floating point, be sure to have floating
  9.      point emulations enabled (IDE) or include EMU.LIB and MATHx.LIB at
  10.      the appropriate location in the TLINK command line.
  11. */
  12.  
  13. #include <iostream.h>
  14.  
  15. #define HUND 100
  16.  
  17. class base {
  18.     public:
  19.         base() { cout << "in base\n"; }
  20. };
  21.  
  22. class derive : public base {
  23.         int x, z;
  24.         float y;
  25.  
  26.     public:
  27.         /* Note that the initialization list comes AFTER the contructors
  28.              formal arguments and a colon. Each variable name is followed
  29.              by an initial value in parenthese. Each variable is separated
  30.              from the next by a comma.  The value in the parenthese can
  31.              be either a variable from the constructor parameter list,
  32.              a value, an expression that results in a value, or a constant.
  33.  
  34.              At the end of the initialization list is the call to the base
  35.              class constructor.  In this example, parameters are not passed,
  36.              however, parameters may be passed and they may consist of a
  37.              variable from the derived constructor parameter list, a value,
  38.              an expression that results in a value, or a constant.
  39.  
  40.              A private or public member of the base class cannot be
  41.              initialized by the initialization list of the derived
  42.              constructor. */
  43.  
  44.         //initialization list...Call to base constructor
  45.         derive(int a) : x(a*a), z(HUND), y(4.33), base() {
  46.             cout << "in derive\n";
  47.         }
  48.  
  49.         void show(void) {
  50.             cout << "x is " << x;
  51.             cout << "\nz is " << z;
  52.             cout << "\ny is " << y;
  53.         }
  54. };
  55.  
  56. //*******************************************************************
  57. int main()
  58. {
  59.     derive dd(3);
  60.     dd.show();
  61.     return 0;
  62. } // end of main()
  63.