C++ Constructors and types of Constructors with example

C++ Constructors

C++ Constructors:

A constructor is a member function of a class that is called and executed automatically when an object of that class is created. The name of the constructor function is the same as the name f the class itself.

A constructor function may have arguments but it cannot return any value.



Example how to create a c++ constructors in programming:

Output:

In the above program, the class “data” contains member function “data”. This member function is the c++ constructors function because name of this function and the name of the class are same. When the member function “data” is executed, it print “programming digest” on the computer screen.

In the program, three objects (a,b, and c) of the class “data are created. Each time an object f the class “data” is created, the constructor is executed and the word “ programming digest” is printed on the computer screen. Since three objects are created, the word programming digest is printed three times.


Initializing data using C++ Constructors:

The constructor function are normally used to initialize values in data members of a class when the program is executed. This type of initialization is called the automatic initialization.

For example, if the member function has two arguments of int type, the specified values can be assigned to the data member of class using a constructor as shown below:

Output:

In the above program when the object “a” of the class “sum” is created, the control shifts to the constructor function “sum”. The c++ constructor function assigns values to variables n and m and its also calculates their sum. Thus when data function is executed by the object a, the values assigned to the data members of the object by the constructor are printed.

Similarly when the object b is created, the c++ constructor function is automatically executed.


C++ constructors Overloading:

More than one c++ constructor function can be defined in one class when more than one c++ constructors function are defined, each c++ constructors is defined with a different set of parameters. Defining more than one constructor with different set of parameters is called c++ constructors overloading.

When a program that uses the c++ constructors overloading is compiled, c++ compiler checks the number of parameters, their order and data types and marks them differently. When an object of the class is created, the corresponding c++ constructors that matches the number of parameters of the object function is executed.

In the following example, two constructors functions are defined in class “sum”

Example how to use c++ constructors overloading in programming:

Output:

when the program is executed, the object x is created first and them the sum constructor function that has only two integer type parameters is executed.

Then the y object is created. It has three parameters of integer type so the c++ constructors function that has three arguments of integer type is executed.



Example how to use constructors overloading function in c++ Programming:


Types of C++ Constructors:

Parameterized constructors

C++ Constructors that can take at least one argument are termed as parameterized C++ constructors. 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. The method of calling the constructor implicitly is also called the shorthand method. If we want to initialize fields of the class with your own values, then use a parameterized C++ constructor.


Default constructors

If the programmer does not supply a constructor for an instantiable class, Java compiler inserts a default constructor into your code on your behalf. This c++ constructors is known as default constructor. You would not find it in your source code (the java file) as it would be inserted into the code during compilation and exists in .class file. The behavior of the default constructor is language dependent. It may initialize data members to zero or other same values, or it may do nothing at all. In Java, a “default constructor” refer to a nullary c++ constructors that is automatically generated by the compiler if no constructors have been defined for the class or in the absence of any programmer-defined constructors (e.g. in Java, the default constructor implicitly calls the superclass’s nullary constructor, then executes an empty body). All fields are left at their initial value of 0 (integer types), 0.0 (floating-point types), false (boolean type), or null (reference types).

Example:

If you rely on an implicit default constructor, be sure to initialize members in the class definition, as shown in the previous example. Without those initializes, the members would be uninitialized and the Volume() call would produce a garbage value. In general, it is good practice to initialize members in this way even when not relying on an implicit default constructor.



Copy constructors

copy constructor initializes an object by copying the member values from an object of the same type. If your class members are all simple types such as scalar values, the compiler-generated copy constructor is sufficient and you do not need to define your own. If your class requires more complex initialization, then you need to implement a custom copy constructor. For example, if a class member is a pointer then you need to define a copy constructor to allocate new memory and copy the values from the other’s pointed-to object. The compiler-generated copy constructor simply copies the pointer, so that the new pointer still points to the other’s memory location.

Implicit copy constructor

Output


User-defined copy constructor

Now, consider a very simple dynamic array class like the following:

Output


Conversion constructors

Conversion constructors provide a means for a compiler to implicitly create an object belonging to one class based on an object of a different type. These c++ constructors are usually invoked implicitly to convert arguments or operands to an appropriate type, but they may also be called explicitly.


Move constructor

In C++, move constructors take a value reference to an object of the class, and are used to implement ownership transfer of the parameter object’s resources.

Recommended For You

Leave a Reply

%d bloggers like this: