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
- Default Constructors: Default constructor is the constructor which doesn’t take any argument. It has no parameters.
// Cpp program to illustrate the// concept of Constructors#include <iostream>usingnamespacestd;classconstruct {public:inta, b;// Default Constructorconstruct(){a = 10;b = 20;}};intmain(){// Default constructor called automatically// when the object is createdconstruct c;cout <<"a: "<< c.a << endl<<"b: "<< c.b;return1;}chevron_rightfilter_noneOutput:
a: 10 b: 20
Note: Even if we do not define any constructor explicitly, the compiler will automatically provide a default constructor implicitly.
- Parameterized Constructors: It is possible to pass arguments to constructors. Typically, these arguments help initialize an object when it is created. To create a parameterized constructor, simply add parameters to it the way you would to any other function. When you define the constructor’s body, use the parameters to initialize the object.
// CPP program to illustrate// parameterized constructors#include <iostream>usingnamespacestd;classPoint {private:intx, y;public:// Parameterized ConstructorPoint(intx1,inty1){x = x1;y = y1;}intgetX(){returnx;}intgetY(){returny;}};intmain(){// Constructor calledPoint p1(10, 15);// Access values assigned by constructorcout <<"p1.x = "<< p1.getX() <<", p1.y = "<< p1.getY();return0;}chevron_rightfilter_noneOutput:
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.
Example e = Example(0, 50); // Explicit call Example e(0, 50); // Implicit call
Uses of Parameterized constructor:
- It is used to initialize the various data elements of different objects with different values when they are created.
- It is used to overload constructors.
Can we have more than one constructors in a class?
Yes, It is called Constructor Overloading. - Copy Constructor: A copy constructor is a member function which initializes an object using another object of the same class. Detailed article on Copy Constructor.
Related Articles :
- Destructors in C++
- quiz on constructors in C++
- Output of C++ programs | Set 26 (Constructors)
- Output of C++ programs | Set 27(Constructors and Destructors)
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Recommended Posts:
- When are Constructors Called?
- Constructors in Java
- Inheritance and constructors in Java
- C++ Internals | Default Constructors | Set 1
- Output of C++ programs | Set 26 (Constructors)
- StringTokenizer class in Java with example | Set 1 ( Constructors)
- C++ Interview questions based on constructors/ Destructors.
- When does compiler create default and copy constructors in C++?
- Program to check if a number is divisible by sum of its digits
- Dividing a Large file into Separate Modules in C/C++, Java and Python
- Count the number of holes in an integer
- Remove first X rows and columns from a matrix
- How to delete a range of values from the List using Iterator
- How to create a List with Constructor in C++ STL



