Even if you know C++ syntax and have memorized STL components, you could stumble during a technical interview since some managers like to explore the boundary of a programmer’s C++ knowledge. To make sure you land on your feet, we asked Dice C++ Community Guide David Bolton to share a few standard questions, as well as some posed by these boundary explorers. Is it legal and moral for a member function to say delete this?
What three default methods are provided by the compiler if the programmer doesn’t specify them?
- What Most People Say: No, you should never tell an object to delete itself.
- What You Should Say: It’s OK, but you need to be careful. The object must have been created with new and you mustn't access any non-static members once you delete the object.
- Why You Should Say It: Deleting an object just feels wrong, Bolton says. However, it’s appropriate under some circumstances and it’s important to show that you understand the ramifications, and how and when it’s appropriate to do it.
What’s the difference between C type strings char *ptr and std::string? Can they be used together?
- What Most People Say: I’m familiar with default constructor.
- What You Should Say: The three methods are: default constructor, default copy constructor and assignment operator.
- Why You Should Say It: Naming all three default methods shows that you understand how to manage objects. By the way, you’ll get extra points for mentioning that you’re familiar with the destructor in C++03.
Should a destructor always be virtual?
- What Most People Say: I’m familiar with standard strings, but I don’t know how to convert them to a char pointer.
- What You Should Say: Char * is a pointer to a 0-terminated array of chars. An std::string is a class that is type-safe and can be initialized from a C type string. Also, an std::string can be converted to a C type string using the c_str() method.
- Why You Should Say It: If you’re only familiar with standard strings, then chances are you’ve programmed in C++ and don’t know C.
What is a pure virtual function?
- What Most People Say: Yes.
- What You Should Say: While it does no harm, your destructor should only be virtual when the class is a base class from which others inherit and has a non-virtual destructor.
- Why You Should Say It: A more complete answer shows that you understand when a destructor should be virtual. This is a big issue in C++. You can rise above the competition by acknowledging the challenge and demonstrating your expertise.
What’s an incomplete type?
- What Most People Say: I don’t know.
- What You Should Say: It's a virtual member function defined in a base class with the =0 notation. Derived classes from this base class must implement pure virtual functions.
- Why You Should Say It: Many programmers are familiar with a pure virtual function but they don’t know how to use it. You need to explain not only what a pure virtual function is, but how it’s applied.
What’s an orthogonal base class?
- What Most People Say: I don’t know.
- What You Should Say: Incomplete type is another name for an uninitialized pointer.
- Why You Should Say It: “This is not a trick question,” Bolton says. “Interviewers ask questions like these to assess your level of proficiency with C++, since inexperienced programmers usually don’t know the answer.”
Which members of the base class are visible in a derived class?
- What Most People Say: I’m not sure.
- What You Should Say: When two or more base classes do not share common methods or data -- and don’t have the same names -- they are said to be orthogonal or independent of each other. They can all be inherited into a derived class without issues.
- Why You Should Say It: This is an advanced question, explained Bolton. Explaining how two or more derived classes can be inherited shows that you’re an expert in base classes.
- What Most People Say: Members defined as public.
- What You Should Say: Members defined as public or protected.
- Why You Should Say It: Most programmers are familiar with access control in a public base class, but you should know that derived-class members can refer to public and protected members of the base class.