home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgLangD.iso / C++-7 / DISK4 / SAMPLES / CPPTUTOR / DEMO.CP$ / DEMO
Encoding:
Text File  |  1991-12-11  |  951 b   |  50 lines

  1. // DEMO.CPP
  2.  
  3. // This is an example class from Chapter 4 of the C++ Tutorial. This
  4. //     class shows when constructors and destructors are called for
  5. //     global, local, and static local objects.
  6.  
  7. #include <iostream.h>
  8. #include <string.h>
  9.  
  10. class Demo
  11. {
  12. public:
  13.     Demo( const char *nm );
  14.     ~Demo();
  15. private:
  16.     char name[20];
  17. };
  18.  
  19. Demo::Demo( const char *nm )
  20. {
  21.     strncpy( name, nm, 20 );
  22.     name[19] = '\0';
  23.     cout << "Constructor called for " << name << '\n';
  24. }
  25.  
  26. Demo::~Demo()
  27. {
  28.     cout << "Destructor called for " << name << '\n';
  29. }
  30.  
  31. void func()
  32. {
  33.     Demo localFuncObject( "localFuncObject" );
  34.     static Demo staticObject( "staticObject" );
  35.  
  36.     cout << "Inside func" << endl;
  37. }
  38.  
  39. Demo globalObject( "globalObject" );
  40.  
  41. void main()
  42. {
  43.     Demo localMainObject( "localMainObject" );
  44.  
  45.     cout << "In main, before calling func\n";
  46.     func();
  47.     cout << "In main, after calling func\n";
  48. }
  49.  
  50.