0% found this document useful (0 votes)
36 views

Constructors

Uploaded by

M. Talha Nadeem
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

Constructors

Uploaded by

M. Talha Nadeem
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Constructors

What is a Constructor?
• Constructor is a special method that gets invoked
“automatically” at the time of object creation.
• Constructor is used for Initializing the values to the data
members of the class.
• Constructor has the same name as the class name.
• Constructors do not have a return type (not even void) and
they do not return a value.
• A class can have more than one constructor as long as they
have different signature (i.e., different input arguments
syntax).
• Constructors have special syntax:
– must always have the same name as their class
Types of Constructor

• Constructor are of different types i.e.


– Default Constructor
– Parameterized Constructor
– Copy Constructor
Default Constructor or No argument
Constructor
• an implicit no-argument constructor is built into
the program automatically by the compiler, and
it’s this constructor that created the objects, even
though we didn’t define it in the class. This no argument
constructor is called the default
constructor.
• If it weren’t created automatically by the
constructor, you wouldn’t be able to create
objects of a class for which no constructor was
defined.
Default Constructor
• Default Constructor is also called as “empty constructor”
which has no arguments.
• It is automatically called when we create the object of
class.
• Remember that name of constructor is same as name of
class.
• Constructor never declared with the help of return type.
• We cannot declare a constructor with the help of void
return type.
Default Constructor
• There is always at least one constructor in every class
• If you do not define any constructors for a class, there exist
one by the compiler
– called default constructor
– default constructor will initialize each instance variable to its default
value
• A constructor is called whenever an object is created.
• If the programmer does not supply any constructors, the
default constructor will be present automatically
– The default constructor takes no arguments
– The default constructor takes no body
• Note: If you add a constructor declaration with arguments to
a class that previously had no explicit constructors, you lose
the default constructor.
Defining a Constructor
Defining a Constructor with body
Parameterized Constructor
• This is another type of constructor which has some arguments
and same name as class name but it uses some arguments
• We have to create object of Class by passing some arguments
at the time of creating object with the name of class.
• When we pass some arguments to the Constructor then this
will automatically pass the Arguments to the Constructor and
the values will retrieve by the Respective Data Members of
the Class.
Constructor Overloading
If we have two or more explicit constructors with
the same name, we say that constructor is
overloaded.
• Which of the constructors is executed when an
object is created depends on how many
arguments are used in the definition:
construct c; // calls first constructor
construct c(4, 7); // calls second constructor
Multiple Constructors
• Sometimes want to initialize in a number of
different ways, depending on circumstance.
• This can be supported by having multiple
constructors having different input
arguments. This is called constructor
overloading.
More on Constructor

• Constructors are used to assign initial values to instance variables of


the class.
Constructors have the following characteristics:
• They are called only once when the class is being instantiated.
• They must have the same name as the class itself.
• They do not return a value and you do not have to specify the
keyword void.
• Constructor are declared just like as we declare methods, except
that the
constructor don't have any return type.
• Constructor can be overloaded provided they should have different
arguments.

You might also like