Pages

Thursday, July 22, 2010

Constructers:

A constructor is a member function with the same name as its class. For example:

class X {
public:
X(); // constructor for class X
};

Constructors are used to create, and can initialize, objects of their class type.

You cannot declare a constructor as virtual or static, nor can you declare a constructor as const, volatile, or const volatile.

You do not specify a return type for a constructor. A return statement in the body of a constructor cannot have a return value.

What is the use of Constructor?


The main use of constructors is to initialize objects. The function of initialization is automatically carried out by the use of a special member function called a constructor.

General Syntax of Constructor

Constructor is a special member function that takes the same name as the class name.

The syntax generally is as given below:

{ arguments};

The default constructor for a class X has the form

X::X()

In the above example the arguments is optional.

The constructor is automatically named when an object is created. A constructor is named whenever an object is defined or dynamically allocated using the "new" operator.

There are several forms in which a constructor can take its shape namely:

Default Constructor:

This constructor has no arguments in it. Default Constructor is also called as no argument constructor.

For example:

class Exforsys
{
private:
int a,b;
public:
Exforsys();
...
};

Exforsys :: Exforsys()
{
a=0;
b=0;
}

Copy constructor:

This constructor takes one argument. Also called one argument constructor. The main use of copy constructor is to initialize the objects while in creation, also used to copy an object. The copy constructor allows the programmer to create a new object from an existing one by initialization.

For example to invoke a copy constructor the programmer writes:

Exforsys e3(e2);
or
Exforsys e3=e2;

Both the above formats can be sued to invoke a copy constructor.

For Example:


#include
class Exforsys()
{
private:
int a;
public:
Exforsys()
{ }
Exforsys(int w)
{
a=w;
}
Exforsys(Exforsys& e)
{
a=e.a;
cout<<” Example of Copy Constructor”;
}
void result()
{
cout<< a;
}
};

void main()
{
Exforsys e1(50);
Exforsys e3(e1);
cout<< “\ne3=”;e3.result();
}


In the above the copy constructor takes one argument an object of type Exforsys which is passed by reference. The output of the above program is

Example of Copy Constructor
e3=50

Some important points about constructors:

* A constructor takes the same name as the class name.
* The programmer cannot declare a constructor as virtual or static, nor can the programmer declare a constructor as const, volatile, or const volatile.
* No return type is specified for a constructor.
* The constructor must be defined in the public. The constructor must be a public member.
* Overloading of constructors is possible.


Source:exforsys.com,wikipedia and publib.boulder.ibm.com (sources edited,compiled for easy understanding)

No comments:

Post a Comment