Constructor

Whenever a class or struct is instantiated, a constructor method is called. It helps in initializing values to the data members of the class.

There are multiple types… a parameterized, default , static constructor.

Parameterized

A parameterized constructor takes the argument to initialize the date members with values while initializing the class. This would be called every time a class is initialized during the life time of the application.

Default

A default constructor is a constructor that either has no parameters, or if it has parameters, all the parameters have default values. If no user-defined constructor exists for a class A and one is needed, the compiler implicitly declares a constructor A::A(). This constructor is an inline public member of its class.

Static(only in .net)(In Java it is not there)
A static constructor, however would be called only once during the lifetime of the application even though we instantiate the class many times.

If the class doesn’t have any constructor then all the variable would be initialized with default values… like int i will be assigned 0 values.

Leave a Reply

Your email address will not be published. Required fields are marked *