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).
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
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();
}