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 :)
Top comments (4)
Hey there!
Some comments :)
You are missing a 4th type: move constructor. It has been around for almost 10 years now!
This is partially true. They are rules to decide if the default constructor can be implicitly defined by the compiler. It is possible that you have to explicitly define it. See en.cppreference.com/w/cpp/language..., section Implicitly-declared default constructor.
Since C++11, smart pointers are here for that. More generally, RAII solves this kind of issues. See en.cppreference.com/w/cpp/language...
Thanks a lot, Pierre!! I never came across Move Constructor, Got this know this because of you. Thank you so much!
You don't use them everyday, don't worry ;)
They are part of the move semantic that was introduced with C++11. This is not something easy but it can increase performances in some situations.
Note that move constructor pairs with move assignment operator (just like copy ctor pairs with copy assignment operator).
You can check this link: thbecker.net/articles/rvalue_refer...
Hello, this was super informative and helpful. Here are a few more questions that could be taken as a reference from InterviewBit
1 - What are the different data types present in C++?
2 - What are class and object in C++?
3 - Compare compile-time polymorphism and Runtime polymorphism
4 - What are the static members and static member functions?
5 - What is the difference between virtual functions and pure virtual functions?