What is a constructor?
A constructor is a special type of member function that is called automatically when
an object is created. It is used to initialize the data members of new objects i.e. provides
data for the objects.
A constructor is a special member function as the name of the constructor is the same as
the class name and it does not return any value hence they do not have a return type.
A constructor is different from normal functions in following ways:
● The name of the constructor is the same as its class name.
● Constructors are mostly declared in the public section of the class.
● Constructors do not return values; hence they do not have a return type.
● A constructor gets called automatically when we create the object of the class.
● Constructors can be overloaded.
The prototype of Constructors is as follows:
<class name> (list of parameter);
Constructors can be defined inside or outside of a class like a function.
The syntax for defining the constructor within the class:
<class-name> (list-of-parameters)
{
// constructor definition
}
The syntax for defining the constructor outside the class:
<class-name>: :<class-name> (list-of-parameters)
{
// constructor definition
}
A constructor is a special type of member function that is called automatically when
an object is created. It is used to initialize the data members of new objects i.e. provides
data for the objects.
A constructor is a special member function as the name of the constructor is the same as
the class name and it does not return any value hence they do not have a return type.
A constructor is different from normal functions in following ways:
● The name of the constructor is the same as its class name.
● Constructors are mostly declared in the public section of the class.
● Constructors do not return values; hence they do not have a return type.
● A constructor gets called automatically when we create the object of the class.
● Constructors can be overloaded.
The prototype of Constructors is as follows:
<class name> (list of parameter);
Constructors can be defined inside or outside of a class like a function.
The syntax for defining the constructor within the class:
<class-name> (list-of-parameters)
{
// constructor definition
}
The syntax for defining the constructor outside the class:
<class-name>: :<class-name> (list-of-parameters)
{
// constructor definition
}