Table of Contents

Top 30 C++ Interview Questions

1.What are C++ access specifiers ?

Ans. Access specifiers are used to declare the scope of the members(functions and variables) on how they are accessible outside the class. This is written before the name of the members when they are defined.

  • Private: The members declared as private cannot be accessed outside the class. They are only accessible in the same class where they are defined. They are also not accessible in the child classes.
  • Public: Public members are accessible from anywhere.
  • Protected: Members declared as protected cannot be accessed from outside the class, however, they can be accessed in inherited classes.

2. Give differences between structure and classes?

  • The following are the basic differences between structure and class:
    • Members inside the struct are public by default and the members inside the class are private by default.
    • When deriving a struct from a class/struct, default access-specifier for a base class/struct is public. And when deriving a class, default access specifier is private.

3. Define Friend class and function in C++.

Ans. Any class declared as friend in other class, can access the private and protected members of that class. A Friend Function like friend class, also allowed to access private and protected members. A friend function can be:

  1. A method of another class
  2. A global function

Though friend class and function can access the members of other classes in which they are declared, but the opposite is not true. Friendship is not mutual. If Class A is a friend of B, then it is not like class B is also the friend with A. Also, Friendship is not inherited.

4. What is a copy constructor ?

Ans. A copy constructor is a member function which initializes an object using another object of the same class, which has been created already. A copy constructor has the following general function prototype:

ClassName (const ClassName &old_object);

A copy constructor is used to copy an object to pass it as an argument to a function and copy an object to return it from a function. A copy constructor may be called when the compiler generates a temporary object.

5. Can we make copy constructor private?

Ans. Yes, a copy constructor can be made private.

6. What is a mutable storage class specifier? How can they be used?

Ans. A mutable storage class specifier is used for changing the constant class object’s member by declaring it. It can be applied only on non-static and non-constant member variable of the class.

7. What are the functions of the scope resolution operator?

Ans. The following are the functions of the scope resolution operator:

  1. It helps in resolving the scope of various global variables.
  2. It helps in associating the function with the class when it is defined outside the class.

8. Define a class template?**

Ans. A class template provides a specification for generating classes based on parameters. Class templates are generally used to implement containers. A class template is instantiated by passing a given set of types to it as template arguments.

9. What is the function of the keyword “Auto”?**

Ans. The function of the the keyword “Auto” is to make a function to work automatically and it is used by default for various local variables.

10. What is Inheritance?

Ans. Inheritance is the method of reusing the features of one class by the other class. The class from which the features is inherited is known as the base calls and the new class which is getting the features is known as derived class. It eliminates the redundancy of the code and provides reusability.


class derived_class :: visibility-mode base_class;

11. What is the difference between new() and malloc()?

Ans. new() is a preprocessor while malloc() is a function.There is no need to allocate the memory while using “new” but in malloc() you have to use sizeof(). “new” initializes the new memory to 0 while malloc() gives random value in the newly allotted memory location.The new() operator allocates the memory and calls the constructor for the object initialization and malloc() function allocates the memory but does not call the constructor for the object initialization. The new() operator is faster than the malloc() function as operator is faster than the function.

12. What is C++?

Ans. C++ is an object-oriented programming language created by Bjarne Stroustrup. It was released in 1985.C++ is a superset of C with the major addition of classes in C language.

Before it was called as “C with classes” but after sometime the name was changed to “C++”.

13. What are the advantages of C++?

C++ doesn’t only maintains all aspects from C language, it also simplifies memory management and adds several features like:

  • C++ is a highly portable language means that the software developed using C++ language can run on any platform.
  • C++ is an object-oriented programming language which includes the concepts such as classes, objects, inheritance, polymorphism, abstraction.
  • C++ has the concept of inheritance. By this feature, redundancy of the code is eliminated as one class can reuse the functionalities of the other class.
  • Data hiding helps the programmer to build secure programs so that the program cannot be attacked by the invaders.
  • Message passing is a technique used for communication between the objects.
  • C++ contains a rich function library.

14. What is the difference between reference and pointer?

Ans. Reference is referring to another variable while the pointer is storing the address of a variable.

Reference behaves like an alias for an existing variable, i.e., it is a temporary variable.The pointer is a variable which stores the address of a variable.

Once the reference variable is assigned, then it cannot be reassigned with different address values.The pointer variable is an independent variable means that it can be reassigned to point to different objects.

References do not make a change in an original variable while if the pointer is changed it does affect the original variable.

A reference must be initialized on declaration while it is not necessary to initialize a pointer once it is declared.

An array of pointers can be created while an array of references cannot be created.

A null value cannot be assigned to a reference but it can be assigned to a pointer.

15. What is a class?

Ans. The class contains the data members and member functions whose access is described by three modifiers, private, public, protected. A class is a uder defined data type. It defines a datatype, but it does not define the data it just specifies the structure of data.The class does not occupy any memory space. Therefore, we can say that the class is the only logical representation of the data.

class ClassName {
    //Data members
    //Member functions

16. What is an Object?

Ans. An object is a run-time entity. An object is the instance of the class. An object can represent a person, place or any other item. An object can operate on both data members and member functions. The class does not occupy any memory space. When an object is created using a new keyword, then space is allocated for the variable in a heap, and the starting address is stored in the stack memory. When an object is created without a new keyword, then space is not allocated in the heap memory, and the object contains the null value in the stack.

 class Employee
  //data members;
  //Member functions

The syntax for declaring the object:

Employee e = new Employee();

17. Define encapsulation.

Ans. Encapsulation is a technique of wrapping the data members and member functions in a single unit. It binds the data within a class, and no outside method can access the data. If the data member is private, then the member function can only access the data.

18. Define abstraction.

Ans. Abstraction is a technique in which we can only show the relevant details to the user and hide the irrelevant details from the user. Data abstraction is a programming technique that depends on the seperation of the interface and implementation details of the program. If the members are defined with a public keyword, then the members are accessible outside also. If the members are defined with a private keyword, then the members are not accessible by the outside methods.

19. Explain Data binding.

Ans. Data binding is a general technique that binds data sources from the provider and consumer together and synchronizes them. Data binding is of two types: static or early binding and dynamic or late binding.

20. Define Polymorphism?

Ans. Polymorphism is technique of taking more than one form. In C++, polymorphism is the feature through which any function can take more than one form, that means it can defined multiple times with the same name but having diferent functionalities whenever it is defined. Polymorphism allows the object to behave differently in different conditions. In C++ there are two types of polymorphism: 1) Compile time Polymorphism – This is also known as static (or early) binding.
2) Runtime Polymorphism – This is also known as dynamic (or late) binding.

21. What are the different types of polymorphism in C++?

Ans. Polymorphism: Polymorphism means multiple forms. It means having more than one function with the same function name but with different functionalities.

Polymorphism is of two types:

  • Runtime polymorphism

Runtime polymorphism is also known as dynamic polymorphism. Function overriding is an example of runtime polymorphism. Function overriding means taking a function from the parent class which is already defined in the parent class by the child. Hence, the child class overrides the method of the parent class. In case of function overriding, parent and child class both contains the same function with the different definition. The call to the function is determined at runtime is known as runtime polymorphism.

  • Compile time polymorphism

Compile-time polymorphism is also known as static polymorphism. The polymorphism which is implemented at the compile time is known as compile-time polymorphism. Method overloading is an example of compile-time polymorphism. Method overloading is a technique which allows you to have more than one function with the same function name but with different functionality.

Method overloading can be possible on the following basis:

  • The return type of the overloaded function.
  • The type of the parameters passed to the function.
  • The number of parameters passed to the function.

22. Define namespace in C++.

Ans. The namespace is a logical division of the code which is designed to stop the naming conflict. The namespace defines the scope where the identifiers such as variables, class, functions are declared. Multiple namespace blocks with the same name are allowed. All declarations within those blocks are declared in the named scope.The main purpose of using namespace in C++ is to remove the ambiguity. Ambiquity occurs when the different task occurs with the same name. For example: if there are two functions exist with the same name such as add(). In order to prevent this ambiguity, the namespace is used. Functions are declared in different namespaces. C++ consists of a standard namespace, i.e., std which contains inbuilt classes and functions. So, by using the statement “using namespace std;” includes the namespace “std” in our program.

  • Syntax of namespace:
  1. namespace namespace_name
  2. {
  3. //body of namespace;
  4. }

Syntax of accessing the namespace variable:

  1. namespace_name::member_name;

23. What is a destructor?

Ans. A Destructor is used to delete any extra resources allocated by the object. A destructor function is called automatically once the object goes out of the scope. Destructors have the same name as class name and it is preceded by tilde. It does not contain any argument and no return type.

24. What is an overflow error?

Ans. An overflow error is a type of arithmetical error. It happens when the result of an arithmetical operation been greater than the actual space provided by the system.

25. How delete [] is different from delete?

  • Delete is used to release a unit of memory
  • delete[] is used to release an array.

27. What is the difference between an array and a list?


  • An Array is a collection of homogeneous elements while a list is a collection of heterogeneous elements.
  • In Array, users don’t need to keep in track of next memory allocation while In the list, the user has to keep in track of next location where memory is allocated.
  • Array memory allocation is static and continuous while List memory allocation is dynamic and random.

28. What is a storage class?

Ans. A class that specifies the life and scope of its variables and functions is called a storage class. In C++ following the storage classes are supported: auto, static, register, extern, and mutable. However, that the keyword register was deprecated in C++11. In C++17, it was removed and reserved for future use

29. What is a virtual function?

Ans. A virtual function is used to replace the implementation provided by the base class. A virtual function is a member function which is present in the base class and redefined by the derived class. When we use the same function name in both base and derived class, the function in base class is declared with a keyword virtual. When the function is made virtual, then C++ determines at run-time which function is to be called based on the type of the object pointed by the base class pointer. Thus, by making the base class pointer to point different objects, we can execute different versions of the virtual functions.

Rules of a virtual function:

  • The virtual functions should be a member of some class.
  • The virtual function cannot be a static member.
  • Virtual functions are called by using the object pointer.
  • It can be a friend of another class.
  • C++ does not contain virtual constructors but can have a virtual destructor.

30. What Is The Difference Between A Copy Constructor And An Overloaded Assignment Operator?

Ans. A copy constructor constructs a new object by using the content of the argument object. An overloaded assignment operator assigns the contents of an existing object to another existing object of the same class.

Scroll to Top