What is Constructor Types of Constructor precat

Jio
By -
0

     

    What is Constructor? and what type ?

      Constructor

    • It is a member function of a class used to initialize object. 
    • Constructors get automatically called when the object is created i.e. memory is allocated to object. 

    •  If we don’t write any constructor, the compiler provides a default constructor. 

    •  Constructor has the same name as that of class and doesn’t have any return type. 

     Due to following reasons, the constructor is considered a special function of the class: 

    1. Its name is same as class name. 

    2. It doesn't have any return type. 

    3. It is designed to call implicitly.

     4. In the life time of the object , it gets called only once per object and according to order of its declaration.

    •  We can not call the constructor on an object, pointer or reference explicitly. It is designed to call implicitly. 
    • We can not declare the constructor static, constant, volatile or virtual. We can declare constructor only inline. 
    • Constructor overloading means inside a class more than one constructor is defined.
    •  We can have constructors with 
    •  No argument: 
        • initialise data member to default values 
    • One or more arguments: 
        • initialize data member to values passed to it 



     The argument of type of object : 

                    initialize the object by using the values of the data members of the passed object. It is called a copy constructor.




    Types of Constructor


    Parameterless constructor

                        • also called zero argument constructor or user defined default constructor

                • If we create object without passing argument then parameterless constructor gets called

                • Constructor do not take any parameter


    Parameterized constructor

    • If constructor take parameter then it is called parameterized constructor

    • If we create object, bypassing the argument then paramterized constructor gets called


    Default constructor

    • If we do not define constructor inside class then compiler generates default constructor for the class.

    • Compiler generated default constructor is parameterless.

    Copy constructor

    Copy constructor is a single parameter constructor hence it is considered a parameterized constructor 

    Post a Comment

    0Comments

    Post a Comment (0)