English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

3 answers

Hai *

Constructors are like "init functions". They turn a pile of arbitrary bits into a living object. Minimally they initialize internally used fields. They may also allocate resources (memory, files, semaphores, sockets, etc).

2006-07-27 02:31:13 · answer #1 · answered by dewman_byju 4 · 0 0

Constructors as the Name suggests Creates an object.
When you create a class in C++ or Java or any of the OOP languages, It is nothing but like a blue print which tells the compiler that, If any object of this class be created, It should have its data members and methods as such.
At this phase, No memory has been allocated which represents the Class.
The Instant you Instantiate ( Create Object ) of the Class ( by = new ) , the constructor of the class is called, since now the compiler needs to allocate a memory area representing the class which you will refer to as the .
When the Constructor is called, It allocates the memory needed by the Data members and also performs binding of the Methods in the Class so that you may call them through the Dot (.) Operator.
A Constructor is also responsible for creating the VTables and Vptrs ( Virtual Tables and Virtual Pointers ) which are used to decide run time binding ( late binding ) of the functions which is needed to implement Run Time Polymorphism.
Since, it performs all these "Constructions", the phrase "Constructor" was coined for this method.

Hope, I satisfy you.!!!

2006-07-27 03:03:07 · answer #2 · answered by ccsCoder 3 · 0 0

constructor is a method when u create an object the methods in that class will automatically called without calling them separately


eg:1)ordinary method

#include
#include
class ord
{
void display
{
cout<<"ORDINARY METHOD ";
}
};
void main()
{
ord o ord();
o.display(); //calling the function in class ord from main function
getch();
}

eg:2) constuctor

#include
#include
class cons
{
cons
{
cout<<"CONSTUCTOR METHOD ";
}
};
void main()
{
cons c cons(); //creates object for the class and call the methods
//NO need to call the methods in cons
getch();
}

2006-07-27 06:26:40 · answer #3 · answered by india_kakinada 2 · 0 0

fedest.com, questions and answers