Chapter Twelve: Constructors in C++
As mentioned earlier, a constructor is a function created in a class when you create an object. This function is used to initialize the object in a class. A constructor is termed as a member function in every class. Before we look at the details of what constructors are, let us understand the difference between constructors and member functions. The following are some differences between constructors and member functions in a class:
●
A constructor, unlike a member function, will have the same name as the class
●
There is no return type for a constructor
●
When you create an object in a class, the compiler automatically creates a constructor. You need to create a member function separately if you want to perform any operations.
●
You need not define any constructor in the code since the compiler automatically generates one with no body or parameters
Let us understand what a constructor is better using an example. Let us assume you went to the store to purchase a pen. Do you consider all the options available when you choose to buy a pen? The first thing you do is think about the store you want to go to. Once you reach the store, you ask the shopkeeper to give you a pen. When you ask for just a pen, it indicates you have not thought about the brand you want to use or which color you prefer. The shopkeeper will hand you a pen or give you a pen that people have bought frequently. This is exactly what a default constructor in your class is.
The other option you have is to go to the store and let the shopkeeper know you want a blue color pen sold by ABC brand. When you mention this to him, he will hand the exact product to you. The shopkeeper knows what you want because you gave him the parameters. This is an example of a parameterized constructor.
The last option is to take a pen you have at home and show the shopkeeper a physical copy of the pen and ask for the same thing. The shopkeeper will give you exactly that pen. This new pen is a copy of the pen you own. This is how a copy constructor works.
Constructor Types
The following are the types of constructors we discussed previously.
Default Constructors
A default constructor is one that does not use any parameters or arguments. There is also no function or operation defined within this constructor.
Consider the following example:
// This program is an example of a default constructor
#include <iostream>
using namespace std;
class construct
{
public:
int a, b;
// Default Constructor
construct()
{
a = 10;
b = 20;
}
};
int main()
{
// Default constructor called automatically
// when the object is create
d
construct c;
cout << "a: " << c.a << endl
<< "b: " << c.b;
return 1;
}
On compiling the code, you will receive the following output:
a : 10
b : 20
It is important to note that the compiler always defines a constructor in the code when you create an object in the class.
Constructors with Parameters
Constructors are like functions in the sense that you can pass arguments or parameters. These parameters or arguments allow you to declare and initialize an object in the class when you create it. If you want to create a constructor that accepts parameters and arguments, you need to add them to the constructor, similar to how you would add them to functions. When you define the body of the constructor, you should use the parameters or arguments to initialize or instantiate the objects in the class.
Consider the following example:
// This example is used to create a constructor with parameters and arguments
#include <iostream>
using namespace std;
class Point
{
private:
int x, y;
public:
// Parameterized Constructor
Point(int x1, int y1)
{
x = x1;
y = y1;
}
int getX()
{
return x;
}
int getY()
{
return y;
}
};
int main()
{
// Constructor called
Point p1(10, 15);
// Access values assigned by constructor
cout << "p1.x = " << p1.getX() << ", p1.y = " << p1.getY();
return 0;
}
When you run the above code, you will obtain the following output:
p1.x = 10, p1.y = 15
When you use a parameterized constructor to create or declare an object, you need to pass the values you want to assign the object as an argument or parameter in the function. The compiler will not allow you to declare and assign a value to an object normally. Therefore, you need to call a constructor, and you can either do this implicitly or explicitly.
Consider the following example:
Example e = Example(0, 50); // Explicit call
Example e(0, 50); // Implicit call
Uses of Parameterized Constructors
Parameterized constructors can be used for the following:
-
You can use this constructor to initialize different data elements in the code with different objects. Each of these objects can be assigned different values depending on when they are created.
-
You can use this function for constructor overloading. This method is similar to the process of overloading discussed above.
Copy Constructors
Copy constructors are member functions using which you can initialize an object in the classes. You can do this by using other objects in the same class. We will look at these in further detail later in the book.
When you define more than one constructor in the class with parameters, you also need to declare a default constructor without parameters. This should be declared explicitly in the class. The compiler will not create a default constructor if you have defined a constructor in the code. It is, however, not required for you to always declare a default constructor. However, this is the best practice.
// Example to create a copy constructor
#include "iostream"
using namespace std;
class point
{
private:
double x, y;
public:
// Non-default Constructor &
// default Constructor
point (double px, double py)
{
x = px, y = py;
}
};
int main(void)
{
// Define an array of size
// 10 & of type point
// This line will cause error
point a[10];
// Remove above line and program
// will compile without error
point b = point(5, 6);
}
The following is the output when the code above is compiled:
Error: point (double px, double py): expects 2 arguments, 0 provided