Explain Constructor - Default Constructor, Parametrized Constructor, Copy Constructor

Explain Constructor With Examples and Syntax, Types Of Constructor, Default Constructor, Parametrized Constructor, Copy Constructor



Constructor: 


1.     It’s a special function which has the same name as that of the Class name.
1.    It has No Return-Type, not even void
2.    It should have public or protected access within the class and rarely declared as private.
3.    Constructor overloading is possible.
4.    Constructors cannot be virtual.
5.    It can not be static.
4.     Default arguments are possible.
5.     They are invoked automatically as soon as Objects of Class are created.
6.    We can explicitly call Constructors of a Class.
7.    it is possible to have more than one constructor in a class.
7.     They can Not be Inherited.
1.    There are Three Basic Categories of Constructors.
a.   Default Constructor: It takes no parameters and performs no processing other than reservation of memory. It will always call by the compiler, if no user defined constructor is being provided.


 Syntax :-  class-name ( ) { code } ;
b.    Parameterized Constructor
 Syntax :- Syntax :-  class-name ( parameter-list ) { code } ;
c.     Copy Constructor.
                   Syntax :-  class-name ( reference of the same class) { code } ;
2.    If in any Class the Default Constructor is not defined then the compiler calls the implicit Default Constructor for Initializing objects of the Class. If there exists a Default Constructor defined in the Class then the defined Constructor will be called.
3.    If any of the three categories of Constructors are defined in the Class then the compiler in any case would never use the implicitly created Constructor, it would only use the user defined Constructor.

Labels: