home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 10 / 10.iso / l / l350 / 3.ddi / EXAMPLES / MSC / SIMPLE.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1993-02-17  |  1.5 KB  |  84 lines

  1. //
  2. // SIMPLE.CPP - Simple C++ program that shows class constructors and
  3. //        destructors executing.
  4. //
  5. #include <iostream.h>
  6. #include <string.h>
  7.  
  8. class a
  9. {
  10.     static a *pChain;
  11.     static int num;
  12.     a *pNext;
  13.     char *pName;
  14.     
  15. public:
  16.  
  17.     a()
  18.     {
  19.         cout << "Hi from default a::a()\n";
  20.         
  21.         pName = new char[7];
  22.         strcpy(pName, "NoName0");
  23.         pName[6] += num ++;
  24.         
  25.         pNext = pChain;
  26.         pChain = this;
  27.         
  28.         cout << "Created instance: " << pName << "\n";
  29.     }
  30.     
  31.     a(char *s)
  32.     {
  33.         cout << "Hi from a::a(char *s)\n";
  34.         
  35.         pName = new char[strlen(s) + 1];
  36.         strcpy(pName, s);
  37.         
  38.         cout << "Created instance: " << pName << "\n";
  39.         
  40.         pNext = pChain;
  41.         pChain = this;
  42.     }
  43.     
  44.     ~a()
  45.     {
  46.         int i = 0;
  47.         a *p;
  48.         
  49.         /* walk chain */
  50.         
  51.         for (p = pChain; p ; p = p -> pNext , i++)
  52.         {
  53.             cout << i << ": Instance name = " << p -> pName << "\n";
  54.         }
  55.         
  56.         pChain = pChain -> pNext;
  57.                 
  58.         cout << "There are " << i << " instances left.\n";
  59.         
  60.         delete[] pName;
  61.     }
  62. };
  63.  
  64. a* a::pChain = NULL;
  65. int a::num = 0;
  66.  
  67. class b : public a
  68. {
  69. public:
  70.     b() { cout << "Hi there from default b::b()!\n"; }
  71.     b(char *s) : a(s) { cout << "Hi there from b::b(char *s)!\n"; }
  72.     ~b() { cout << "Bye from b::~b()!\n"; }
  73. };
  74.  
  75. void main()
  76. {
  77.     a i1;            // use default constructor
  78.     b i2;            // use default constructor
  79.     a j1 = "My a string";    // use a::a(char *) constructor
  80.     b j2 = "My b string";    // use b::b(char *) constructor
  81.     
  82.     cout << "***\n*** Exiting from main() ***\n***\n";
  83. }
  84.