Default InBuilt Constructor

public class Employee
    {
        private int employeeID;
        private string employeeName;
        private bool isEmployeed;

        /*No constructor specified therefore default constructor comes into picture which will initialize employeeID to 0 and employeeName = "" */

        public int printEmployeeID()
        {
            return employeeID;
        }
        public string printEmployeeName()
        {
            return employeeName;
        }
        public bool printisEmployeed()
        {
            return isEmployeed;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            int employeeID;
            string employeeName;
            bool isEmployeed;
            Employee emp1;

 // Here only reference is created so constructor will not be called.
            emp1 = new Employee();

 // Here the object is created so Default parameterless constrctor will be called.
            employeeID = emp1.printEmployeeID();
            employeeName = emp1.printEmployeeName();
            isEmployeed = emp1.printisEmployeed();
            Console.WriteLine("employeeID initiatlized by default constructor to " + employeeID);

//employeeID will be 0 which is initialized by default constructor
            Console.WriteLine("employeeName initiatlized by default constructor to " +employeeName);

// employeeName will be "" which is also initialized by default constructor.

            Console.WriteLine("isEmployeed initiatlized by default constructor to " + isEmployeed);

// isEmployeed will be false by default which is also initialized by default constructor.

            Console.Read();
        }
    }

No comments:

Post a Comment