Constructors Flashcards
Constructor Definition
A constructor is a member method having the same name as
that of the class and is used to initialize the instance
variables of the Objects.
Return type of constructor
No return type; not even void
Use of constructor
To initialize data members / instance variables
Eg: class sum
{
int a, b;
sum() → constructor
{ 0=0,6=0;
}
How can constructor be invoked?
At the time of creating an object of a class
Constructor is automatically/manually called
Automatically
Features of constructor
same name as class name
No return type; not even void
called automatically when object is created
Used to initialize data members
constructor is ALWAYS public
Two types of constructors
Parameterized
Non-parameterized
Non parameterized constructor?
A constructor which Initialized the instance variables of an object with definite values readily available within it.
Parameterized constructor?
Initializes instance variables with the help of parameters passed in it at the time of creating an object
Default constructor
Non_parameterized constructor which is used to initialize instance variables with default values of datatype
! No syntax for constructor is written!
Constructor overloading?
Using a number of constructors with the same name but different types of parameters.