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

  1. // Chap14_1.cpp
  2. #include <iostream.h>
  3. #include <string.h>
  4. class Student
  5. {
  6.   public:
  7.    //conventional constructor
  8.    Student(char *pName  = "no name", int ssId = 0)
  9.    {
  10.       cout << "Constructing new student " << pName << "\n";
  11.       strncpy(name, pName, sizeof(name));
  12.       name[sizeof(name) - 1]  = '\0';
  13.       id = ssId;
  14.    }
  15.  
  16.    //copy constructor
  17.    Student(Student &s)
  18.    {
  19.       cout << "Constructing Copy of " << s.name << "\n";
  20.       strcpy(name, "Copy of ");
  21.       strcat(name, s.name);
  22.       id = s.id;
  23.    }
  24.   ~Student()
  25.    {
  26.       cout << "Destructing " << name << "\n";
  27.    }
  28.   protected:
  29.    char  name[40];
  30.    int   id;
  31. };
  32.  
  33. //fn - receives its argument by value
  34. void fn(Student s)
  35. {
  36.    cout << "In function fn()\n";
  37. }
  38.  
  39. int main()
  40. {
  41.    Student randy("Randy", 1234);
  42.    cout << "Calling fn()\n";
  43.    fn(randy);
  44.    cout << "Returned from fn()\n";
  45.    return 0;
  46. }
  47.