The article cover’s most commonly asked interview questions in c++ and a brief explanation of each question.

1. What is OOPS?

Object-oriented programming is a programming paradigm based on the concept of “objects” of the classes, Objects may contain data in the form of fields and associated code in the form of methods. The objects can access their own procedures and can modify their data fields.

2. What is function overloading, how did it make the lives of C++ programmer better than C programmer?

In C++ it’s allowed to make functions with the same name in the same scope. The functions with the same name are called overloaded functions. The difference that must exist between the two functions with the same name is the parameters that are being passed. Function overloading can be done by using different parameter types and changing the number of arguments. C, on the other hand, doesn’t support function overloading which reduces the code quality and readability.

3. How is function overloading achieved?

The program below has “add” function with two different implementations. One for double data type numbers and another for integers. The function has the same name but different parameters so it’s a clear example of function overloading.

#include<bits/stdc++.h>
using namespace std;
int add(int a, int b){//Add Integers
    return a+b;
}
double add(double a, double b){//Add Double
    return a+b;
}
int main(){
    cout<<"Integer Type Numbers after addition : "<<add(1,2)<<"\n";
    cout<<"Double Type Numbers after addition : "<<add(1.1,2.2);
}
Output:
Integer Type Numbers after addition : 3
Double Type Numbers after addition : 3.3

4. what is dynamic binding?

To Understand Dynamic binding let’s see what are virtual functions and what binding means.

Binding refers to the process of converting identifiers such as variable and function calls into addresses by the compiler.

**Virtual Function **in c++ is a function which is defined in“Base” class and overridden in “Derived” class. It is made by adding “virtual” keyword in the function definition.

Dynamic Binding: In C++ when the compiler is not able to decide which function to call at compile time but computes the same at runtime then this type of binding is known as dynamic binding.

An example of Dynamic Binding is when a virtual function is defined in “Base” class and overridden in “Derived” class, and this function is called with the help of pointer of the base class that stores the address of the object of “Derived Class”.

#include<bits/stdc++.h>
using namespace std;
class Base{
    public:
        virtual void print(){
            cout<<"Base Class";
        }
};
class Derived : public Base{
    public:
        void print(){
            cout<<"Derived Class";
        }
};
int main(){
    Base *ptr;
    Derived d;
    ptr=&d;
    ptr->print();
}
Output : Derived Class

In the above program when the compiler compiles the program a pointer named “ptr” is made which is initialized by the address of an object of a derived class and this address is only available at compile time. With this information, the compiler is able to know which print function is being referenced by the programmer so as a result derived class “print” function is called.

#cpp17 #most-asked-question #technical-interview #c++ #programming-c #cplusplus

Complete C++ Interview Questions & Answers
5.50 GEEK