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

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