Tutorial Study Image

C++ Interview Questions

C++ is an object-oriented programming language which is invented by Bjarne Stroustrup in 1985.

C++ is initially called 'C with classes' because C++ is considered a superset of the programming language C. C++ has included some additional features than C like the Class concept. 

C++ is a superset of programming language C that uses object-oriented concepts, and it has some benefits.

  • Applications created using C++ are highly portable, which means they can run on different platforms.
  • C++ uses many concepts like polymorphism, inheritance, classes, objects, etc because it is an Object-oriented programming language.
  • C++ has no redundant code snippets inside because of inheritance property.
  • C++ supports Data hiding that helps to hide the sensitive code parts from attackers.
  • Has a huge function library in C++

 

C++ is an object-oriented language because C++ is using the object-oriented concepts like Classes, Objects, Inheritance, Polymorphism, Data Binding, Abstraction, etc

C++ and C differences
C++ C
C++ is an object-oriented programming language that uses classes and objects. C programming is a procedure-oriented programming language.
C++ follows a Bottom-Up approach to programming  C is following a Top-down approach
C++ is a superset of C programming language C is a subset of C++
C++ is more secure as it supports data hiding that helps to prevent attacks C does not support data hiding and it is more susceptible to attack
C++ supports both function overloading and operator overloading C does not have this feature 
C++ contains more keywords than C programming language (52) C programming has only 32 keywords
C++ supports both friend function and virtual functions C does not support friend function or virtual function
C++ has the reference variables  C does not have a reference variable

 

A class in C++ can be defined as a user-defined data type that has some data members and data functions that are defined using private, public, or protected. 

In simple words, we can say that a class is a collection of data and its related functions that can be called using a single name. 

The class will be declared using the keyword "class"  and we can declare any number of classes in C++.

 

A class can be declared using the keyword class in C++, it can have some data members and related functions.

Class teachers

{

    // data members

    // Functions

}

Object in C++ is an instance of a class. An object is created from a class that has some data and some functions combined together to form a self-contained unit.

An object can able to represent an item or a person or some other item. An object can able to work on data and functions defined in the class. An object variable does not take any memory space and it is allocated in the heap memory.

In C++, we have three different methods to declare an object

  1. Using Default constructors
  2. Using Parameterized constructors
  3. Using copy constructors

 

Access modifiers in C++ are used to define the class member's accessibility. It defines how to access the class data which is outside the scope of the class. 

There are three types of access modifiers for a class, which are

  1. Public
  2. Private
  3. Protected

Inheritance is a property of Object-oriented language which helps to remove the code redundancy and provide the reusability of the code. With inheritance, we can use the properties of one class into another class, which means a new class is derived from the old class having the properties of old class. 

  • Derived class: The class that is derived from the old class is called the derived class.
  • Base Class: The old class is called the base class

 

Encapsulation is a method of binding the data of a class and its functions into a single wrap so that no outside functions can access the data members inside the class if we use the access modifiers as private to data members. 

Using encapsulation, only the functions inside the class can access the data of a class. Example: net banking login id and password.

Abstraction is a process of hiding non-essential details from the outside function. With abstraction, it shows only the details that are necessary, avoiding all the implementation details.

Example: suppose we are sending a message when we click on the send button. it shows the message has been sent only, it doesn't show the details of how it sends that message, which is an example of Abstraction.

 

Data binding in C++ is a process of wrapping the data and the related User interface and functions into a single name. 

As the name suggests, polymorphism means multiple forms. In C++, if there is more than one function with the same name but has different functionality we can call it polymorphism. 

Polymorphism is of two types

  1. Static polymorphism can be called early binding. It can be also called compile-time polymorphism, which means the polymorphism is implemented during the compile time. An example of static polymorphism is Method overloading.
  2. Dynamic polymorphism can be called late binding. it is also called run time polymorphism because its function call is implemented at run time. An example of dynamic polymorphism is function overloading.

There are three types of access modifiers in C++ which are

Private: Private member data of a class cannot be accessed by any function from outside that class. Even the child class functions cant able to access the parent class member data.

Protected: It is a little more accessible than private like the child class functions can access the member data of the parent class.

Public: Public class data can be accessible by any function throughout the program. Public class data members are globally accessible.

Basic data types used in C++ are

  • Bool: Boolean operations like Yes or No
  • Int: Integer values
  • Char: Character values
  • Float: Floating-point decimal values
  • Double: large size floating-point values

Derived data types 

  • Array
  • Pointer

Enumeration data type

  • enum

User-defined data type

  • Class
  • Structure

 

Namespaces are used to arrange the huge number of classes in a program to avoid confusion and ease to handle the program. 

Namespace helps the C++ programmer to remove the name conflicts of an identifier by keeping them in different namespaces.

Namespace helps to remove the ambiguity of the program code by keeping the different tasks of the same name in different namespaces.

Namespaces actually diving the program logically and it also helps define the scope of different identifiers like variables, class, etc

In C++, a token can be any of these like keyword, identifier, literal, constant, or symbol.

The keyword volatile in C++ is used to declare a variable is highly liable to change rapidly at any time. it tells the compiler that the value of such variables can change at any time.

Storage class in C++ is used to provide the visibility and the lifetime of a variable or a function in a program.

Visibility: Class or the part of code in which the data or a variable is accessible.

Lifetime: It is time in the C++ program that the variable is accessible and active.

In C++ there are five types of storage classes, they are

Auto: local variables declared and accessible  inside a function block with some garbage value initially

Register: local variables declared and accessible inside a function and have some junk values initially.

Extern: Global variables declared and accessible throughout the whole program and have zero values initially

Static: Local variable but accessible throughout the program and have zero values initially. 

Mutable: Local variable declared and accessible inside a class and have junk values initially.

In C++. if we want to run a specific set of code a finite or infinite number of times until a particular criterion is met, the loop function is used. The code snippet inside the loop function must be in a {} braces.

Types of loops in C++ are

  • While
  • For
  • Do-while

A function in C++ has the following parts to form an structure, they are

  1. Return type
  2. Function name
  3. Parameters
  4. Function body

Operators in C++ are a symbol or an operand that tells the compiler to do a specific mathematical or logical operation. 

In C++, there are different operators available such as

  • Arithmetic operators
  • Logical operators
  • Increment operators
  • Assignment operators
  • etc

The string is an array of characters that form words that are ended or terminated by a symbol '\0'

The reference variable in C++ is somewhat similar to the pointer concept in C programming. It can be defined as a variable declared as another name for the already existing variable.

The issue or a bug that becomes active while the program runtime is called an exception and handling that error is exception handling.

In C++, we have Try Catch and Throw method for doing the exception handling.

  • Try: help us to locate the code part where the exception is active
  • Catch: it will help to catch the exception using the exception handler.
  • Throw: this will help to throw an exception if an error happens while executing the program.

In C++ there are five types of functions available, which are

  • Simple functions
  • Static functions
  • Const functions
  • Inline functions
  • Friend functions

Decision-making statements are conditional statements that help to make a decision inside a program. C++ supports

  • if statement
  • switch statement
  • Conditional operators

In C++, the structure is almost similar to the class concept but has some differences from class

Class Structure
Inside a class, all the member data are private as the default condition In the case of structure, all the data members are of public as default.

 

 

Operator overloading is an example of static polymorphism and it is very essential for doing operations on user-defined data types.

With the help of operator overloading, we can assign special meaning to an operator that is working with user-defined data types.

A constructor is a function that is created automatically when an object is created with the same name as the class in which it is a member. 

Constructors have no return value and there are three types of constructors available

  • Default constructor
  • Parameterized constructor
  • Copy constructor

We know in C++ the parent class is called the base class and the chile class is called the derived class. A virtual function is a function that is inside the base class which is expected to redefine in the derived class to create polymorphism.

A virtual function is declared using the keyword virtual.

When a class is declared as a friend, it can access all the member data of the friend class, which is of any access modifier like private, public, or protected. 

A friend function is similar to a friend class in which a friend function can access all the public, private and protected members of the friend functions.

If we use an inline function in C++, the compiler will place that function code in the code where it finds that function call during the compile time.

The main use of the inline function is to avoid the function call procedure while executing the program.

Destructors in C++ are functions that we call when we need to delete an object when the function is closed or the program ends. Destructors are functions the helps to delete or remove an object.

Destructors works opposite of constructors which helps to create a object. Like constructors, destructoes are also called automatically.

 

A static member in C++ is a class member that is declared using the keyword static. Static members have only one copy and can be accessed by all the objects, irrespective of how many objects have been created from that class. If a member of a class is defined as static, then its memory is allocated until the program ends. 

Multithreading defines as the ability to do more than one process in parallel. Multithreading ability makes the programming language more efficient and powerful. There are two types of multithreading in C++, that are

Process-based: In this type of multithreading it supports the execution of more than two programs in parallel.

Thread-based: In this type of multithreading, it supports more than two threads of a single program to run in parallel. It splits the program into threads.

Upcasting in C++ is the process of converting a derived class reference variable or a pointer to a base class reference variable or a pointer.

Downcasting in C++ is the opposite of upcasting, it converts a base class reference variable or a pointer to a derived class pointer or a reference variable. downcasting is a comparatively more difficult process than upcasting.

Before the compilation begins, there are some directives that start with the '#' sign to give some commands to the compiler about some information that the compiler has to pre-process are called preprocessors. 

Copy constructor in C++ is an overloaded constructor which helps to copy the member data of an object to another object. which means a copy constructor is used to initialize an object using another object of the same class.

'==' operator is called the "Equal to" operator which checks whether the left-hand side value and right-hand side value are the same or not. If it is the same it returns true else it returns false. 

'=' is called the "Assignment operator", which assigns the right-hand side value to the left-hand side variable.

Both are looping structures in C++ which are used to execute a bit of code for a definite or indefinite number of times until a condition is satisfied. But the difference between a while loop and a do-while loop is that

  • A while loop is an entry-controlled loop which means the condition is checked before entering into the loop. If the condition is not satisfied loop will not execute even a single time.
  • Do while is an exit controlled loop, which means the condition is checked only after the execution of the loop. In this case, Loop is executed once even if the condition is not satisfied.

 

Prefix and postfix expressions are depending on how the increment is doing to an expression, is it before or after the assignment of value.

  • Prefix: in the Prefix expression, the increment is done before the value is assigned to the expression. For example ++i.
  • Postfix: In the postfix expression, the increment is done after the value is assigned to the expression. For example i++.

In C++, when using the call by value method of the function call, the copy of actual parameters is passed into the formal parameters inside a function during a function call.

While doing the call by value method, the actual parameters are safe and not change while the formal parameters inside the function change.

 

Pointers in C++ are variables that contain the address of some other variables. Pointers are also known as locators of another variable.

Pointers in C++ have the data type and it must be matched with the variable data type that the pointer points to. For example, an integer pointer can point to an integer data type variable.

Scope resolution operator which is represented by the symbol "::" is used for linking a function to its class. it is also used for using a global variable if there is a local variable also present with the common name as a global variable. The scope resolution operator is also used to define a function that is outside a class.

A mutable class specifier in C++ is used to change the member variable values of a class even if its object is a constant type during execution time.

In simple words, mutable object values can be changed anytime even after being created.

In C++, Shallow copying is done by copying all the member data intact and creating a copy of the object.

Copying all the member data and the memory locations of resources that are not in that object but are handled by that object are called Deep copying.

In C++ a function is said to be a pure virtual function only if the function has no body and is assigned a value of zero.

A class in C++ is said to be an abstract class if that class has at least one pure virtual function in it. 

In C++, we can store wide characters using the data type wchar_t. 

Function overloading can be described as two or more functions with the same name but having different parameters in the same scope. Such functions are supported by C++ and are called Function overloading.

Standard streams are used for input and output related operations in C++, that are

  • cin
  • cout
  • cerr
  • clog

In C++, there are different ways to allocate dynamic memory, and using the 'new' operator we can allocate dynamic memory.

'delete' operator helps to de-allocate the dynamic memory that is made while using the 'new' operator. 

'new' operator is used to allocate memory space dynamically.

This pointer in C++ is a pointer that holds the address of the currently active object. This pointer points to the current running instance of the class

A block scope variable in C++ is a variable that has the scope only in a block. We can declare a variable anywhere in the block.

Command-line arguments in C++ are the arguments we provide to the main program from the command line in the form of strings, during run time. 

The base address of an array in C++ is the starting address of the array which is denoted by array[0]. 

Suppose we need a variable that is used so frequently, then it must be declared using the register storage specifier.

When a variable is declared as a register storage specifier, it is stored in CPU registers that can be accessed quickly during the program running.

A class in C++ is called a container class if it holds at least one variable of another class type in it.

In C++ we can pass the arguments into the main program from the command line while the program is running is called command-line arguments.

In C++, the sizeof operator is used to calculate the size of a data type or a class or an object.

In C++, there are limited operations available on pointers which are 

  1. Mathematical operations Addition and Subtraction
  2. Comparision operations

Recursion in C++ is defined as a function that calls itself for a finite or infinite number of times.

delete is used to delete or deallocate a memory space whereas delete[] is used to delete or deallocate memory locations allocated for an array.

cin in C++ is an istream class object that is used to accept input from the user using standard input devices like keyboard etc. 

cout is an object of istream class of C++ which is used to display the output to the user, using the standard output devices like monitor etc.

The actual parameter is the actual values or parameters that have to pass to the calling function when the function is called. 

The formal parameters are the parameters in the calling function which accept the actual parameter when a function call happens.

$undef in C++ is used to undefine a macro definition that already exists. 

In C++, STL is the short form of Standard Template Library.

  • Arrays are a collection of data that is of the same data type. whereas a List is a collection of data that is of different data types.
  • Memory allocation for an Array is static whereas the List memory allocation is dynamic.

 

For exporting functions from DLL there are two ways, 

  • Using the DLL's type library
  • take reference to function from DLL

Overflow error occurs in some mathematical calculations, it happens when the result of the mathematical operations is greater than the memory allocated for that result. 

A void pointer in C++ is a reference variable that has no data type associated with it. A void pointer can point to any data type.

STL in C++ has four components that are

  • Containers
  • Algorithms
  • iterators
  • Functions

A class template can be defined as a method or a prototype to create a generic class or a function. We are using the keyword template for defining the class template.

Comments are the piece of written information in the code segments that are neglected by the compiler but helpful for the programmer to understand the purpose of that code segment. Comments are written using '#' in C++.

The scope of a variable is defined as the part of code where that variable is declared and accessible.

  • Local Scope: A variable is defined and accessed inside a class or a block of code is called a local scope variable. This variable cannot be accessed from anywhere else.
  • Global Scope: If a variable is accessible from anywhere in the program irrespective of the class or a block of code, it is said to have a global scope.

A constant in C++ is defined using the preprocessor directive '#define'.

We can use the "using" declaration if we want to refer to a member's name from the namespace without using the scope resolution operator.

A conversion constructor is a constructor that accepts an argument of a different type and it is used to convert one type to another

An explicit constructor is a conversion construction declared using the keyword "explicit". When we declare an explicit constructor, the compiler reserves that explicitly for conversion purposes. it is not used for implementing the implied conversion of types.

Operators which cannot be overloaded are,

sizeof : size of operator

  • . : Dot operator
  • .* : Dereferencing operator
  • -> member dereference operator
  • :: : Scope resolution operator
  • ?: : Conditional operator

C++ programming language is created by Bjarne Stroustrup in 1985. C programing language is a subset of C++ and C++ is a superset of C programming language.