A constructor is a function that initializes the members of an object. A constructor only knows how to build an object of its own class. If no constructor is supplied then a default one is created by the compiler without any parameters. There must always be a constructor, even if it is the default and empty. If you supply a constructor with parameters then a default will NOT be created.
Like any other function constructor can also be overloaded. A Default constructor is a constructor that can be called with no arguments.
There is a special constructor called copy constructor. Copy constructor creates a new object as a copy of an existing object. The first argument of such a constructor is a reference to an object of the same type as is being constructed. If you don’t define your own copy constructor, the compiler will provide you one.
Normally if you have any pointer or reference variable in your class, then you should define your own copy constructor and overloaded assignment operator to do the deep copy. Otherwise it will do the shallow copy. We will discuss about deep and shallow copy in separate post soon.
One important point to note is that we cannot pass object by value to the copy constructor. According to the C++ standard the copy constructor must take a reference to the same class.
Constructors cannot be virtual. Since virtual allows us to call a function knowing only the interfaces and not the exact type of the object. To create an object you need complete information. In particular, you need to know what you want to create exactly. Hence call to a constructor can't be virtual. We will see about virtual functions and vtable in forthcoming posts.
class class_Name
{
Private:
Int var;
Public:
class_Name() {} // Default Constructor
class_Name(int x):var(x) {} //Overloaded Constructor
class_Name(class_Name const &obj){} //Copy constructor
};
Where class_Name() is the default constructor for class class_Name. class_Name(int x)is the overloaded constructor which takes an integer as argument and assigns it to variable ‘a’. class_Name(class_Name const &obj) is the copy constructor.
Example:
#include
Using std::cout;
Class ConstructorExample
{
Int x;
Public:
ConstructorExample ():x(10){}//Default
ConstructorExample(int a):x(a){}//Overloaded
ConstructorExample(ConstructorExample const &obj){//Copy constructor
X = obj.x;
}
Void Display(){
Cout<<”Value of x is “<}
};
Int main()
{
ConstructorExample obj1;
Obj1. Display ();
ConstructorExample obj2(25);
Obj2. Display ();
ConstructorExample obj3(obj2);
Obj3. Display ();
}
ConstructorExample () is the default constructor. The constructor must have the public access in order to create the object of that class type.
In default constructor x is initialized with value 10 in the member initialization list of the constructor ConstructorExample ().
ConstructorExample (int a) is the overloaded constructor. It takes integer an as argument and assigns it to variable ‘x’.
ConstructorExample(ConstructorExample const &obj) is the copy constructor. It builds new ConstructorExample object from existing ConstructorExample object.
And we have an public member function called void Display () which takes and returns nothing, Also prints the message with value of x.
In main function we are declaring the object obj1 of type ConstructorExample. It uses the default constructor ConstructorExample () to build this object. So it will initialize the variable x with default value 10.
The object obj2 of type ConstructorExample. It uses the overloaded constructor ConstructorExample (int a) to build this object. So it will initialize the variable x with the given value 25.
And we are declaring the object obj3 of type ConstructorExample. It uses the copy constructor ConstructorExample (ConstructorExample const &obj) to build this object. So it will initialize the variable x with the existing obj2.x value 25.
Now we are calling the member function Display () on object obj1, obj2 and obj3 to check the available values.
save the above file as ConstructorExample.cpp
compile the above program using GNU g++ compiler (Linux).
g++ ConstructorExample.cpp -o ConstructorExample
The output file name will be ConstructorExample. Run the file using ./ConstructorExample
You can see the output below
Value of x is 10
Value of x is 25
Value of x is 25
Things to note when coding the constructors
- The constructor must have the same name as class.
- The constructor should not have any return type (not even void).
- Constructors cannot be virtual.
- Storage specifiers and Storage qualifiers are not applicable to class Constructors.
- Copy constructor is called
- When an object is created from another object of the same type
- When an object is passed by value as a parameter to a function
- When an object is returned from a function
Happy Coding :)