Complete C++ Interview Questions & Answers

Mohit Soni
Analytics Vidhya
7 min readAug 24, 2020

--

The article cover’s most commonly asked interview questions in c++ and a brief explanation of each question.

1. What is OOPS?

Object-oriented programming is a programming paradigm based on the concept of “objects” of the classes, Objects may contain data in the form of fields and associated code in the form of methods. The objects can access their own procedures and can modify their data fields.

2. What is function overloading, how did it make the lives of C++ programmer better than C programmer?

In C++ it’s allowed to make functions with the same name in the same scope. The functions with the same name are called overloaded functions. The difference that must exist between the two functions with the same name is the parameters that are being passed. Function overloading can be done by using different parameter types and changing the number of arguments. C, on the other hand, doesn’t support function overloading which reduces the code quality and readability.

3. How is function overloading achieved?

The program below has “add” function with two different implementations. One for double data type numbers and another for integers. The function has the same name but different parameters so it’s a clear example of function overloading.

#include<bits/stdc++.h>
using namespace std;
int add(int a, int b){//Add Integers
return a+b;
}
double add(double a, double b){//Add Double
return a+b;
}
int main(){
cout<<"Integer Type Numbers after addition : "<<add(1,2)<<"\n";
cout<<"Double Type Numbers after addition : "<<add(1.1,2.2);
}
Output:
Integer Type Numbers after addition : 3
Double Type Numbers after addition : 3.3

4. what is dynamic binding?

To Understand Dynamic binding let’s see what are virtual functions and what binding means.

Binding refers to the process of converting identifiers such as variable and function calls into addresses by the compiler.

Virtual Function in c++ is a function which is defined in“Base” class and overridden in “Derived” class. It is made by adding “virtual” keyword in the function definition.
Dynamic Binding: In C++ when the compiler is not able to decide which function to call at compile time but computes the same at runtime then this type of binding is known as dynamic binding.
An example of Dynamic Binding is when a virtual function is defined in “Base” class and overridden in “Derived” class, and this function is called with the help of pointer of the base class that stores the address of the object of “Derived Class”.

#include<bits/stdc++.h>
using namespace std;
class Base{
public:
virtual void print(){
cout<<"Base Class";
}
};
class Derived : public Base{
public:
void print(){
cout<<"Derived Class";
}
};
int main(){
Base *ptr;
Derived d;
ptr=&d;
ptr->print();
}
Output : Derived Class

In the above program when the compiler compiles the program a pointer named “ptr” is made which is initialized by the address of an object of a derived class and this address is only available at compile time. With this information, the compiler is able to know which print function is being referenced by the programmer so as a result derived class “print” function is called.

5. What is an abstract class?

Abstract class in c++ is a Class that has no object and that Class consists of at least one pure virtual function. We are not allowed to make instances of Abstract classes. This class as its name suggests provides us with a level of abstraction about the functionalities we are going to encounter in a program like the print functionality explained below.
Example:

#include<bits/stdc++.h>
using namespace std;
class Base{
public:
virtual void print()=0;
};
class Derived : public Base{
public:
void print(){
cout<<"Abstract Class print method defination.";
}
};
int main(){
ptr=&d;
d.print();
}

The pure virtual “print” function is declared in the Abstract class named “Base” and defined in “Derived” class, in this way the abstract class provided us with the abstraction to what we are going to encounter. If the Derived class inherits the abstract class and does not define the pure virtual functions of Base class then it also becomes an Abstract class.

6. What happens under the hood for virtual functions? (vPointer, vTable etc)

For understanding this part let’s go on to the example we considered while learning Dynamic Binding.

#include<bits/stdc++.h>
using namespace std;
class Base{
public:
virtual void print(){
cout<<"Base Class";
}
};
class Derived : public Base{
public:
void print(){
cout<<"Derived Class";
}
};
int main(){
Base *ptr;
Derived d;
ptr=&d;
ptr->print();
}
Output : Derived Class

As in the above program, ptr is a pointer to Base class but still ptr->print() calls the “print” function of Derived class.
Let’s understand what internally happens here:

  1. For every class that has a virtual function, compiler maintains a vTable that contains information about all virtual functions inside the class.
  2. For every object of the class, the first 4 bytes contain the pointer to vTable of the class it belongs to, and this pointer is called vPointer.

The above two points are the reason that ptr in the above program even being the pointer of Base class calls the print function of the derived class, as ptr=&d where d is an object of the derived class and it’s first 4 bytes contain vPointer that point to vTable of Derived class which contains the print function.

7. What is the diamond problem?

The problem when two classes inherit a base class and those two classes are inherited by another class then the class that inherits both the classes has two copies of the methods and properties of the base class.

  A
/ \
B C // B and C inherit A
\ /
D // D inherit both B and C so has A two times

The below program will give an error:

#include<bits/stdc++.h>
using namespace std;
class Base{
public:
int a=10;
};
class Derived1 : public Base{
public:
int b=20;
};
class Derived2 : public Base{
public:
int c=30;
};
class Derived3 : public Derived1, public Derived2{
public:
int d=40;
};
int main(){
Derived3 d;
cout<<d.a;
}

The error is that a comes twice in Derived3 class and compiler finds ambiguity in data member a.
To solve this problem we inherit Base class virtually in Derived1 & Derived2 which makes only one instance of data member a and thus solves the diamond problem.
The right code will be:

#include<bits/stdc++.h>
using namespace std;
class Base{
public:
int a=10;
};
class Derived1 : public virtual Base{
public:
int b=20;
};
class Derived2 : public virtual Base{
public:
int c=30;
};
class Derived3 : public Derived1, public Derived2{
public:
int d=40;
};
int main(){
Derived3 d;
cout<<d.a;
}

8. What is operator overloading?

Normally a “+” operator would be used to add two numbers, but if we want to add objects of two classes then it is also possible, for that operator overloading as a concept is used.

Consider the below program:

#include<bits/stdc++.h>
using namespace std;
class A{
public:
int a=10;
};
class B{
public:
int a=20;
};
int operator+(A obj1,B obj2){
return obj1.a+obj2.a;
}
int main(){
A obj1;
B obj2;
cout<<obj1+obj2;
}
Output: 30

In the above program, as we see the two objects get added, this is because we overloaded the + operator. For overloading, we defined int as the return value of “operator+” function which takes two arguments that are objects of the two classes. So with this way, we were able to add two objects using the + operator. Operator overloading works similarly for all overloadable operators.
Non Overloadable operators are:

::
.
*.
?:

9. What is a namespace?

A namespace is a declarative region that provides a scope to identifiers inside it. A Namespace helps to group elements in groups to provide collisions when multiple libraries are being used. Identifiers in one namespace are visible to each other without qualification, but for variables outside the same namespace needs to be specified with a fully qualified name for example: “std:: string”.

10. When and why will you use static functions in a class?

A static function is used in a class when the objects of the class share a common function that doesn’t need to have a new copy for each object of that class.

#include<bits/stdc++.h>
using namespace std;
class A{
public:
static printName(){
cout<<"I am object of class A\n";
}
int a;
};
int main(){
A obj1;
A obj2;
obj1.printName();
obj2.printName();
}
Output:
I am object of class A
I am object of class A

In the above program for each object “printName ”function will remain the same, so putting static keyword before the function definition makes only one shared function for each object of class A.

11. Do you have an idea about Standard Template Library In C++?

Standard Template Library in c++ is a set of template classes that provides a direct implementation of common data structures like a stack, queue, list, set etc. STL has four major categories that are:

  • Algorithms
  • Containers
  • Functions
  • Iterators

12. How are errors handled in C++?

In C++ exceptions are handled with the help “try ”and “catch ” blocks. The block of code that may throw an exception is kept under try block.
Example:

#include<bits/stdc++.h>
using namespace std;
float divide(float a,float b){
try{
if(b!=0){
return a/b;
}
else{
throw "Divide By Zero Exception\n";
}
}
catch(const char* err){
cout<<err;
}
}
int main(){
cout<<divide(1.2,0);
}
Output: Divide By Zero Exception

Some other must know Questions are:

  1. Difference Between C & C++?
  2. Do you have an idea what metaprogramming is? How is it achieved in C++?
  3. How many kinds of castings are available in C++, how is it better than C?

--

--