home *** CD-ROM | disk | FTP | other *** search
/ C++ for Dummies (3rd Edition) / C_FD.iso / WORKOUT / PART4.TXT < prev    next >
Text File  |  1996-09-06  |  5KB  |  120 lines

  1. 25-Minute Workout
  2. Part IV Answers    
  3. Hey, go back and try working the problems first!
  4. 1
  5.     a.    HereÆs the constructor for Student with no limitations on name length:
  6. #include <string.h>
  7. class Student
  8. {
  9.   public:
  10.    Student()
  11.    {
  12.       //just zero out everything
  13.       noCourses = 0;
  14.       pName = 0;
  15.    }
  16.    Student(char *pN)
  17.    {
  18.       noCourses = 0;
  19.       int length = strlen(pN) + 1;
  20.       pName = new char[length];
  21.       if (pName)
  22.       {
  23.          strcpy(pName, pN);
  24.       }
  25.    }
  26.   ~Student()
  27.    {
  28.       delete pName;
  29.    }
  30.    //...other member functions the same...
  31.   protected:
  32.    char   *pName;
  33.    Course *pClasses[maxCourses];
  34.    int     noCourses;
  35. };
  36.  
  37.         This constructor first calls strlen to count the number of characters in the name 
  38. provided. It then allocates that much memory from the heap. If the memory request is 
  39. granted, it copies the name into the heap block. (DonÆt forget to check to make sure 
  40. that the memory pointer returned from a heap request is valid before using it! ) Note 
  41. that the class Student now needs a destructor to return the heap block when the object 
  42. ôgoes away.ö
  43.     b.    The point of this question is to get you to construct the member motor correctly. 
  44. (Should the constructor for Car be called Detroit?)
  45. Car::Car(int color, float displacement,
  46.        int doors, int noCylinders) :
  47.        motor(noCylinders, displacement)
  48. {
  49.    paintColor = color;
  50.    numberDoors = doors;
  51. }
  52.     c.    HereÆs the play-by-play:
  53. student[0]    Student(ôRandyö)
  54. student[1]    Student(otherStudent)
  55. student[2]    Student(ôTrudyö)
  56. student[3]    Student()
  57. student[4]    Student()
  58.     d.    This was probably an attempt to consolidate code by having the default constructor 
  59. call the char* constructor with ôno nameö as the name. This does not work, however. 
  60. It constructs locally a nameless object with the name ôno name.ö (ThatÆs a fitting 
  61. name for a nameless object, donÆt you think?) This object is destructed on the next 
  62. line, the closed brace.
  63.     e.    The programmer could have created the desired effect in several ways. One way is to 
  64. default the name field as follows:
  65.     class Student
  66.     {
  67.         public:
  68.        Student(char *pName = ôno nameö);
  69.     };
  70.         Another approach is to build a separate protected function that both constructors can 
  71. call:
  72. class Student
  73. {
  74.    public:
  75.      Student(char *pName)
  76.      {
  77.         init(pName);
  78.       }
  79.       Student()
  80.       {
  81.         init(ôno nameö);
  82.       }
  83.     protected:
  84.       void init(char *pName);
  85. };
  86.         This is the most flexible solution because it works for any combination of argument 
  87. types.
  88. 2
  89.     a.    Suppose this constructor was not illegal and someone defined it. Now suppose what 
  90. would happen if someone tried to use one of these beasties. For example:
  91. void fn(Student &original)
  92. {
  93.    Student copy(original);
  94. }
  95.         C++ needs to make a copy of original. Finding the preceding constructor, it says 
  96. ôAha, I can use this constructor to make a copy of original, so IÆll call it.ö ôBut wait,ö 
  97. the compiler says, ôfirst IÆll need to make a copy of the argument to the constructor 
  98. because itÆs passed by value.ö
  99.         So the compiler starts looking for a constructor for that purpose. Does it find it? Of 
  100. course; it finds Student(Student). ôAha,ö it says, ôI can use this constructor to make a 
  101. copy of the copy of original, so IÆll call it.ö ôBut wait,ö the compiler says, ôfirst IÆll 
  102. need to make a copy of the argument to the constructor because itÆs passed by value.ö 
  103. So it finds Student(Student), and so on ad computer nauseam. This would be an 
  104. infinite loop if it werenÆt for the fact that it eventually blows up the stack. To avoid 
  105. this silliness, the designers of C++ just decided to make it illegal.
  106.     b.    The copy constructor in BUDGET4 was declared protected to keep the application 
  107. from making copies of any accounts. If an account object were copied, this would 
  108. copy the balance, effectively creating cash. (Did anybody say embezzlement?) 
  109. Protecting the copy constructor precludes this from happening.
  110.  
  111. 3
  112.     a.    Because count is a static member of Student, the subexpression pS++ in the 
  113. expression pS++->count   is never evaluated. (Only the class of subexpression ps++ 
  114. is important.) Thus, pS never changes and the loop is infinite.
  115.     b.    s is 400 bytes or 402 bytes, depending on how you look at it. That is, each element of 
  116. s is 40 chars, which is 40 bytes, and there are 10 of them, plus the single static int 
  117. count for the entire class Student. The point is that itÆs not 420 because the member 
  118. count appears only once ù not 10 times.
  119.  
  120.