Constructors in C++
Constructors are the first thing we come across when learning about OOPs Concepts. I have tried to summarize constructors for you.
Object-Oriented Programming is a paradigm of programming which is based on the concepts of "Objects". It is a style of programming that revolves around data or objects rather than function and logic.
We have a "Class" which resembles a real-world entity and every object is an instance of that class.
Inside Class, we define methods and data specific to that class. Methods resemble functions and are called Member functions whereas data are similar to variables and are called Member variables. Now that we are clear with basic terminologies, we can now proceed with Constructors and Destructors.
CONSTRUCTORS:
Constructors are special methods. They are called every time (by themselves) whenever an instance of this class is created. Even if we do not define a constructor specifically, the compiler itself creates a constructor. Now the question arises, why do we need constructors? So, we need constructors to set the values of the variables which we have inside the class. If we do not have any constructor, then some default value is assigned to the variables depending on the data type of the variables.
In the above image, we can see that the constructor runs every time an object is instantiated. Also, you can notice that I have written "DEFAULT CONSTRUCTOR" rather than the constructor. This is where I come to my next point, another type of constructor, ### Parameterized Constructor.
Parameterized Constructor:
One issue which we can have with default constructors is that every instance will have the same value for the data(variable). This is true for "wheels" as every car has two wheels but what about colors? Cars can have different colors, right? So how do we achieve this? To do this, we can pass a parameter "color" to constructor and set different colors to different cars.
Let's see this with the example.
include <iostream>
using namespace std;
class Car {
public :
int wheels;
string color = "black";
// DEFAUL CONSTRUCTOR
public:
Car (string col){
cout << "Second CONSTRUCTOR" << endl;
color = col;
}
// PRIVATE CONSTRUCTOR
Car () {
cout << "CONSTRUCTOR CALLED" << endl;
wheels = 4;
}
void getWheels() {
cout << wheels << endl;
return ;
}
void getColor() {
cout << color << endl;
return;
}
};
int main()
{
Car c1;
c1.getWheels();
Car c2("red");
c2.getColor();
return 0;
}
OUTPUT :
So here we get the color set to RED. We need to keep in mind that though the name of the Constructor should be the same as that of the class, we can have many constructors in a single class. This is because of constructor overloading. The only thing which we have to make sure of is that each Constructor must have different numbers of parameters.
Also, a key point you should remember is , if you have a parameterized constructor in a class, then you have to declare the default constructor yourself.
So, If I write down for you, the key points you should remember are:
- Constructure are special member functions.
- Their name is the same as that of the Class name.
- They are called every time an object of the class is instantiated.
- They are used to initialize the value of the member variable.
- We can have as many constructors as we want, the only condition is the number of parameters should be different.
Till now we have learned about two types of constructors. There is also a third type of constructor called Copy Constructor which I will discuss in my next blog.