Friday, August 6, 2010

Constructors c#


Write a program to showing different types of constructors.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Csharp
{
    /// <summary>
    /// (a)Default Constructor
    /// (b)Parameterized Constructor
    /// (c)static Constructor
    /// (d)Copy Constructor
    /// (e)Private Constructor
    /// </summary>
    class Constructor
    {
        public static void Main(string[] args)
        {
            defaultconstructor obj1 = new defaultconstructor();
            obj1.member();
            int a, b;
            Console.WriteLine("Enter the values of a,b");
            a = Convert.ToInt32(Console.ReadLine());
            b = Convert.ToInt32(Console.ReadLine());
            constructor obj = new constructor(a, b);
            obj.display();
            privateconstructor obj2 = privateconstructor.createinstance();
            obj2.result();
            staticconstructor obj3 = new staticconstructor();
            obj3.display();
            copyconstructor ob1 = new copyconstructor(10, 20);
            ob1.display();
            // Here we are using copy constructor. Copy constructor is using the values already defined with ob1
            copyconstructor ob2 =new copyconstructor(ob1);
            ob2.display();
            Console.ReadLine();
          
        }

    }
    //(a) default constructor
    class defaultconstructor
    {
        int a, b;
        public defaultconstructor()
        {
            a = 5;
            b = 6;
        }
        public void member()
        {
            Console.WriteLine("Example of defaultconstructor");
            Console.WriteLine("The values of a is: {0} \n The value of b is: {1}", a, b);
            Console.WriteLine("===========================================================");
        }
    }
    //(b)parameterized constructor
    class constructor
    {
        int a, b;
        public constructor(int x, int y)
        {
            a = x;
            b = y;
        }
        public void display()
        {
            Console.WriteLine("Example of parameterizedconstructor");
            Console.WriteLine("Value of a: {0}",a);
            Console.WriteLine("Value of b: {0}",b);
            Console.WriteLine("===========================================================");
        }
    }
    //(c) private constructor
    class privateconstructor
    {
        public int a, b; public static int c, d;
        private privateconstructor(int x, int y)
        {
            this.a=x;
            this.b = y;
        }
        public static privateconstructor createinstance()
        {
            Console.WriteLine("Enter the values");
            c = Convert.ToInt32(Console.ReadLine());
            d = Convert.ToInt32(Console.ReadLine());
            return new privateconstructor(c,d);
        }
        public void result()
        {
            int z;
            z = a + b;
            Console.WriteLine("Example of privateconstructor");
            Console.WriteLine("The result is {0}", z);
            Console.WriteLine("===========================================================");
        }
    }
    //(d) static constructor
    class staticconstructor
    {
        static string carname, carmodel;
        static staticconstructor()
        {
            carname = "BMW";
            carmodel = "BMWZ8";
        }
        public void display()
        {
            Console.WriteLine("Example of static constructor");
            Console.WriteLine("The carname is {0}\n The carmodel is {1}", carname, carmodel);
            Console.WriteLine("===========================================================");
        }
    }
    //(e) copy constructor
    class copyconstructor
    {
        int a, b;

        public copyconstructor(int x, int y)
        {
            this.a = x;
            this.b = y;
        }
        public copyconstructor(copyconstructor a)
        {
            this.a = a.a;
            this.b = a.b;
        }





        public void display()
        {
            Console.WriteLine("Example of copy constructor");
            int z = a + b;
            Console.WriteLine(z);
            Console.WriteLine("===========================================================");
        }
    }

}

No comments:

Post a Comment