C++ Interview Questions
Komal Singh
Posted on October 12, 2020
What is a constructor
A constructor is a member function of a class that initializes objects of a class. In C++, Constructor is automatically called when an object(instance of a class) is created.
There are three types of constructors:
- Default Constructor: Constructor with no parameters/arguments
#include <iostream>
using namespace std;
class A {
public:
int a, b;
// Default Constructor
A()
{
a = 1;
b = 2;
}
};
int main()
{
// Default constructor called automatically
// when the object is created
A obj;
cout << "a: " << obj.a << endl
<< "b: " << obj.b;
return 1;
}
Output:
a: 1
b: 2
The compiler automatically provides a default constructor even if we do not initialize one.
- Parameterized Constructor: Constructor with Parameter/Arguments
#include <iostream>
using namespace std;
class ParamA {
private:
int b, c;
public:
ParamA (int b1, int c1)
{
b = b1;
c = c1;
}
int getX ()
{
return b;
}
int getY ()
{
return c;
}
};
int main ()
{
ParamA p1(10, 15);
cout << "p1.b = " << p1. getX() << ", p1.c = " << p1.getY();
return 0;
}
Output: p1.b = 10, p1.c = 15
- Copy Constructor: A copy constructor is a member function that initializes an object using another object of the same class.
#include<iostream>
using namespace std;
class test
{
private:
int x;
public:
test(int x1)
{
x = x1;
}
test(const test &t2)
{
x = t2.x;
}
int getX()
{
return x;
}
};
int main()
{
test t1(7); // Normal constructor is called here
test t2 = t1; // Copy constructor is called here
cout << "t1.x = " << t1.getX();
cout << "t2.x = " << t2.getX();
return 0;
}
t1.x = 7
t2.x = 7
How constructors are different from a normal member function?
A constructor is different from normal functions in the following ways:
A constructor has the same name as the class name
Constructors don’t have a return type
A constructor is automatically called when an object is created.
If we do not specify a constructor, the C++ compiler generates a default constructor.
Constructor Overloading in C++
Two or more constructors in a class with the same name but a different list of arguments is known as Constructor Overloading.
A constructor is called depending upon the number and type of arguments passed. While creating the object, arguments must be passed to let the compiler know the constructor that is needed to be called.
What is a destructor?
Destructor is a member function that destructs or deletes an object.
A destructor function is called automatically when the object goes out of scope:
(1) the function ends
(2) the program ends
(3) a block containing local variables ends
(4) a delete operator is called
Destructors have the same name as the class preceded by a tilde (~)
Destructors don’t take any argument and don’t return anything.
NOTE
The compiler creates a default destructor for us if we do not write our own destructor in class. The default destructor works fine unless we have dynamically allocated memory or pointer in class. When a class contains a pointer to memory allocated in class, we should write a destructor to release memory before the class instance is destroyed. This must be done to avoid Memory Leak.
Part 1: Series of C++ interview Questions
Will be adding some more C++ based interview questions. Do suggest if you have come across some interesting C++ questions in interviews :)
Posted on October 12, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.