C++ Interview Questions

What is OOP?

OOP is a design philosophy. It stands for Object Oriented Programming. Everything in OOP is “objects”. Hence, you gain re-usability by means of four main object-oriented programming concepts.

What are the OOPS concepts?

a) Encapsulation

It is the mechanism that binds together code and data it manipulates, and keeps both safe from outside interference and misuse. The advantage of encapsulated code is that the outside world knows how to access it and thus can use it regardless of the implementation details and without fear of unexpected side-effects.

b) Inheritance

It is the process by which one object acquires the properties of another object. That means creating new classes, called derived classes from existing, called base classes. Derived classes have all the properties of the base classes including new features if its own.

c) Polymorphism

It is the ability of an object to take more than one forms. There are two different types of polymorphism, compile-time polymorphism and run-time polymorphism. Compile-time polymorphism can establish by Function overloading and Operator overloading. Run-time polymorphism can establish by Virtual functions.

d) Abstraction

It indicates what kind of data an object can hold and what kind of operations can it perform on that data. The implementations of these functions are completely abstracted away. The good example for abstraction is implementation of stack, without bothering about how its store and retrieve the data we can directly use push and pop functions.

What is the difference between classes and objects?

A class is a blueprint for objects. From one class several objects can be created. Each object is known as an instance of a particular class.
For example, int n – where int is a class and n is an object of that class. Instead of standard class like int we can consider user defined classes MyClass from which objects like obj1, obj2 can be created through a statement.
MyClass obj1, obj2;

What is constructor?

A constructor is a member function with the same name as its class. Constructors are used to create, and can initialize objects of their class type. It also creates vtable for virtual functions.

What is a default constructor?

Default constructors are constructors that either accepts no arguments or for which all arguments have a default. If you don’t provide one, the compiler provides one if there are no other constructors.

What is a conversion constructor?

A constructor that can be called with a single argument is used for conversions from the type of the argument to the class type. Such a constructor is called a conversion constructor.

When should the explicit keyword be used in the constructor?

When we want that the constructor should build the object but it should not get used for carrying out conversions. Such times we can use explicit keyword with constructor. The explicit keyword can be used only with constructors.

What is destructor?

Destructors are usually used to deallocate memory and do other cleanup for a class object and its class members when the object is destroyed. A destructor is called for a class object when that object passes out of scope or is explicitly deleted. A destructor takes no arguments and has no return type.

What is virtual destructor?

Virtual destructor is one that is declared with the keyword virtual. The behavior of a virtual destructor is what is important. If you destroy an object through a pointer or reference to a base class, and the base-class destructor is not virtual, the derived-class destructors are not executed, and the destruction might not be complete. So the base class destructor should be virtual to execute the destructors from derived to base class order.

What is virtual constructor?

There is no such concept in C++.
A constructor can not be virtual because at the time when the constructor is invoked the virtual table (vtable) would not be available in the memory.

What are the differences between a C++ struct and C++ 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 abstract class?

A generic class defines at least one pure virtual function and whose instance cannot be created although it is possible use their pointers types.

What are C++ storage classes?

a) auto: the default. Variables are automatically created and initialized when they are defined and are destroyed at the end of the block containing their definition. They are not visible outside that block.

b) register: a type of auto variable. a suggestion to the compiler to use a CPU register for performance.

c) static: a variable that is known only in the function that contains its definition but is never destroyed and retains its value between calls to that function. It exists from the time the program begins execution.

d) extern: a static variable whose definition and placement is determined when all object and library modules are combined (linked) to form the executable code file. It can be visible outside the file where it is defined.

What are storage qualifiers in C++ ?

a) Const keyword indicates that memory once initialized, should not be altered by a program.

b) Volatile keyword indicates that the value in the memory location can be altered even though nothing in the program code modifies the contents. for example if you have a pointer to hardware location that contains the time, where hardware changes the value of this pointer variable and not the program. The intent of this keyword to improve the optimization ability of the compiler.

c) Mutable keyword indicates that particular member of a structure or class can be altered even if a particular structure variable, class, or class member function is constant.

How to initialize a const member data?

Use constructor initializer list.

What is the difference between new and Malloc?

New invokes constructor, Malloc will not
New doesn’t need type casting, Malloc requires typecasting to required type
New operator can be overloaded, Malloc can’t
New does not require to explicitly specifying the quantity of memory allocated,

What is the difference between delete and free?

Delete invokes destructor, free will not

What is placement new operator?

Operator new allocates memory from the heap on which an object is constructed. But the placement new operator constructs an object on a pre-allocated buffer. Since the memory is already allocated in placement new operator, so there is no danger of memory allocation failure and constructing an object on a pre-allocated buffer takes less time.

void PlacementNew()
TCHAR* preBuff = new TCHAR[1000];
CString* ptr = new (preBuff) CString(“hello”); //placement new

What is the difference between a copy constructor and an overloaded assignment operator?

A copy constructor constructs a new object by using the content of the argument object. An overloaded assignment operator assigns the contents of an existing object to another existing object of the same class.

Copy Constructor invokes from following 3 scenarios

a) Creation and initialization of an object simultaneously.
b) When an object is passed to a function by value.
c) When an object is returned from a function by value.

Can a copy constructor accept an object of the same class parameter as pass by value, instead of reference of the object?

No. It is specified in the definition of the copy constructor itself. It should generate an error if a programmer specifies a copy constructor with a first argument that is an object and not a reference. Even if it won’t prompt any error it will make infinite loops during execution.

Define Stack & Heap Objects ?

The memory a program uses is typically divided into four different areas:
* The code area, where the compiled program sits in memory.
* The globals area, where global variables are stored.
* The heap, where dynamically allocated variables are allocated from.
* The stack, where parameters and local variables are allocated from.

What is a mutable member?

One that can be modified by the class even when the object of the class or the member function doing the modification is const.

What is a modifier?

A modifier, also called a modifying function is a member function that changes the value of at least one data member. In other words, an operation that modifies the state of an object. Modifiers are also known as ‘mutators’.

What is an accessor?

An accessor is a class operation that does not modify the state of an object. The accessor functions need to be declared as const operations

What is a dangling pointer?

A dangling pointer arises when you use the address of an object after its lifetime is over.
This may occur in situations like returning addresses of the automatic variables from a function or using the address of the memory block after it is freed.

What do you mean by Stack unwinding?

It is a process during exception handling when the destructor is called for all local objects between the place where the exception was thrown and where it is caught.

Differentiate between a deep copy and a shallow copy?

Deep copy involves using the contents of one object to create another instance of the same class. In a deep copy, the two objects may contain the same information but the target object will have its own buffers and resources. the destruction of either object will not affect the remaining object.
Shallow copy involves copying the contents of one object into another instance of the same class thus creating a mirror image. Owing to straight copying of references and pointers, the two objects will share the same externally contained contents of the other object to be unpredictable.

If the object has any pointers a deep copy needs to be executed.

What is slicing?

Slicing means that the data added by a subclass are discarded when an object of the subclass is passed or returned by value or from a function expecting a base class object.

Describe the main characteristics of static functions.

 It is without the a this pointer,
 It can’t directly access the non-static members of its class
 It can’t be declared const, volatile or virtual.
 It doesn’t need to be invoked through an object of its class, although for convenience, it may.

What is pure virtual function?

A class is made abstract by declaring one or more of its virtual functions to be pure. A pure virtual function is one with an initializer of = 0 in its declaration.

What is binding?

The term binding refers to the connection between a function call and the actual code executed as a result of the call.

There are two types of binding.

a) Static binding or Early binding
When a function call gets resolved at compile-time is called static binding.

b) Dynamic binding or Late binding
When a function call gets resolved at runtime is called dynamic binding.

What is Scalar Delete?

When you create an array of objects using new[], it is necessary to delete the same by using delete[]. Then only it deletes the complete array and calls the destructors for each object in the array.

What is name mangling or name decoration?

Name mangling means different unique names are given to the different versions of an overloaded function. Name mangling is compiler dependent. Different compiler may mangle the same function name differently. Compiler mangles the names as per type, order and number of arguments.

How name mangling can be prevented?

To avoid name mangling the function should be declared with extern “C” attribute. Function declared as extern “C” are treated as C-style functions. Hence compiler doesn’t mangle them.

What is “this” pointer?

The “this” pointer is a pointer accessible only within the member functions of a class, struct, or union type. It points to the object for which the member function is called. Static member functions do not have a “this” pointer. When a non-static member function is called for an object, the address of the object is passed as a hidden argument to the function.

What is the type of “this” pointer?

It would be a constant pointer.

When does a “this” pointer get created?

The “this” pointer gets created when a non-static member function of a class is called.

What happens when you make call “delete this;” ?

The code has two built-in pitfalls. First, if it executes in a member function for an extern, static, or automatic object, the program will probably crash as soon as the delete statement executes. There is no portable way for an object to tell that it was instantiated on the heap, so the class cannot assert that its object is properly instantiated. Second, when an object commits suicide this way, the using program might not know about its demise. As far as the instantiating program is concerned, the object remains in scope and continues to exist even though the object did itself in. Subsequent dereferencing of the pointer can and usually does lead to disaster.
You should never do this.

What is inline function?

The inline keyword tells the compiler to substitute the code within the function definition for every instance of a function call. However, substitution occurs only at the compiler’s discretion. For example, the compiler does not inline a function if its address is taken or if it is too large to inline.

Can inline functions have a recursion?

Syntax wise It is allowed. But then the function is no longer Inline. As the compiler will never know how deep the recursion is at compilation time.

What is overloading?

Overloading is the practice of supplying more than one definition for a given function name in the same scope.

There are two type of overloading, Function Overloading and Operator Overloading.
Any two functions in a set of overloaded functions must have different argument lists.

What is Overriding?

Overriding a method means that replacing a method functionality in derived class. To imply overriding functionality we need base and derived classes. In the derived class you define the same method signature as one defined in the base class.

How can we differentiate between a pre and post increment operators while overloading?

Mentioning the keyword int as the second parameter in the post increment form of the operator++() helps distinguish between the two forms.

What is VTABLE?

A VTABLE contains address of virtual functions. The compiler creates a VTABLE for each class that contains at least one virtual function and classes that are derived from it. The VTABLE contains addresses in the order in which virtual functions are defined within the class.

What is VPTR?

The address of the VTABLE stored in the object is known as VPTR.

What is upcasting?

Upcasting means storing the address of the derived class object in the base class pointer.

What are the virtual base classes?
What is the Diagonal issue?

Consider the situation where there is one parent class called base and two classes derived from it, derived1 and derived2. Suppose we derive a class derived3 from derived1 and derived2. Now suppose a member function of derived3 class wants to access data or functions in the base class. Since derivd1 and derived2 are derived from base, each inherits a copy of base. Now when derived3 refers to the data in the base class, which of the two copies will it access? This is an ambiguous situation for the compiler, hence it reports an error. The get rid of this ambiguity, we should make derived1 and derived2 as virtual base classes. That means derived1 and derived2 derived from base with virtual keyword.

Can we declare a static function as virtual?

No. The virtual function mechanism is used on the specific object that determines which virtual function to call. Since the static functions are not any way related to objects, so they cannot be declared as virtual.

Is each object has its own virtual table?

No. The VTABLE belongs to a class. All objects of the class share the same VTABLE.

What is an anonymous union?

A union which does not have a union name is called anonymous union and its elements can be accessed directly without using a union variable.

What is a reference?

A reference is a constant pointer. Hence once initialized a reference cannot be made refer to another variable.

What are the advantages of reference over pointers?

A reference gets automatically de-referenced. So we don’t have to use value at address operator(*). Also if we want the change being made in the parameter in the called function to become effective in the calling function, we shall make a call by reference.

What is the difference between a pointer and a reference?

A reference must always refer to some object and, therefore, must always be initialized, pointers do not have such restrictions. A pointer can be reassigned to point to different objects while a reference always refers to an object with which it was initialized.

How can we initialize a const data member?

The const data member can be initialized with constructor initializer list.

What is anonymous class?

Whenever a class is defined without any name is called anonymous class.

What is the size of an empty class?

Normally 1 byte, sometimes it may change depends on compiler. Since C++ allows us to create an object of an empty class it should have some bytes of memory. The minimum amount of memory that could be reserved is 1 byte.

What is Smart Pointers?

Smart pointers are objects that look and feel like pointers, but are smarter. To be smarter than regular pointers, smart pointers need to do things that regular pointers don’t. Probably the most common bugs in C++ are related to pointers and memory management: dangling pointers, memory leaks, allocation failures and other problems. Smart pointer take care of these things.
The simplest example of a smart pointer is auto_ptr, which is included in the standard C++ library.

For example consider a function:

void Fun()
TestClass* p(new TestClass);
delete p;

You can write the same using auto_ptr:
void Fun()
auto_ptr<TestClass> p(new TestClass);

Why should we use forward declaration?

Forward declaration reduces the dependency between classes or modules.

Consider a situation,
//File A.h
class A
// File B.h
class A; //forward declaration
class B
B* m_pTest; // no error

Once you use forward declaration like this, then it won’t compile the B.h corresponding to modifications in A.h

That means it reduces the compile time of a solution. It is very helpful while doing bug fixes in small modules of big projects.

Also sometimes we can avoid class redefinition compiler error too.

What is a pdb file?

A program database (PDB) file holds debugging and project state information that allows incremental linking of a Debug configuration of your program. A PDB file is created when you compile a C/C++ program with /ZI or /Zi or a Visual Basic/C#/JScript .NET program with /debug.

What is an ncb file?

Non-compressed browse file generated by Visual Studio. It holds information for ClassView, IntelliSense and the WizardBar. File will be recreated by Visual Studio automatically if it deleted.

What is an opt file?

The workspace options file generated by Microsoft Visual Studio. Holds working workspace configuration information, such as current window and breakpoint positions. This file can be safely deleted and will be recreated automatically, but with the resultant loss of some information, such as breakpoint positions etc.

Explain the scope resolution operator?

It permits a program to reference an identifier in the global scope that has been hidden by another identifier with the same name in the local scope.

What is conversion operator function?

The conversion from one user-defined type to another can be performed by conversion operator functions.

For example:
class Circle

class Rectangle()

//conversion operator function
operator Circle()
return Circle();

void main()
Rectangle r;
Circle c;
c = r;

When c = r is executed the compiler searches for an overloaded assignment operator in the class Circle which accepts the object of type Rectangle. Since there is no such function, the conversion operator function will be executed.

What are the C++ casting operators?

Casting means change the representation of a variable by changing its type to a different one.

There are four different type-casting operators in C++.

a) static_cast : Used for conversion of nonpolymorphic types.

b) dynamic_cast : Used for conversion of polymorphic types.

c) const_cast : Used to remove the const, volatile, and __unaligned attributes.

d) reinterpret_cast : Used for simple reinterpretation of bits.

7 responses

29 08 2010


i ask you nicely if you can update this code so it can work on c++ 2008 ?

and if possible instead of message box can you save it to log file…

greatly appreciated if you could do this…


romeofq@gmail.com //my contact

26 04 2013

nice one

25 09 2013

very good one…comprehensive..thanks

11 05 2014

It really covered all most all imp points and Thanx for that.
But missing some points lile – Design pattern concepts , STL.

16 06 2014

Very Good. Thanks for giving such important

21 07 2014
Sanoop S P

Thank you.

With Best Regards,

8 09 2014

Its toooooooooooo goood.Thank you

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: