home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c100 / 1.ddi / OOPWLD.ZIP / DIRECT / EXMPL1.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1990-05-08  |  972 b   |  32 lines

  1. // C++ comments go to the end of the line
  2. /* C-style comments still work */
  3.  
  4. class typename {
  5.   int internal_data;  // private
  6. public: // accessible to the outside world
  7.   typename() { // constructor has same name as class
  8.     internal_data = 0;  // perform initialization in constructor
  9.   }
  10.   ~typename() { // destructor is class name plus a tilde
  11.     // perform cleanup in destructor
  12.   }
  13.   void external_operation() { internal_data++; }
  14.   int access_function() { return internal_data;}
  15.   // The above are "inline" functions.  
  16.   // Here's a non-inline declaration:
  17.   void set(int x);
  18. };  // end of class must have a semicolon!
  19.  
  20. // here's the non-inline definition:
  21. void typename::set(int x) {
  22.   internal_data = x;
  23. }
  24.  
  25. // here's how we use the class: 
  26. main() {
  27.   typename X;  // just like defining a variable of a built-in type
  28.   X.external_operation();  // just like accessing a struct member
  29.   int a = X.access_function();
  30.   X.set(100);
  31. }
  32.