Click here to hide categories Click here to show left categories

User: Home          welcome : Guest          Log In / Register here     




Constructor

Definition: It is a special member function which is used to give initial values automatically to the variables associated with the objects that is it is used to create the objects. Since it creates the objects, it is called ‘Constructor’. Sometimes the abbreviation ‘ctor’ is used to refer to the constructor.

Certain characteristics of constructor are:


1. Its name is same as that of class name.
2. It is executed automatically when the object is created. It means there is no need to call it as we call ordinary member function.
3. It can take arguments but it does not have any return type that is it can not return any value.
4. If we do not use our own constructor in the class then it is automatically provided by the system.
5. We can overload constructor.


class sample
{
private:
int a, b;
public:
sample( ) //Constructor
{
a=0; // Gives initial values to
b=0; variables a and b
}
void show( )
{
cout<<a<<b;
}
};

void main( )
{
sample s1; //s1 Object is created and constructor
is called automatically to create s1
clrscr( );
s1.show( );
getch( );
}

Share this article   |    Print    |    Article read by 1518 times
Author:
Rohit kakria
I am software developer, moderator of xpode.com
Related Articles: No related article
Related Interview Questions: No related interview question