Constructors in C++
Constructors in C++
What is constructor?
A constructor is a member function of a class which initializes objects of a class. In C++,
Constructor is automatically called when object (instance of class) create. It is special member
function of the class.
How constructors are different from a normal member function?
A constructor is different from normal functions in following ways:
• Constructor has same name as the class itself
• Constructors don’t have return type
• A constructor is automatically called when an object is created.
• If we do not specify a constructor, C++ compiler generates a default constructor for us
(expects no parameters and has an empty body).
Types of Constructors
1. Default Constructors: Default constructor is the constructor which doesn’t take any
argument. It has no parameters.
#include <iostream.h>
class construct {
public:
int a, b;
// Default Constructor
construct()
{
a = 10;
b = 20;
}
};
int main()
{
// Default constructor called automatically
// when the object is created
construct c;
cout << "a: " << c.a << endl
<< "b: " << c.b;
return 1;
1
}
Output:
a: 10
b: 20
Note: Even if we do not define any constructor explicitly, the compiler will automatically provide a
default constructor implicitly.
#include <iostream.h>
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);
return 0;
}
Output:
p1.x = 10, p1.y = 15
When an object is declared in a parameterized constructor, the initial values have to be passed
as arguments to the constructor function. The normal way of object declaration may not work.
The constructors can be called explicitly or implicitly.
2
Example e = Example(0, 50); // Explicit call
Example e(0, 50); // Implicit call
Uses of Parameterized constructor:
1. It is used to initialize the various data elements of different objects with different values
when they are created.
2. It is used to overload constructors.
Note: We can have more than one constructors in a class. This is called as Constructor
Overloading.
Copy Constructor: A copy constructor is a member function which initializes an object using
another object of the same class. A copy constructor has the following general function
prototype:
ClassName (const ClassName &old_obj);
class Point
{
private:
int x, y;
public:
Point(int x1, int y1) { x = x1; y = y1; }
// Copy constructor
Point(const Point &p2) {x = p2.x; y = p2.y; }
int main()
{
Point p1(10, 15); // Normal constructor is called here
Point p2 = p1; // Copy constructor is called here
return 0;
}
Output:
p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15
Copy constructor is called when a new object is created from an existing object, as a copy of
the existing object. And assignment operator is called when an already initialized object is
assigned a new value from another existing object.
class Test
{
public:
Test() {}
Test(const Test &t)
{
cout<<"Copy constructor called "<<endl;
}
// Driver code
int main()
{
Test t1, t2;
t2 = t1;
Test t3 = t1;
getchar();
return 0;
}
Output:
Assignment operator called
Copy constructor called