home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C++ for Dummies (3rd Edition)
/
C_FD.iso
/
WORKOUT
/
PART4.TXT
< prev
next >
Wrap
Text File
|
1996-09-06
|
5KB
|
120 lines
25-Minute Workout
Part IV Answers
Hey, go back and try working the problems first!
1
a. HereÆs the constructor for Student with no limitations on name length:
#include <string.h>
class Student
{
public:
Student()
{
//just zero out everything
noCourses = 0;
pName = 0;
}
Student(char *pN)
{
noCourses = 0;
int length = strlen(pN) + 1;
pName = new char[length];
if (pName)
{
strcpy(pName, pN);
}
}
~Student()
{
delete pName;
}
//...other member functions the same...
protected:
char *pName;
Course *pClasses[maxCourses];
int noCourses;
};
This constructor first calls strlen to count the number of characters in the name
provided. It then allocates that much memory from the heap. If the memory request is
granted, it copies the name into the heap block. (DonÆt forget to check to make sure
that the memory pointer returned from a heap request is valid before using it! ) Note
that the class Student now needs a destructor to return the heap block when the object
ôgoes away.ö
b. The point of this question is to get you to construct the member motor correctly.
(Should the constructor for Car be called Detroit?)
Car::Car(int color, float displacement,
int doors, int noCylinders) :
motor(noCylinders, displacement)
{
paintColor = color;
numberDoors = doors;
}
c. HereÆs the play-by-play:
student[0] Student(ôRandyö)
student[1] Student(otherStudent)
student[2] Student(ôTrudyö)
student[3] Student()
student[4] Student()
d. This was probably an attempt to consolidate code by having the default constructor
call the char* constructor with ôno nameö as the name. This does not work, however.
It constructs locally a nameless object with the name ôno name.ö (ThatÆs a fitting
name for a nameless object, donÆt you think?) This object is destructed on the next
line, the closed brace.
e. The programmer could have created the desired effect in several ways. One way is to
default the name field as follows:
class Student
{
public:
Student(char *pName = ôno nameö);
};
Another approach is to build a separate protected function that both constructors can
call:
class Student
{
public:
Student(char *pName)
{
init(pName);
}
Student()
{
init(ôno nameö);
}
protected:
void init(char *pName);
};
This is the most flexible solution because it works for any combination of argument
types.
2
a. Suppose this constructor was not illegal and someone defined it. Now suppose what
would happen if someone tried to use one of these beasties. For example:
void fn(Student &original)
{
Student copy(original);
}
C++ needs to make a copy of original. Finding the preceding constructor, it says
ôAha, I can use this constructor to make a copy of original, so IÆll call it.ö ôBut wait,ö
the compiler says, ôfirst IÆll need to make a copy of the argument to the constructor
because itÆs passed by value.ö
So the compiler starts looking for a constructor for that purpose. Does it find it? Of
course; it finds Student(Student). ôAha,ö it says, ôI can use this constructor to make a
copy of the copy of original, so IÆll call it.ö ôBut wait,ö the compiler says, ôfirst IÆll
need to make a copy of the argument to the constructor because itÆs passed by value.ö
So it finds Student(Student), and so on ad computer nauseam. This would be an
infinite loop if it werenÆt for the fact that it eventually blows up the stack. To avoid
this silliness, the designers of C++ just decided to make it illegal.
b. The copy constructor in BUDGET4 was declared protected to keep the application
from making copies of any accounts. If an account object were copied, this would
copy the balance, effectively creating cash. (Did anybody say embezzlement?)
Protecting the copy constructor precludes this from happening.
3
a. Because count is a static member of Student, the subexpression pS++ in the
expression pS++->count is never evaluated. (Only the class of subexpression ps++
is important.) Thus, pS never changes and the loop is infinite.
b. s is 400 bytes or 402 bytes, depending on how you look at it. That is, each element of
s is 40 chars, which is 40 bytes, and there are 10 of them, plus the single static int
count for the entire class Student. The point is that itÆs not 420 because the member
count appears only once ù not 10 times.