Online Book Reader

Home Category

Professional C__ - Marc Gregoire [500]

By Root 1438 0
point of view

Method overriding and virtual

The reason why destructors should be virtual

Chained constructors

The ins and outs of upcasting and downcasting

The principle of polymorphism

Pure virtual methods and abstract base classes

Multiple inheritance

Run Time Type Information (RTTI)

C++11 inherited constructors

The C++11 final keyword on classes

The C++11 override and final keywords on methods

Types of Questions

Many of the pitfalls in inheritance questions are related to getting the details right. When you are writing a base class, don’t forget to make the methods virtual. If you mark all methods virtual, be prepared to justify that decision. You should be able to explain what virtual means and how it works. Also, don’t forget the public keyword before the name of the parent class in the subclass definition (e.g., class Sub : public Super). It’s unlikely that you’ll be asked to perform nonpublic inheritance during an interview.

More challenging inheritance questions have to do with the relationship between a superclass and a subclass. Be sure you know how the different access levels work, and the difference between private and protected. Remind yourself of the phenomenon known as slicing, when certain types of casts cause a class to lose its subclass information.

CHAPTER 9: UNDERSTANDING C++ QUIRKS AND ODDITIES

Many interviewers tend to focus on the more obscure cases because that way experienced C++ programmers can demonstrate that they have conquered the unusual parts of C++. Sometimes interviewers have difficulty coming up with interesting questions and end up asking the most obscure question they can think of.

Things to Remember

References must be bound to a variable when they are declared and the binding cannot be changed.

The advantages of pass-by-reference over pass-by-value

The many uses of const

The many uses of static

The different types of casts in C++

The difference between rvalues and lvalues

How typedefs work

C++11 rvalue references

C++11 move semantics with move constructors and move assignment operators

C++11 type aliasing using the template alias feature

C++11 uniform initialization

C++11 alternative function syntax

C++11 nullptr

Types of Questions

Asking a candidate to define const and static is a classic C++ interview question. Both keywords provide a sliding scale with which an interviewer can assess an answer. For example, a fair candidate will talk about static methods and static data members. A good candidate will give good examples of static methods and static data members. A great candidate will also know about static linkage and static variables in functions.

The edge cases described in this chapter also come in find-the-bug type problems. Be on the lookout for misuse of references. For example, imagine a class that contains a reference as a data member:

class Gwenyth

{

public:

int& mCaversham;

};

Because mCaversham is a reference, it needs to be bound to a variable when the class is constructed. To do that, you’ll need to use a constructor initializer. The class could take the variable to be referenced as a parameter to the constructor:

class Gwenyth

{

public:

Gwenyth(int& i);

int& mCaversham;

};

Gwenyth::Gwenyth(int& i) : mCaversham(i)

{

}

CHAPTER 10: HANDLING ERRORS

Managers sometimes shy away from hiring recent graduates or novice programmers for vital (and high-paying) jobs because it is assumed that they don’t write production-quality code. You can prove to an interviewer that your code won’t keel over randomly by demonstrating your error-handling skills during an interview.

Things to Remember

Syntax of exceptions

Catch exceptions as const references.

For production code, hierarchies of exceptions are preferable to a few generic ones.

Throw lists in C++ are not like throw lists in Java.

Smart pointers help avoid memory leaks when exceptions are thrown.

The basics of how stack unwinding works when an exception gets thrown

How to handle errors in constructors and destructors

Why you should never use the C functions setjmp()

Return Main Page Previous Page Next Page

®Online Book Reader