Commonly Asked C++ Interview Questions

Here we will see some important C++ interview questions.
What are the differences between C and C++?
What are the differences between C and C++?
What are the differences between pointers and references?
What are the differences between pointers and references?
The main differences between pointers and references are -
References are used to refer an existing variable in another name whereas pointers are used to store address of variable.
References are used to refer an existing variable in another name whereas pointers are used to store address of variable.
References cannot have a null value assigned but pointer can.
References cannot have a null value assigned but pointer can.
A reference variable can be referenced by pass by value whereas a pointer can be referenced but pass by reference.
A reference variable can be referenced by pass by value whereas a pointer can be referenced but pass by reference.
A reference must be initialized on declaration while it is not necessary in case of pointer.
A reference must be initialized on declaration while it is not necessary in case of pointer.
A reference shares the same memory address with the original variable but also takes up some space on the stack whereas a pointer has its own memory address and size on the stack
A reference shares the same memory address with the original variable but also takes up some space on the stack whereas a pointer has its own memory address and size on the stack
What is virtual function in C++?
What is virtual function in C++?
Virtual functions in C++ use to create a list of base class pointers and call methods of any of the derived classes without even knowing the kind of derived class object. Virtual functions are resolved late, at the runtime.
If A virtual function in a base class declared as once a member function, it becomes virtual in every class derived from that base class. So, use of the keyword virtual is not necessary in the derived class while declaring redefined versions of the virtual base class function.
 Live Demo
#include<iostream>
using namespace std;
class B {
   public:
   virtual void s() {
      cout<<" In Base \n";
   }
};
class D: public B {
   public:
   void s() {
      cout<<"In Derived \n";
   }
};
int main(void) {
   D d; // An object of class D
   B *b= &d; // A pointer of type B* pointing to d
   b->s(); // prints"D::s() called"
   return 0;
}
In Derived
What is this pointer in C++?
Every object in C++ has access to its own address through an important pointer called this pointer. The this pointer is an implicit parameter to all member functions. Therefore, inside a member function, this may be used to refer to the invoking object.
Friend functions do not have a this pointer, because friends are not members of a class. Only member functions have a this pointer.
Let us try the following example to understand the concept of this pointer −
 Live Demo
#include <iostream>
using namespace std;
class Box {
   public:
   // Constructor definition
   Box(double l = 2.0, double b = 2.0, double h = 2.0) {
      cout <<"Constructor called." << endl;
      length = l;
      breadth = b;
      height = h;
   }
   double Volume() {
      return length * breadth * height;
   }
   int compare(Box box) {
      return this->Volume() > box.Volume();
   }
   private:
   double length; // Length of a box
   double breadth; // Breadth of a box
   double height; // Height of a box
};
int main(void) {
   Box Box1(3.3, 1.2, 1.5); // Declare box1
   Box Box2(8.5, 6.0, 2.0); // Declare box2
   if(Box1.compare(Box2)) {
      cout << "Box2 is smaller than Box1" <<endl;
   } else {
      cout << "Box2 is equal to or larger than Box1" <<endl;
   }
   return 0;
}
Constructor called.
Constructor called.
Box2 is equal to or larger than Box1