What is the use of constructor?

Showing Answers 1 - 19 of 19 Answers

Chitta Ranjan Ray,TCS-KOCHI

  • Nov 21st, 2006
 

Constructor is basicaly used for initialising the variables at the time of creation of object

  Was this answer useful?  Yes

G.Nithya

  • Nov 29th, 2006
 

Use of constructor:

                      For dynamically allocating memory space.

  Was this answer useful?  Yes

Pushpa Siva Kumar

  • Dec 14th, 2006
 

Constructor is used to initialize the member variable,when the object is created.

  Was this answer useful?  Yes

govind pavan

  • Jan 6th, 2007
 

constructors are used for allocating memory space for variables. It is special funtion which getsactivated automatically when object of that class is created.

  Was this answer useful?  Yes

ravi shastry

  • Jan 19th, 2007
 

constructor is automatically called when an object is created. It has no return type and it has the same name as the class.

  Was this answer useful?  Yes

shipra Rana

  • Feb 13th, 2007
 

constructors are used to intialize the values. its name is same as that of class. it activates automatically as the class object activated. it has no return type as it returns nothing. it saves the memory space. it activates at run time

  Was this answer useful?  Yes

Manoj Kumar

  • Mar 2nd, 2007
 

Constructor is a special member function of a class having same name as the class name.it is called at the time of creation of the object.it initialises the object.constructor can be overloaded.

  Was this answer useful?  Yes

dasam

  • Mar 29th, 2007
 

one more point ... constructors also used to create a vTable and initialises the vptr to NULL

  Was this answer useful?  Yes

Rajan

  • Oct 15th, 2015
 

Constructors are of two types both of em initializes the the variables at the time of the object creation and align the memory dynamically.

  Was this answer useful?  Yes

Ethan

  • Dec 20th, 2015
 

The constructor initializes the member variables and makes calls to the constructors of any superclasses. It also provides the programmer the opportunity to code extra funcionality into their program when an object is created. I do not beleive it dynamically allocates memory for the object, the "new" keyword is necessary for that. Otherwise the memory is allocated at compile time. It certainly gives the programmer the opportunity to dynamically allocate memory for any variables of varying size like arrays where the desired size isnt known at compile time.

  Was this answer useful?  Yes

Aarish

  • Apr 20th, 2016
 

In class-based object-oriented programming, a constructor (abbreviation: ctor) in a class is a special type of subroutine called to create an object. It prepares the new object for use, often accepting arguments that the constructor uses to set required member variables.

  Was this answer useful?  Yes

Talha Bilal

  • Nov 28th, 2016
 

Last sentence is very useful and any other did not describe it.

  Was this answer useful?  Yes

Kumar Lav

  • Jun 26th, 2017
 

Constructor is generally used to initialize the member variable,when the object is created and it is automatically call by Compiler. And also Constructor is used for dynamically allocating memory space.

Code
  1. /*  Example Program For Simple Example Program Of Constructor In C++

  2. Fb:- kumar.lav.562

  3.    

  4.  

  5.     Coded By:Kumar Lav             */

  6.  

  7. #include<iostream>

  8. #include<conio.h>

  9. // Variable Declaration

  10. //Constructor

  11.     Example()            {

  12.     // Assign Values In Constructor

  13. "Im Constructor

  14. ""Values :"<<a<<"       "// Constructor invoked.

  15.         Object.Display();

  16.  

  17.         // Wait For Output Screen

  Was this answer useful?  Yes

Atul

  • Dec 18th, 2017
 

Constructor is a function which is considered as user define function which gets implicitly called after allocating memory for object.
Writing constructor is optional.
Constructor should be written in public access specifier.
If we write as private or protected it generate error. We cannot use concept of friend, virtual, static, constant with constructor.
Basically main thing is that after calling the constructor the memory will get allocated for class characteristic.

  Was this answer useful?  Yes

abhinav

  • Mar 3rd, 2018
 

then why do we create constructor if it is automatically created by default.

  Was this answer useful?  Yes

lakshminarayana k

  • May 24th, 2018
 

Default constructor will be called automatically by the compiler and initialize the members to default values. If at all you want to allocate some initial values to class members other than default values then we have to write our own constructor.

  Was this answer useful?  Yes

Anshu Shrivastava

  • Aug 27th, 2018
 

When we want to assign some values for the data member of the class then we make our own constructor.
like->
class Abc
{
int i;
};
int main()
{
Abc b1;//object of Abc class,it will call by default unparameterised constructor which will assign zero value to "i";
but if we make our own constructor with some data member value then that value will be assign
Abc::Abc()
{
i=8;//when constructor will call then the value of i will be 8
}
}

  Was this answer useful?  Yes

Avinash Patil

  • Oct 30th, 2018
 

Constructor is a member function of a class which is used to initialize the object.
Types:
1) Copy Constructor
2) Parameterless constructor
3) Parameterrized constructor
4) Default constructor

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions