C++ Language Definition and Acronym Answers
by Curtis Krauskopf
The key to answering a definition-type question is
to keep the answer short. Even though you might be an
expert on a particular word or phrase, the interviewer
is generally not interested in that and they just want
a simple explanation that shows you understand what
it means.
Some interviewers adopt a more sadistic perspective.
Their philosophy is follows the idiom "Give a man enough
rope and he'll hang himself". The goal of the sadistic
interviewer is to allow you to talk until you've said
something that is undeniably incorrect and then point
it out to you. Short answers prevent you from rambling
on about a topic and then getting yourself into trouble
by giving an inaccurate or misleading description.
One way to tell the difference between a language issue
and a definition question is to examine the question.
A definition-type answer is expected for questions similar
to "What is X?". A language-issue type answer is expected
for questions that ask how to do something, or why is
something defined the way it is.
1) Polymorphism is the
ability of a pointer to a derived object to be type-compatible
with a pointer to its base class. If the interviewer's
follow-up question is, "Huh?", you can answer:
Polymorphism allows two or more classes derived from
the same base class to share a common base member function
but have different behaviors. Moving to a whiteboard,
you can whip up the sample program in Listing
A. It uses a pure virtual base member but that's
not a requirement. A regular virtual base member would
also have been acceptable.
In Listing A, polymorphism
occurs with the Vehicle parameter for the printFuel()
function. printFuel() can
accept either an instantiation of Car or an instantiation
of Truck.
A follow-up question might ask if a Vehicle object
could be passed to the printFuel()
function. The answer is "no" because Vehicle uses a
pure-virtual function and classes with pure-virtual
functions can not be instantiated.
2) virtual is a C++ keyword
that is used for virtual methods and for virtual base
classes.
3) mutable is a storage-class
specifier. It allows const-member functions to modify
the data member.
4) explicit is used on
constructors that have one parameter. It prevents automatic
type conversion from changing a candidate parameter
into the type used in the constructor.
5) template metaprogramming
is an idiom that uses templates to generate source code
that calculates an answer at compile-time rather than
at run-time.
Follow-up questions would ask about the Curiously Recurring
Template Pattern, the Barton-Nackman trick, static polymorphism
in template metaprogramming and the benefits and drawbacks
of template metaprogramming. See wikipedia.org
for a good explanation along with cross reference material.
6) public, private
and protected are the access
control specifiers in class and struct designs. All
three of the keywords are used to control the access
to methods and data in base classes and in derived classes.
Listing A shows an example
of a Car class defining public access to a Vehicle class.
The Vehicle class defines public access of the getFuel()
pure virtual method.
7) The static keyword
is used all over the place in the C++ language. When
used inside of a method or function, the static keyword
preserves the last value of a variable between method
or function calls. Inside of a class definition, a data
value can be declared static -- this causes one version
of the data to be shared amongst all of the objects
of the class. Static member functions can not be virtual
because they have external linkage. External linkage
means that the function does not have a this pointer
and the function can only call other static member functions
and access static data.
8) An assignment operator
is a simple equal (=) sign for built-in types or the
operator=() method for
objects. If your first answer only provided one of those
assignment operators, a good interviewer would ask something
like "is that all?". Written tests can't do that, of
course, so be careful when giving what seems like an
obvious answer.
9) A dangling pointer
can be an unassigned pointer or it can be a pointer
to an object that has been destroyed.
10) A functor is short
for function object. Function objects are used as callbacks
to modify or customize the behavior of an algorithm.
Function objects define a member function that provides
the glue between an algorithm and the customized behavior.
Because functors are full-fledged objects, they have
all of the power of an object: state, inheritance, encapsulation
and templates. In Listing B,
the operator() method
of the magnitude structure is a functor. Listing
C takes a slightly different approach by using an
arbitrary method name, in this case called "isGreaterThan",
to attach a customized behavior to an algorithm.
Acronyms
Just like definitions, try to keep the answers to acronyms
short.
1) STL: Standard Template Library
2) RAII: Resource Allocation is Initialization.
See http://www.hackcraft.net/raii/ for a well-written
tutorial on RAII.
3) VCL: Visual Component Library.
I'll admit that even though I knew what the VCL was,
I didn't know what its acronym stood for until I wrote
this article.
4) C++: Quote from Chapter 1 of the
C++ Programming Language (Stroustrup)
"The name C++ ... was coined by Rick
Mascitti in the summer of 1983. The name signifies the
evolutionary nature of the changes from C; "++" is the
C increment operator."
See also att.com.
5) A WChar type in a program you didn't
write would typically be a wide (16-bit) character type.
|