home *** CD-ROM | disk | FTP | other *** search
/ C++ for Dummies (3rd Edition) / C_FD.iso / CHAP14 / CHAP14_3.CPP < prev    next >
C/C++ Source or Header  |  1996-09-02  |  721b  |  34 lines

  1. // Chap14_3.cpp
  2. // this version doesn't work because of lack of a copy constructor
  3. #include <iostream.h>
  4. #include <string.h>
  5. class Person
  6. {
  7.   public:
  8.    Person(char *pN)
  9.    {
  10.       cout << "Constructing " << pN << "\n";
  11.       pName = new char[strlen(pN) + 1];
  12.       if (pName != 0)
  13.       {
  14.           strcpy(pName, pN);
  15.       }
  16.    }
  17.   ~Person()
  18.    {
  19.       cout << "Destructing " << pName << "\n";
  20.       //letÆs wipe out the name just for the heck of it
  21.       pName[0] = '\0';
  22.       delete pName;
  23.    }
  24.   protected:
  25.    char *pName;
  26. };
  27.  
  28. int main()
  29. {
  30.    Person p1("Randy");
  31.    Person p2 = p1;       //invoke the copy constructor...
  32.    return 0;             //...equivalent to Person p2(p1);
  33. }
  34.