Thursday, 18 September 2014

Constructor is a method which invokes automatic at the time of object creation.It Initialize object as per nature.Constructor is perform with various type

Default

Parameters

Static

Private

Copy

Constructor relates with overloading and base class initialization

1.Default

A default constructor is a constructor that either has no parameters, or if it has parameters, all the parameters have default values.

Example

Protectedvoid Page_Load(object sender, EventArgs e)

{

Akm ob= Akm()

Response.Write(ob.constr);

}

publicclassAkm

{

publicstring constr;

public Akm()

constr="oracle server";

}

2.Parameterized Constructor

When an object is declared in a parameterized constructor, the initial values have to be passed as arguments to the constructor function. The normal way of object declaration may not work. The constructors can be called explicitly or implicitly.

Example

protectedvoid Page_Load(object sender, EventArgs e)

{

Akm ob=newAkm("parameter
constr")

Response.Write(ob.constr);

}

publicclassAkm

{

publicstring constr;

public Akm(string con)

{

constr = con;

}

}

3.Static Constructor

It is execute automatic at the time of calling any static data member.It Does not take parameters.It can not be a public.

Example

protectedvoid Page_Load(object sender, EventArgs e)

{

string result = Akm.str;

Response.Write("<h1>" +
result);

Response.Write(Akm.str);

}

publicclassAkm

{

publicstaticstring str;

public Akm()

{

str = "static constr";

}

4.Private Constructor

It does not allow object creation outside the class

Example

protectedvoid Page_Load(object sender, EventArgs e)

{

Response.Write(xyz.info());

}

publicclassxyz

{

publicstring str;

private xyz()

{

str = "Microsoft tech";

}

publicstaticstring info()

{

xyz xx = newxyz();

return xx.str;

}

}

5.Copy Constructor

A copy constructor is a special constructor in the C++ programming language for creating a new object as a copy of an existing object.