Friday, March 5, 2010

Common C++ Interview Questions

What are the differences between a C++ struct and C++ class?
The default member and base class access specifiers are different.
The C++ struct has all the features of the class. The only differences are that a struct defaults to public member access and public base class inheritance, and a class defaults to the private access specifier and private base class inheritance.

What is overloading?
With the C++ language, you can overload functions and operators. Overloading is the practice of supplying more than one definition for a given function name in the same scope.
- Any two functions in a set of overloaded functions must have different argument lists.
- Overloading functions with argument lists of the same types, based on return type alone, is an error.

What is overriding?
To override a method, a subclass of the class that originally declared the method must declare a method with the same name, return type (or a subclass of that return type), and same parameter list.
The definition of the method overriding is:
· Must have same method name.
· Must have same data type.
· Must have same argument list.

How can I handle a constructor that fails?
Throw an exception. Constructors don't have a return type, so it's not possible to use return codes. The best way to signal constructor failure is therefore to throw an exception.

When should my destructor be virtual? 
When someone will delete a derived-class object via a base-class pointer.

Is prefix increment (e.g. ++i) faster than postfix (i++)? Why? 
Prefix increment is guaranteed to be at least as fast as postfix and never slower. Postfix returns the value before incrementing and therefore when the result is used, it will need to store / copy the value before carrying out the incrementation.

No comments:

Post a Comment