Thursday 18 September 2014

What Is Constructor & Types Of Constructor

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
  1. Default
  2. Parameters
  3. Static
  4. Private
  5. Copy
Constructor relates with overloading and base class initialization


1.Default

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

Example

Protected void Page_Load(object sender, EventArgs e)
    {
        Akm ob= Akm()
            Response.Write(ob.constr);

    }
    public class Akm
    {
        public string constr;
        public Akm()
            constr="oracle server";
    }

   2.Parameterized Constructor
  1. 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
    protected void Page_Load(object sender, EventArgs e)
        {
           Akm ob=new Akm("parameter constr")
           Response.Write(ob.constr);
    }
    public class Akm
        {
            public string 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

    protected void Page_Load(object sender, EventArgs e)
        {
            string result = Akm.str;
            Response.Write("<h1>" + result);
            Response.Write(Akm.str);
           }

    public class Akm
        {
            public static string str;
            public Akm()
            {
                str = "static constr";
            }

    4.Private Constructor

    It does not allow object creation outside the class

    Example

    protected void Page_Load(object sender, EventArgs e)
        {
            Response.Write(xyz.info());
           }
    public class xyz
        {
            public string str;
            private xyz()
            {
                str = "Microsoft tech";
            }
            public static string info()
            {
                xyz xx = new xyz();
                return xx.str;
            }

        }

    5.Copy Constructor

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

0 comments::

Post a Comment

Copyright © DotNet-Ashok