Technical Interview
Python Java C++ C C# PHP R SQL DS Algo InterviewQ

C++ Interview Questions



Several jobs require candidates to have a profound knowledge of C++. These C++ Interview Questions have been designed specially to get you acquainted with the nature of questions that you may encounter during your interview for the subject of C++.

1. What is C++?

C++ is a middle-level and general-purpose programming language created by Bjarne Stroustrup starting in 1979 at Bell Labs. It is an extension to C programming with additional implementation of object-oriented programming (OOPs) concepts. It runs on a variety of platforms, such as Windows, Mac OS, and the various versions of UNIX. C++ is the most widely used programming languages in application and system programming.


2. What are the features of C++?

Some important features of C++ are given below:

  • Simple Language - C++ is a simple language. It is very easy to understand and learn.
  • Case-Sensitive Language - C++ is a case sensitive language and treats the uppercase and lowercase characters in a different manner.
  • Powerful & Fast Language - C++ is a fast language as it takes very less time in compilation and execution.
  • Object-Oriented Programming Language – C++ is an object oriented programming language. This is one of the most important feature of C++. C++ lets a user to implement real-time problems based on data abstraction, data encapsulation, inheritance, data hiding, and polymorphism.
  • Machine Independent Language – C++ can be interpreted on various operating systems including UNIX-based systems, Linux, Mac OS and various versions of Windows.
  • Mid-Level Language – C++ is a mid-level language and enables low-level language features like development of system applications such as kernel and drivers. It also supports the feature of high-level language. Hence C++ is a mid-level language.
  • Compiler Based Language – C++ is a compiler based language which means any C++ program can not be executed without compilation. Therefore, first of all the C++ program need to be compiled using compiler, after that the program can be executed.
  • Dynamic Memory Allocation - C++ supports the use of pointers which means a user can directly interact with memory and allocate memory dynamically.
  • Rich Library Support – C++ provides lots of inbuilt functions which makes programming faster and easier. These functions can be accessed by including appropriate header file in the C++ program.
  • Structured Programming Language – C++ is a structured programming language that means any C++ program can be achieved in parts using functions, classes and objects. This makes any C++ program easy to understand and modify.

3. What are the C++ access specifiers?

Access specifiers defines the access type of the members and functions of the class and there are three types of access specifier in C++.

  • public: members of the class are accessible from everywhere.
  • protected: members of the class are accessible within the class and by friend and inherited classes.
  • private: members of the class are accessible within the class and by friend classes.

4. What is a constructor?

A constructor is a special member function of a class which automatically executed when a new object of the class is created. It allows the class to initialize member variables and allocate memory.

The constructor function is declared just like a regular member function except the class name and function name should be same without any return type.

//Defining constructor inside the class
className(parameters) {
  statements;
}

//Defining constructor outside the class
className::className(parameters) {
  statements;
}

5. What is a destructor?

A destructor is a special member function of a class which destructs or deletes an object and automatically executed when the object goes out of scope. An object goes out of scope when:

  • the function containing object ends.
  • the program ends.
  • a block containing local object variable ends.
  • a delete operator is called for an object.

To create a destructor in C++, there are certain rules which need to be followed:

  • Destructor should begin with tilde sign(~) followed by class name.
  • A class can have only one destructor.
  • Unlike constructors that can have parameters, destructors do not allow any parameter.
  • Destructor do not have any return type, just like constructors.
  • When a destructor is not specified in a class, compiler generates a default destructor and inserts it into the code.
//Defining destructor inside the class
~className() {
  statements;
}

//Defining destructor outside the class
className::~className() {
  statements;
}

6. What is function overloading?

With function overloading feature in C++, two or more functions can have the same name but different parameters. It allows the programmer to write functions to perform conceptually the same thing on different types of data without changing the name.

In the example below, two function with the same name MyFunct are created. First function takes two integer numbers as arguments and returns sum of these numbers. The second function takes two float numbers as arguments and returns sum of these numbers.

#include <iostream>
using namespace std;
int MyFunct(int x, int y); 
float MyFunct(float x, float y);
 
int main (){
  int a = 100, b = 10;
  float p = 500.5, q = 50.05;

  cout<<"MyFunct(int, int) is used. Result: "
      <<MyFunct(a, b)<<"\n"; 
  cout<<"MyFunct(float, float) is used. Result: "
      <<MyFunct(p, q)<<"\n"; 

  return 0;
}

int MyFunct(int x, int y){
  return x+y; 
}

float MyFunct(float x, float y){
  return x+y; 
}
}

The output of the above code will be:

MyFunct(int, int) is used. Result: 110
MyFunct(float, float) is used. Result: 550.55

7. What is constructor overloading?

With constructor overloading feature in C++, two or more constructors can be created in the same class with different definitions like different number and types of parameters. The compiler automatically calls the constructor which matches the argument passed while creating the object.


5