Articles added in Mathematics Lab | Cyber Lab is now open | Astronomy Lab is now open | VLSI Lab is now open | 120 SEO Tips article published | More exciting articles coming soon!


A constructor is a special member function of a class which is meant for initializing an object at the time of declaration. Constructor will be executed automatically whenever an object is created.

-> Name of a constructor should be the class name.
-> Constructor will not return any thing(not even void)
-> Constructor can accept zero or more arguments. Constructor that accepts no arguments is known as a DEFAULT CONSTRUCTOR. Constructor that accepts one or more arguments is known as a PARAMETRIC CONSTRUCTOR.
-> There can be more than one constructors in a class.
-> Constructor can be overloaded.
-> Constructors should be declared as public members of a class.
-> Constructors is used for initialization of data members of the object.
-> If there are no constructor in a class, then the compiler will supply a default constructor.If there is a parameterized constructor then there should be a default constructor also.

//a class Test with a constructor function

#include < iostream.h >
class Test
Test :: Test()
cout<<”constructor of a class Test called” << endl;
//and here is the test program:
Test G; // global object

void func()
Test L; // local object in function func()
cout<<”here’s function func()” << endl;
void main()
Test X;
cout<<”main()function”<< endl;


constructor of class Test called (global object G)
constructor of class Test called (Object X in main())
main() function
constructor of class Test called (Object L in func())
here’s function func()

Default constructor:

C++ declares a built-in constructor for every class which is called default constructor. This constructor is called only if we have not defined any constructor explicitly inside the class.

Parameterized constructors:

Constructors can be invoked with arguments, just as in case of functions. The argument list can be specified within braces similar to the argument list in the function. Constructors with arguments are called PARAMETERIZED CONSTRUCTORS. The distinguishing characteristic is that the name of the constructor functions have to be the same as that of its class name.

class complex
float a,b;
complex(float x, float y)
{a=x; b=y;}
void getdata()
cout<<"Enter real part:";
cout<<"Enter imaginary part:";
void putdata()
cout< }
void main()
complex c1,c2(5.5,2.5);

When object c1 is created first constructor will be automatically called which will set c1=0+i0.But when object c2 is created the second constructor will be called where c2 will be set to 5.5+2.5i.

Constructors with default arguments:

Like any other function in c++, constructors can also be defined with default arguments.
If any arguments are passed during the creation of an object, the compiler selects the suitable constructor with default arguments.

For example constructor ex() can be declared as follows:
ex(int a, int b=0);
The default value of b is zero, then
ex e1(2,0);
assigns value 2 to the variable a and 0 to variable b. But the following statement
ex e2(2,3);
assigns 2 to a and 3 to b.The actual argument overrides the default value.

Copy constructor:

A copy constructor is used to declare and initialize an object from another object.
Defining a copy constructor can help you prevent problems that might occur when one object is used to initialize another,because at that time c++ performs bitwise copy.

Copy contructor can be declared as follows:
complex(complex &c);
The statement
complex c2(c1);
calls the copy constructor. The copy constructor can also called using following statement:
complex c2=c1;

CACKLE comment system

Programming Resources
Computer Networking Fundamentals Android Application