C++ Interview Questions (2021) - InterviewBit
C++ Interview Questions (2021) - InterviewBit
C++ is a powerful and all-purpose programming tool developed by Bjarne Stroustrup at Bell
Labs. This language is an extension of C and is by far one of the fastest object-oriented
programming languages. C++ is super popular because of its high speed and compatibility.
It is widely used in the development of games and servers while some of the real-world
applications of C++ are as follows
Operating systems
GUI based applications
Distributed systems
Database software
Banking applications
Advanced computations and graphics
Embedded systems
So, today, well understand the different C++ questions asked in an interview at a basic,
intermediate and advanced level.
Primitive Datatype(basic datatype). Example- char, short, int, float, long, double, bool, etc.
Derived datatype. Example- array, pointer, etc.
Enumeration. Example- enum
User-defined data types. Example- structure, class, etc.
C C++
Function and operator overloading are not supported Function and operator overloading is
in C supported in C++
Namespace features are not present in C Namespace is used by C++, which avoids name
collisions.
Functions can not be defined inside structures. Functions can be defined inside structures.
calloc() and malloc() functions are used for memory new operator is used for memory allocation
allocation and free() function is used for memory and deletes operator is used for memory
deallocation. deallocation.
An object is an instance of a class. Since a class is a user-defined data type so an object can
also be called a variable of that data type.
class A{
private:
int data;
public:
void fun(){
};
15'
For example, the following is a class car that can have properties like name, color, etc. and they
can have methods like speed().
Structure Class
Members of the structure are public by default. Members of the class are private by
default.
When deriving a struct from a class/struct, default access When deriving a class, default access
specifiers for base class/struct are public. specifiers are private.
For example -
The following code is for adding two complex number using operator overloading-
15'
class complex{
private:
float r, i;
public:
this->r=r;
this->i=i;
complex(){}
void displaydata(){
};
int main(){
complex a(2,3);
complex b(3,4);
complex c=a+b;
c.displaydata();
return 0;
For example, think of a base class called a car that has a method called car brand(). Derived
classes of cars could be Mercedes, BMW, Audi - And they also have their own implementation
of a cars
Polymorphism in C++
Example:
class A{
private:
15'
int val;
public:
val=x;
int main(){
A a(3);
return 0;
In this method, we would come to know at compile In this method, we come to know at run time
time which method will be called. And the call is which method will be called. The call is not
resolved by the compiler. resolved by the compiler.
It provides fast execution because it is known at the It provides slow execution compared to
compile time. compile-time polymorphism because it is
known at the run time.
It is achieved by function overloading and operator It can be achieved by virtual functions and
overloading. pointers.
Compile-time polymorphism Run time polymorphism
15'
Example - Example -
return a+b;
public:
}
void fun(){
return a+b+c;
}
}
};
int main(){
public:
cout<<add(2,3)<<endl;
void fun(){
cout<<add(2,3,4)<<endl;
cout<<”derived ”;
};
return 0;
int main(){
} A *a=new B;
a->fun();
return 0;
10. What do you know about friend class and friend function?
A friend class can access private, protected, and public members of other classes in which it is
declared as friends.
Like friend class, friend function can also access private, protected, and public members. But,
Friend functions are not member functions.
For example -
class A{
15'
private:
int data_a;
public:
A(int x){
data_a=x;
class B{
private:
int data_b;
public:
A(int x){
data_b=x;
int main(){
A a(10);
B b(20);
cout<<fun(a,b)<<endl;
return 0;
Public: All data members and member functions are accessible outside the class.
Protected: All data members and member functions are accessible inside the class and to the
derived class.
Private: All data members and member functions are not accessible outside the class.
For example-
int x=10;
If we change the value of ref it will be reflected in x. Once a reference variable is initialized it
cannot refer to any other variable. We can declare an array of pointers but an array of
references is not possible.
In call by reference method, we pass the address of the variable and the address is used to
access the actual argument used in the function call. So changes made in the parameter alter
the passing argument.
Example:
class A{
private:
int val;
public:
A(int x){
val=x;
A(){
~A(){ //destructor
int main(){
A a(3);
return 0;
19. What are the static members and static member functions?
When a variable in a class is declared static, space for it is allocated for the lifetime of the
program. No matter how many objects of that class have been created, there is only one copy
of the static member. So same static member can be accessed by all the objects of that class.
A static member function can be called even if no objects of the class exist and the static
function are accessed using only the class name and the scope resolution operator ::
Example-
15'
Inheritance in C++
Class Bus, Class Car, and Class Truck inherit the properties of Class Vehicle.
The most important thing about inheritance is that it permits code reusability.
Example-
class A{
int x,y;
this->x=x;
this->y=y;
};
int main(){
A a1(2,3);
return 0;
}
We can define our copy constructor. If we don’t define a copy constructor then the default
copy constructor is called. 15'
22. What is the difference between shallow copy and deep copy?
The difference between shallow copy and a deep copy is given below:
Shallow copy stores the references of objects to Deep copy makes a new and separate copy of an
the original memory address. entire object with its unique memory address.
Shallow copy reflects changes made to the Deep copy doesn’t reflect changes made to the
new/copied object in the original object. new/copied object in the original object
23. What is the difference between virtual functions and pure virtual
functions?
A virtual function is a member function in the base class that you redefine in a derived class. It
is declared using the virtual keyword.
Example-
class base{
public:
};
A pure virtual function is a function that has no implementation and is declared by assigning 0.
It has no body.
Example-
class base{
public:
};
Here, = sign has got nothing to do with the assignment, and value 0 is not assigned to
anything. It is used to simply tell the compiler that a function will be pure and it will not have
anybody.
24. If class D is derived from a base class B. When creating an object of
15'
type D in what order would the constructors of these classes get
called?
The derived class has two parts, a base part, and a derived part. When C++ constructs derived
objects, it does so in phases. First, the most-base class(at the top of the inheritance tree) is
constructed. Then each child class is constructed in order until the most-child class is
constructed last.
So the first Constructor of class B will be called and then the constructor of class D will be
called.
During the destruction exactly reverse order is followed. That is destructor starts at the most-
derived class and works its way down to base class.
So the first destructor of class D will be called and then the destructor of class B will be called.
For example-
class base{
private:
15'
int value;
public:
base(int x){
value=x;
class derived{
private:
int a;
public:
base *b;
b=this;
void fun(){
For example-
void *ptr;
char *str;
p=str; // no error
We can assign a pointer of any type to a void pointer but the reverse is not true unless you
typecast it as
str=(char*) ptr;
Example
class A{
private:
int value;
public:
this->value=x;
};
int main(){
A a;
a.setvalue(5);
return 0;
For example-
Additional Resources
C++ MCQs
1. Which operator can not be overloaded in C++?
+ 15'
::
++
#include<iostream>
int main(){
int a=1;
cout<<(a++)*(++a)<<endl;
return 0;
#include<iostream>
int main(){
int a=1;
int x=(a++)++;
cout<<x<<endl;
return 0;
1
2 15'
#include<iostream>
class A{
public:
A(){
cout<<"A ";
};
class B: public A
public:
B(){
cout<<"B ";
};
int main(){
A *a=new B();
return 0;
AB
BA
Compile-time error
None of the above
15'
7. If a base class and derived class each include a member function with the same name.
Function from which class will be called if called by an object of the derived class
Contiguous
Non-contiguous
Not determined
Blog (https://blog.interviewbit.com)
About Us (/pages/about_us/)
FAQ (/pages/faq/)
Contact Us (/pages/contact_us/)
Terms (/pages/terms/)
Privacy Policy (/pages/privacy/)
Like Us
(https://www.facebook.com/interviewbit)
Follow Us
(https://twitter.com/interview_bit)
Email
(mailto:hello@interviewbit.com)
Got suggestions ? We would love to hear your feedback.