RSS Feed for C/C++ Interview QuestionsCategory: C/C++ Interview Questions

What are the access privileges in C++? What is the default access level? »

The access privileges in C++ are private, public and protected. The default access level assigned to members of a class is private. Private members of a class are accessible only within the class and by friends of the class. Protected members are accessible by the class itself and it’s sub-classes. Public members of a class [...]

What is multiple inheritance(virtual inheritance)? What are its advantages and disadvantages? »

Multiple Inheritance is the process whereby a child can be derived from more than one parent class. The advantage of multiple inheritance is that it allows a class to inherit the functionality of more than one base class thus allowing for modeling of complex relationships. The disadvantage of multiple inheritance is that it can lead [...]

How do you access the static member of a class? »

::

What does extern “C” int func(int *, Foo) accomplish? »

It will turn off “name mangling” for func so that one can link to code compiled by a C compiler.

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.

Can you think of a situation where your program would crash without reaching the breakpoint which you set at the beginning of main()? »

C++ allows for dynamic initialization of global variables before main() is invoked. It is
possible that initialization of global will invoke some function. If this function crashes
the crash will occur before main() is entered.
Name two cases where you MUST use initialization list as opposed to assignment in
constructors.
Both non-static const data members and reference data members cannot [...]

What is Virtual Destructor? »

Using virtual destructors, you can destroy objects without knowing their type - the correct
destructor for the object is invoked using the virtual function mechanism. Note that
destructors can also be declared as pure virtual functions for abstract classes.
if someone will derive from your class, and if someone will say “new Derived”, where
“Derived” is derived from your [...]

How can I handle a destructor that fails? »

Write a message to a log-file. But do not throw an exception.
The C++ rule is that you must never throw an exception from a destructor that is being
called during the “stack unwinding” process of another exception. For example, if someone
says throw Foo(), the stack will be unwound so all the stack frames between the throw [...]