OOP Q&A
Question 12
9/19/2004
mr Steil, i just have a question, i know it
is so simple that it might be ignored but i guess it i better to ask anyway, i thought
NULL and '\0' were the same, after researching a bit i
found out that NULL is actually 0 but i don't
completely understand when should NULL be used.
is '\0' only when it
refers to the null terminator in strings?
thanks in advance.
Answer:
NULL is always used to represent the address
0. That is the address that you should make
a pointer point to when it is really not pointing to memory you can use.
‘\0’ is called the null terminator. It is the special character that marks the
end of a c style string. It also happens
to have the ASCII value 0. It should not
be used as an address in memory.
Question 11
I had a point deducted for using 'break'
in the following code. The explanation
was that this is not structured. In this
case, removing it would neither affect the result nor cause any major
efficiency problems, but there are certainly situations in which it would be
much better to exit the loop instead of wasting time on the remaining
elements. The only other solution I
could think of is to set Character to (m_Value.length()) to guarantee the condition would be false. That doesn't seem any more structured though.
What do you suggest I do?
for(unsigned int Character = 0; Character < m_Value.length(); Character++)
{
//if this character is not a number
if(m_Value[Character] < 48 || m_Value[Character] > 57)
{
//if this character is not a dash in the first position
if(Character != 0 || m_Value[Character] != '-')
{
IsANumber = false;
break;
}
}
}
Answer:
I agree with the grader. Using a brake to jump out of any loop or
function is unstructured and unadvisable.
Just change the choice part of
the "for loop" as follows:
for(unsigned int Character = 0;
Character < m_Value.length() && IsANumber;
Character++)
{
//if this character is not a number
if(m_Value[Character] < 48 || m_Value[Character] > 57)
{
//if this character is not a dash in the first position
if(Character != 0 || m_Value[Character] != '-')
{
IsANumber = false;
}
}
}
Question 10
Why would you use a const_cast?
Question 9 Applies to Assignment 1 CString Class
In the conversion constructor, am I essentially taking a char* (or a char array, too, right?) and copying its value into the member variable?
Answer:
Yes.
But be sure the char* passed in is not NULL.
If it is just set the m_pString to NULL.
Question 8
Are classes or objects instantiated?
Answer:
Objects
are instantiated. An object is an
instance or instantiation of a class.
Question 7 Applies to Assignment 1 CString Class
In the copy/conversion constructors, when I debug it says m_pString is a bad pointer. Should I set it to NULL before attempting to allocate memory for it and storing information in it?
Answer:
It
is not necessary to set the pointer to NULL if you are going to be setting it
to some new memory directly afterward.
On the other hand in both the copy and conversion constructors the new
string may itself be NULL. In that case
you wish you had just set the m_pString to NULL in
the first place. Also, if you plan on
calling any of your functions from the constructors, m_pString
needs to be set to NULL first. Otherwise
it is just pointing to a random location.
Question 6 Applies to Assignment 1 CString Class
I was also wondering if there is a more efficient way of performing the following code for my Append function:
Answer:
I would replace the code:
m_pString = new char[size];
strcpy(m_pString, temp);
delete []temp;
with:
m_pString = temp;
Question 5
What type is size_t anyway, if not int? (couldn't find this in the help)
Answer:
size_t is defined as a unsigned integer. It is declared in STDDEF.H and other include files.
On
a side note, size_type is defined the same way.
Question 4 Applies to Assignment 1 CString Class
It only compiles if the copy constructor is declared non-const.
Otherwise it gives me the following error message:
"error C2662: 'CString::Value': cannot convert 'this' pointer from 'const CString' to 'CString &'"
Here is the relevant code:
//this works fine without the const
CString::CString(const CString&
other)
{
//I intend to fix this
m_pString = new char[10];
m_pString[0]
= '\0';
//This is where the error is supposedly coming from
Set(other.Value());
}
void CString::Set(const
char* const NewString)
{
Delete();
size_t length = strlen(NewString);
m_pString = new char[length+10];
strncpy(m_pString, NewString, length+1);
}
const char* const CString::Value()
{
return m_pString;
}
Any idea what is causing this odd error?
Answer:
The
error message is telling you that you can not call a function that may change a
member variable for an object that has been declared constant. In this case the parameter other in the copy
constructor is constant. You are not
allowed to call the Value member function using this object because it may
change some of its member variables. Fix
this problem by adding the word const to the end of the Value function. We will discuss this more in class.
Question 3 Applies to Assignment 1 CString Class
Why does CString::Value need to return a *const pointer? it seems (more
than) reasonable to return a const char*, but I don't see how it is
useful to restrict what the receiver does with the pointer he's holding.
Answer:
In
this case you must restrict what the receiver does with the pointer he's
holding because he is pointing at your data.
Typically this sort of function is used only to copy values or to pass
them to other functions. In OOP the
concepts of data-hiding and encapsulation encourage programmers to restrict the
modification of member variables to the member functions themselves.
Question 2
Why do you want us to use Hungarian notation? (CClass, m_member_variable, ppointer, etc)
Answer:
Some
naming conventions need to be used for documentation purposes. You programs will be much easier to read and
maintain if you stick to some standard.
I am enforcing my own set of standards for multiple reasons. First,
students need to become accustom to conforming to the standards of their
employer/teacher. Second, when all
students in my classes conform to the same standard it makes grading much
easier. Third, students need to become
accustom to using a standard, at this point most students have not. They need
to start now and get in the habit of producing cleaner and more manageable
code. Lastly, it is both beneficial and
comforting to most students if they are given a clear set of coding styles to
conform to.
Also,
for clarification I am not asking you my students to use Hungarian notation
exactly.
Question 1
I'm getting a warning because it's casting size_t to int. Will you
count off for this or will I have to write code to cast it?
Answer:
Yes
you need to type cast the size_t to an int to remove the compiler warning. And yes, I will "count off" for
such compiler warnings. In general it is
a good idea to get rid of all compiler warnings even if you think they will not
cause any runtime problems. It is easier
to just get rid of the compiler warnings than to read them each time you
compile to see if it is one you need to worry about. When you are working on a programming team a
team mate of your should not have to read through a list of compiler warnings
you created to see if they need to fix them.
It is a big time saver to just fix them the first time you see them.