Top 100 C++ Interview Q&A
Top 100 C++ Interview Q&A
Why C++?
– Google Chrome, Mozilla Firefox etc. web browsers are developed using
C++
There are many more such uses that make C++ the desired language.
What is namespace in C++?
If there are two or more functions with the same name defined in different
libraries then how will the compiler know which one to refer to? Thus
namespace came to the picture. A namespace defines the scope and
differentiates functions, classes, variables etc. with the same name
available in different libraries. The namespace starts with the keyword
“namespace”. The syntax for the same is as follows:
1namespace namespace_name {
2
3 // code declarations
4
5}
C C++
Functions can not be defined inside structures. Functions can be defined inside structures.
return (a+b);
int main(){
cout<<fun<int>(11,22);
}
What is using namespace std in C++?
Using namespace std in C++ tells the compiler that you will be making use
of the namespace called ‘std’. The ‘std’ namespace contains all the
features of the standard library. You need to put this statement at the start
of all your C++ codes if you don’t want to keep on writing std:: infront of
every variable/string or whatever standard library feature you are making
use of, as it becomes tedious to do so.
• Shift+Insert
• Open the file in notepad with .cpp extension. Make the changes
and save it. After saving the file, you can open it from the Turbo
C++ application file menu from where you stored the cpp file.
What is a pointer in C++?
Pointers in C++ are a data type that store the memory address of another
variable.
For eg.
Here the pointer variable *str points to the string "Hi, How are you?"
or
int age;
int *int_value;
*int_value = &age;
cin>>age;
// this will print your age as the variable is pointing to the variable age.
int fun(){
int a = 11;
return 11;
int main(){
int b = fun();
Stl is the standard template library. It is a library that allows you to use a
standard set of templates for things such as: Algorithms, functions,
Iterators in place of actual code.
queue<int> Q;
for(k=0;k<10;k++)
Q.push(k);
}
How to run a C++ program in cmd?
$ gcc main.cpp
or
$ main.exe
Type casting in C is used to change the data type. They are of two types:
Implicit Type Conversion: It is automatic. Explicit Type Conversion: It is user-
defined.
– By default, the data members of the class are private whereas data
members of structure are public.
Step-3: Configure notepad++ for gcc. This step can be further divided into two sub-steps. A:
Create C compiler tool in Notepad++
There are 95 reserved keywords in C++ which are not available for re-
definition or overloading.
It is a header file that includes basic objects such as cin, cout, cerr, clog.
In C++ programming, the space can be given using the following code.
cout << ” ” ;
– “sizeof” operator
– Synchronous
– Asynchronous
– try
– catch
– throw
What is the difference between C++ and Java?
This is one of the most common c++ interview questions asked, the
difference between c++ and java are as follows:
– C++ has pointers which can be used in the program whereas Java has
pointers but internally.
– C++ uses a compiler only whereas Java uses both compiler and
interpreter.
– C++ has both call by value and call by reference whereas Java supports
only call by value.
– C++ supports structures and joins whereas Java does not support
structure and joins
– Java supports unsigned right shift operator (>>>) whereas C++ does not.
– C++ is interactive with hardware whereas Java is not that interactive with
hardware.
A linear data structure which implements all the operations (push, pop) in
LIFO (Last In First Out) order. Stack can be implemented using either arrays
or linked list.The operations in Stack are
– Push: adding element to stack
Conio.h is a header file used for console input and output operations and is
used for creating text based user interfaces.
To exit Turbo C++, use the Quit option under the File Menu, or press Alt + X.
Any object which has an ability to iterate through elements of the range it
has been pointing to is called iterator.
What is :: in C++?
When you have written code in the file (notepad),save the file as
“hello.cpp.” If you want to write in a file using C++ code, you can do it using
iostream and fstream libraries in C++.
#include <iostream>
#include <fstream>
int main () {
ofstream file_name;
file_name.open ("sample.txt");
file_name.close();
return 0;
- Integral expressions: x * y
If the program does not have using namespace std; then when you write
cout <<; you would have to put std::cout <<; same for other functions such
as cin, endl etc.
Which is the best C++ compiler?
MinGW /
Dev C++
Embracadero
Clang
Visual C++
Intel C++
Code Block
GCC and clang are great compilers if the programmers target more
portability with good speed.
Intel and other compilers target speed with relatively less emphasis on
portability.
• Primitive Datatype such as char, short, int, float, long, double, bool,
etc.
• Derived datatype such as an array, pointer, etc.
• Enumeration such as enum
• User-defined data types such as structure, class, etc.
What are the advantages of C++?
Reference Pointers
A reference shares the same memory address with the original Pointer has its own memory address
variable and takes up some space on the stack and size on the stack
Exceptions are errors that happen during the execution of code. To handle
them we use throw, try & catch keywords.
This section of the blog talks about advanced C++ Interview Questions for
your reference.
class Circle{
public:
float radius;
A friend function has access rights to all private and protected members of
the class.
class Circle
double radius;
public:
};
void printradius(Circle c )
{
/* Because printradius() is a friend of Circle, it can directly access any member of this class
*/
Circle c;
#include<iostream>
#include<vector>
int main()
vec_push_back("sample code");
vec_push_back("change example");
cout<<*i;
return 0;
}
A feature that allows functions and classes to operate with generic types
which means a function or class can work on different data types without
being rewritten is called a template.
#include <bits/stdc++.h>
int main()
sort(vec.begin(), vec.end());
for (auto x : v)
return 0;
}
A pure virtual function is a type of virtual function which does not have
implementation, but is only declared. It is declared by assigning 0 in
declaration.
1class Test
2
3{
4
5 // Data members of class
6
7public:
8
9 virtual void show() = 0;
10
11 /* Other members */
12
13};
#include <iterator>
#include <map>
int main()
// inserting elements
return 0;
#include <iostream>
#include <vector>
int main ()
{
std::vector<int> vec;
while (!vec.empty())
add+= vec.back();
vec.pop_back();
return 0;
int* p = malloc(8);
*p = 100;
free(p);
*p = 110;
Solution: Before freeing the pointer check the assignment or any operation
required to perform.
int arr[2];
arr[3] = 10;
int n = 2;
scanf("%d",n);
int *p;
printf("%d",*p);
Here classes are data types that allow you to list several types of
data within it and even functions. You can access these classes with the
help of class objects
Constructor in C++ is a method in the class which has the same name as
that of the class and is followed by parentheses (). It is automatically
called when an object of a class is created.
};
int main() {
Hello obj; // Create an object of Hello (this will call the constructor)
return 0;
Inheritance in C++ is just like a child inherits some features and attributes
from his parent similarly a class inherit attributes and methods from
another class. The parent class is called base class and the child class is
called derived class.
// Base class
class Food_Item{
public:
void taste() {
};
// Derived class
public:
void taste() {
};
Class in C++ provides a blueprint for object, that means, object is created
from the class.
For example,
class Circle{
public:
float radius;
Circle C1;
Circle C2;
Abstraction in C++ means showing only what is necessary. It’s part of the
Object-oriented Programming concept. Abstraction is used to hide any
irrelevant data from the outside world and only show what is absolutely
necessary for the outside world to use.
eg. Classes use the abstraction concept to only show relevant data types
or elements. This is done through access specifiers such as: public, private,
and protected.
Member functions are those functions that you declare within a class, they
aremembers of the class. You can reference them using class objects. Eg:
class A
public:
int add(int b)
{
a = b * 10;
return a;
};
};
Here X & Y inherit from W. So they both have similar features being
inherited from W.
Here Z may inherit similar features from X & Y as they both have inherited
them from W. This can cause issues and that’s why we use virtual base
classes as they stop multiple features of a class from appearing in another
class.
#include <iostream>
class sample_test{
private:
int n;
public:
sample_test() { n = 45; }
int display() {
return n;
};
An abstract class in C++ is such that cannot be used directly and is used
to form a base class for others to inherit from.
If you create an object for an abstract class the compiler will throw an error
at you.
Polymorphism Concept
#include <bits/stdc++.h>
class parent
public:
void print()
};
public:
void print()
};
int main()
parent *p;
child c;
p = &c;
p->print();
return 0;
The size of an empty class is 1 byte generally just to ensure that the two
different objects will have different addresses.
#include <iostream>
int main()
return 0;
}
How to input string in C++?
There are three ways to input a string, using cin, get, and getline. All three
methods are mentioned in the sample program below.
#include <iostream>
int main()
char s[10];
cin.get(s, 10);
getline(cin, str);
return 0;
#include<iostream>
#include<string.h>
int main ()
{
char n[50], t;
int i, j;
gets(n);
i = strlen(n) - 1;
t = s[j];
s[j] = s[i];
s[i] = t;
return 0;
Approach-1
#include<iostream>
#include<string>
int n= 1;
string s= to_string(n);
cout << s;
Approach-2
#include<iostream>
#include <sstream>
#include <string>
int main()
int n = 17;
ostringstream s1;
return 0;
#include <iostream>
#include <string>
int main()
string s;
getline(cin, s);
return 0;
}
There are several methods by which one can allocate memory to 2D array
dynamically one of which is as follows:
#include <iostream>
int main()
int i, j, count = 0;
delete[ ] a;
return 0;
}
How to use goto statement in C++ ?
The syntax is
1goto label;
2label: statement;
#include <iostream>
void main () {
int j, n;
cin >> n;
cin >> d;
goto jump;
}
add+= d;
jump:
When a function with same name is present in both parent and child class
then it is called function overriding.
#include <iostream>
class parent {
public:
void display(){
cout<<"Parent Class";
};
public:
void display() {
cout<<"Child Class";
}
};
int main() {
child o = parent();
o.display();
return 0;
Bool is a data type in C++ which takes two values- True and False.
1 bool b1 = true;
#include<iostream>
int main()
bool c, d;
c= a== b; // false
c= a< b; // true
cout <<b1;
cout << b2 ;
return 0;
For limiting the decimal places in C++ there are five functions : floor(),
ceil(), trunc(), round() and setprecision(). Out of these five, only
setprecision() function is used for setting the decimal places to put as
output. All the functions are mentioned in the following sample code.
#include<bits/stdc++.h>
int main()
float a =33333;
return 0;
}
How to get absolute value in C++?
In C++, there are three functions in the cstdlib header file to return the
absolute value of the integer. Those are:
• abs()
• labs()
• llabs()
The syntax for all the functions is same
1 function_name(integer value)
• The difference lies in the range for integer value being passed as
an argument.
• For abs() its type int in C++.
• For labs(), its type long int in C++
• For llabs() its long long int in C++.
The sample code for illustrating the three functions is as follows:
#include <cstdlib>
#include <iostream>
int main()
int a, b, c;
a = abs(22);
b= labs(1234355L);
c= llabs(1234863551LL);
cout << a;
cout << b;
cout<< c;
return 0;
#include <iostream>
int main()
cin.getline(str1, 50);
cin.getline(str2, 50);
strcat(str1, str2);
return 0;
There are three methods for converting char variable to int type variable.
These are as follows:
• atoi()
• sscanf()
• typecasting
A sample code depicting all three functions are as follows:
#include<stdio.h>
#include<stdlib.h>
int main() {
char *s = "6790";
char d = 's';
int a,b,c;
return 0;
Using the rand() function we can generate random numbers in C++ within
a range.
#include <iostream>
#include <random>
int main()
{
cout<<num;
return 0;
To find the absolute value in c++, we can use abs() function. The abs()
function in C++ returns the absolute value of an integer number.
#include <iostream>
#include <cstdlib>
int main()
int a=3.456;
int x = abs(a);
cout << x;
return 0;
}; //Class end
For example:
class Sample
// Access specifier
private:
// Data Members
string s;
// Member Functions()
void printname()
cout << s;
};
#include<string.h>
int main()
if (r==0)
printf("Strings are equal");
else
printf("%d" , r);
return 0;
#include <iostream>
#include <fstream>
int main()
ofstream fout;
string r;
fout.open("test.txt");
while (fout) {
getline(cin, r);
if (r == "-1")
break;
fout.close();
ifstream fin;
fin.open("test.txt");
while (fin) {
getline(fin, line);
fin.close();
return 0;
Syntax is as follows:
1 stringstream string_name(str);
str()
<<
>>