What is a constructor

As per the definition – A constructor is a function with the same name as that of the class. You can also add a few more things to this definition. You can say

  1. A constructor is a function with the same name as that of the class
  2. Just like any normal function a constructor can have arguments.
  3. Just like any normal function a constructor can be public, private and protected
  4. Just like any normal function you can write any code you want inside the constructor
  5. The only exception is that like other functions a constructor does not have a return type, not even void.

What is the role of a constructor?

Constructors are used to initialize the data members of an object.

How do you call a constructor?

Constructors are called immediately after the object is created. When you create the object of a class in your code you can tell which constructor you wish to call after the object creation. A constructor gets called just once during the lifetime of an object.

Quick Links

E-Magazines

@

Total Followers
Study Group Created
Study Group Joined
Following Teacher
Following Organization
Blog Articles Added
Questions Asked
Questions Answered
Jobs Posted
Total Members in Group
Questions asked by members
Tasks added in this Group

Please wait..

Ok

Login to Open ESchool OR Create your account    Login   SignUp