The constructors are special member
functions that are used in class object to enable automatic initialization of the object. Whenever an object is created, the special member
function will be executed automatically.
A constructor function is different from all other non-static member functions in a class because it is used to initialize the variables of whatever instance being created. A constructor function can be overloaded to accommodate the many different forms of initialization.
A constructor function is different from all other non-static member functions in a class because it is used to initialize the variables of whatever instance being created. A constructor function can be overloaded to accommodate the many different forms of initialization.
Syntax Rules for writing Constructor Function:
- A constructor name must be the same as that of its class name.
- It may not be static.
- It may not be virtual.
- It is declared with no return type (not even void).
- It should be declared private.
When the Constructor Function is invoked:
- The constructor is called before main () start of execution.
- Whenever the object is created in any of the following way:
- Local variable
- Global variable
- As a Static variable
- The temporary instance of class needs to be created.
- During use of the Dynamic Memory allocation operation new.
There are three types of Constructor
- Default Constructor
- Parameterized Constructor
- Copy Constructor
No comments:
Post a Comment