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

  1. /* CRAIGTAT.CPP:
  2.  
  3.          When declaring an instance of a class with the intention of
  4.          invoking the default constructor the syntax is:
  5.  
  6.                          MyClass Object; not MyClass Object();
  7. */
  8.  
  9. #include <iostream.h>
  10. #include <string.h>
  11.  
  12. class MyClass {
  13.         char *myString;
  14.     public:
  15.         MyClass( char* dfault="default string") {
  16.             myString = strdup( dfault );
  17.         }
  18.         ~MyClass(){ delete myString; };
  19.         void Display(){ cout<<myString<<endl;};
  20. };
  21.  
  22. //*******************************************************************
  23. int main(void)
  24. {
  25.     MyClass Obj1;              // default constructor is called
  26.  
  27.     /* Here we are NOT invoking the constructor that takes no
  28.          arguments instead we are declaring a function 'Obj2' that
  29.          takes no arguments and returns an object of type 'MyCass'.
  30.          We will get no error message here---only later when we try
  31.          to use it as a class. */
  32.     MyClass Obj2();
  33.  
  34.     Obj1.Display();         // Works like a champ!
  35.  
  36.     // "Error:Left side must be a structure in function main()"
  37.     Obj2.Display();
  38.  
  39.     return 0;
  40. } // end of main()
  41.