Commonly Asked C++ Interview Question

  1.  What are the differences between C and C++? 

Ans:

CC++
C is a procedure-oriented programming language.C++ is an object-oriented programming language.
Function and operator overloading are not supported in CFunction and operator overloading is supported in C++
calloc() and malloc() functions are used for memory allocation and free() function is used for memory deallocation.new operator is used for memory allocation and deletes operator is used for memory deallocation.
C does not support data hiding.Data is hidden by encapsulation to ensure that data structures and operators are used as intended.
Basic Difference between C and C++

2.  What are access modifiers?

Ans: Access modifiers (or access specifiers) are keywords in object-oriented languages that set the accessibility of classes, methods, and other members. Access modifiers are a specific part of programming language syntax used to facilitate the encapsulation of components. In C++, there are only three access modifiers.

  • Private
  • Public
  • Protected

3. What do you understand about polymorphism in C++?

Ans: Polymorphism is a feature of object-oriented programming languages that allows a specific routine to use variables of different types at different times. Polymorphism in programming gives a program the ability to redefine methods for derived classes.

4. What is a constructor in C++?

Ans: A constructor in C++ is a special method that is automatically called when an object of a class is created.It is used to initialize the data members of new objects generally. The constructor in C++ has the same name as the class or structure. 

5. Define inline function in C++

Ans: Inline function in C++ is an enhancement feature that improves the execution time and speed of the program. The main advantage of inline functions is that you can use them with C++ classes as well.

6.What are the three different types of C++ access specifiers?

Ans:

  • Public: All member functions and data members are accessible outside the class.
  • Protected: All member functions and data members are accessible within the class and to the derived class.
  • Private: All member functions and data members cannot be accessed outside the class. 

7. What do you mean by call by value and call by reference?

Ans :

  • In call by value method, we pass a copy of the parameter is passed to the functions. For these copied values a new memory is assigned and changes made to these values do not reflect the variable in the main function.
  • 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.

8. Explain inheritance

Inheritance is a feature or a process in which, new classes are created from the existing classes. The new class created is called “derived class” or “child class” and the existing class is known as the “base class” or “parent class”. The derived class now is said to be inherited from the base class.

9. If class D is derived from a base class B. When creating an object of type D in what order would the constructors of these classes get called?

Ans :

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.

10.  Can we call a virtual function from a constructor?

Ans : Yes, we can call a virtual function from a constructor. But the behavior is a little different in this case. When a virtual function is called, the virtual call is resolved at runtime. It is always the member function of the current class that gets called. That is the virtual machine doesn’t work within the constructor.

Share your love
Saransh Saurav

Saransh Saurav

Articles: 68

Leave a Reply

Your email address will not be published. Required fields are marked *