A Constructor in C++ is a special member function having the same name as that of its class, which is used to initialize some valid values to an object’s data members. It is executed automatically whenever an object of a class is created. The only restriction that applies to the constructor is that it must not have a return type or void. The compiler automatically calls the constructor, and it is usually used to initialize values. The compiler distinguishes the constructor from other member functions of a class by its name, which is the same as its class. ctor is an abbreviation of constructors in C++.
The constructor can be defined in the class in the same way as that of normal member functions and can access any of its data members. The syntax for defining constructors inside the class body is as follows.
class CLASSNAME { .......... public: CLASSNAME([parameter_list]) // constructor definition { ...............} .............. };
You can also define a constructor with its declaration inside the class body and its definition outside as follows.
class CLASSNAME { ................. public: CLASSNAME ([parameter_list]);//Constructor Declaration { ...............}; CLASSNAME::CLASSNAME([parameter_list]) //Constructor Definition {................}
The above syntax shows the declaration and definition of a constructor. It is defined outside the class in the same way as defining a member function outside the class using the scope resolution operator (::). One should note that the name of the constructor is the same as that of its class. The constructor parameter list enclosed in the square brackets is optional and may contain zero or more parameters. We should declare the constructor in the class’s public section as they are invoked automatically when the objects are created.
Note: In the constructor, the initialization includes assigning specific values to the data members and includes allocating dynamic storage and calling other member functions.
#include<iostream.h> linclude<conio.h> class rectanqle { private: int length,breadth; public: rectangle()//constructor definition { length = 5; breadth = 6; } int area() { return(lenqth * breadth); } } ; int main() { clrscr(); rectanqle r1; cout<<"Area of rectangle = "<<r1.area(); qetch(); return 0; } Output : Area of rectangle = 30
Explanation: In this program, whenever we create an object of the class rectangle with the statement rectangle r1, the data members length and breadth of object r1 will be initialized with the values 5 and 6, respectively, by invoking the zero parameter constructor automatically. This method of initializing data members of objects using constructors is straightforward and concise as there is no need to call the member function for each object separately explicitly.
In this program, the constructor is defined inside the class. However, we can also declare constructor rectangle() inside the class and defined outside the class as follows :
class rectangle { ......... public: rectangle(); //constructor declaration }; rectangle::rectangle() // construction definition { length = 5; breadth = 6; }
A constructor to which no arguments are passed is called a default constructor. It is also called a constructor with no parameters. Using the default constructor, data members can be initialized to some reasonable value in its definition even though no arguments are specified explicitly. In the above program, the constructor rectangle::rectangle() is a default constructor.
Each time an object is created, a constructor is invoked. It can be seen in the above program. But the programs made in the previous chapters have defined objects and classes without defining any constructor for a class. So in such situations, the compiler automatically generates a constructor of its own with no parameters, i.e., default constructor. This compiler-generated default constructor is invoked automatically whenever the class’s object is created but doesn’t perform any initialization. However, if you define a default constructor explicitly, the compiler no longer generates a default constructor for you.
The following are the key points while defining constructors for a class:
• A constructor has the same name as that of the class to which it belongs.
• A constructor is executed automatically whenever the objects of a class are created.
• A constructor doesn’t have a return type, not even void.
• We can declare more than one constructor in a class, i.e., constructors can be overloaded. These constructors differ in their parameter lists.
• If you don’t explicitly provide a constructor of your own, then the compiler generates a default constructor for you.
• If you write any constructor explicitly with zero or more parameters, the compiler doesn’t generate the default constructor.
• A constructor can preferably be used for initialization and not for Input/Output operations.
• Like normal functions, constructors can have default arguments.
• Constructor should be declared in the public section of the class. If it is not declared in the public section (i.e., private), the whole class becomes private. The objects of the class created from outside cannot invoke the constructor, which is the first member function to be executed automatically.
• Constructors may not be static.
• Constructors are also used to allocate memory at run time using the new operator.
• You cannot call the constructor the way you call the normal function.
In addition to the above key points, the constructors also possess some additional features beyond the scope of the current topic.
• We cannot access the address of the constructors.
• An object with a constructor (or destructor) cannot be used as a member of a union.
• Constructors cannot be virtual.
• Constructors cannot be inherited, although a derived class can call the constructor of a base class.·
The various types of Constructor are as follows:
Default Constructor: Default Constructor is also called as Empty Constructor which has no arguments and It is Automatically called when we creates the object of class but Remember name of Constructor is same as name of class and Constructor never declared with the help of Return Type. Means we cant Declare a Constructor with the help of void Return Type. , if we never Pass or Declare any Arguments then this called as the Copy Constructors.
Parameterized Constructor: This is Another type Constructor which has some Arguments and same name as class name but it uses some Arguments So For this We have to create object of Class by passing some Arguments at the time of creating object with the name of class. When we pass some Arguments to the Constructor then this will automatically pass the Arguments to the Constructor and the values will retrieve by the Respective Data Members of the Class.
Copy Constructor: This is also Another type of Constructor. In this Constructor we pass the object of class into the Another Object of Same Class. As name Suggests you Copy, means Copy the values of one Object into the another Object of Class .This is used for Copying the values of class object into an another object of class So we call them as Copy Constructor and For Copying the values We have to pass the name of object whose values we wants to Copying and When we are using or passing an Object to a Constructor then we must have to use the & Ampersand or Address Operator.
Destructor: As we know that Constructor is that which is used for Assigning Some Values to data Members and For Assigning Some Values this May also used Some Memory so that to free up the Memory which is Allocated by Constructor, destructor is used which gets Automatically Called at the End of Program and we doesn’t have to Explicitly Call a Destructor and Destructor Cant be Parameterized or a Copy This can be only one Means Default Destructor which Have no Arguments. For Declaring a Destructor we have to use ~tiled Symbol in front of Destructor.