The Database Managers, Inc.

Contact The Database Managers, Inc.


Use an RSS enabled news reader to read these articles.Use an RSS enabled news reader to read these articles.

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.

#include <iostream>

using namespace std;

class Vehicle {
public:
  virtual string getFuel() = 0;
};

class Car : public Vehicle {
public:
  virtual string getFuel()
    { return "gasoline"; }
};

class Truck : public Vehicle {
public:
  virtual string getFuel()
    { return "diesel"; }
};

void printFuel(Vehicle &v) {
  cout << v.getFuel() << endl;
}

int main(int, char **) {
  Car car;
  Truck truck;
  printFuel(car);
  printFuel(truck);
  return 0;
}
Listing A: Example of Polymorphism

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.

#include <algorithm>
#include <function>
#include <assert>
#include <iostream>

using namespace std;

// Use the absolute value (magnitude) of the
// number.  For example:  3 < -5 because abs(3) < abs(-5)
struct magnitude {
  bool operator()(const int& x, const int& y)
    const
    { return abs(x) < abs(y); }
};


int main(int, char **) {
  int a[10];
  int i;

  for(i = 0; i < 10; ++i)
    a[i] = i * (i % 2 ? -1 : 1);
  random_shuffle(&a[0], &a[10]);

  // sort into special order:
  sort(&a[0], &a[10], magnitude());

  // Should be: 0,-1,2,-3,4,-5,6,-7,8,-9,
  for(i = 0; i < 10; ++i)
    cout << a[i] << ",";
  cout << endl;

  return 0;
}
Listing B: Functor Example


#include <algorithm>
#include <iostream>

using namespace std;

// Used to allow for generic implementations of bubble_sort
struct SortOrder {
  virtual bool isGreaterThan(const int x, const int y) const = 0;
};

void bubble_sort(int *first, int *last, SortOrder &compare) {
  int numLaps = last - first - 1;
  for(int lap = 0; lap < numLaps; ++lap) {
    for(int *current = first; current != last; ++current) {
      int *next = current + 1;
      if (next == last)
        continue;
      if (compare.isGreaterThan(*current, *next)) {
        std::swap(*current, *next);
      }
    }
  }
}


// Use the absolute value (magnitude) of the
// number.  For example:  3 < -5 because abs(3) < abs(-5)
struct Magnitude : SortOrder {
  virtual bool isGreaterThan(const int x, const int y) const
    { return abs(x) > abs(y); }
};


int main(int, char **) {
  int a[10];
  int i;

  for(i = 0; i < 10; ++i)
    a[i] = i * (i % 2 ? -1 : 1);
  random_shuffle(&a[0], &a[10]);

  // sort into special order:
  Magnitude sort_by_magnitude;
  bubble_sort(&a[0], &a[10], sort_by_magnitude);

  // Should be: 0,-1,2,-3,4,-5,6,-7,8,-9,
  for(i = 0; i < 10; ++i)
    cout << a[i] << ",";
  cout << endl;

  return 0;
}
Listing C: Another Functor Example


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.

Previous Answers More C++ Answers
Jump to Questions Page:  1  2  3  4  5  6  7  8  9  10  11  12  13 
 
Jump to Answers Page:  1  2  3  4  5  6  7  8  9  10  11  12  13 
Services | Programming | Contact Us | Recent Updates
Send feedback to: