C#.Net find output programs (Constructors & Destructors) | set 2

Find the output of C#.Net programs | Constructors & Destructors | Set 2: Enhance the knowledge of C#.Net Constructors & Destructors concepts by solving and finding the output of some C#.Net programs.
Submitted by Nidhi, on February 08, 2021

Question 1:

using System;

namespace Demo
{
    class Employee
    {
        int     emp_id;
        string  name;
        int     salary;

        public Employee()
        {
            emp_id = 100;
            name = "john";
            salary = 20000;
        }
        public Employee(int id, string na, int sal)
        {
            emp_id  = id;
            name    = na;
            salary  = sal;
        }

        public void Print()
        {
            Console.WriteLine("Emp ID: " + emp_id);
            Console.WriteLine("Name  : " + name  );
            Console.WriteLine("Salary: " + salary+"\n");
        }
    }

    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            Employee emp1 = new Employee();
            Employee emp2 = new Employee(101,"Harry",10000);

            Employee emp3 = emp1;
            emp1.Print();
            emp2.Print();
            emp3.Print();
        }
    }
}

Output:

Emp ID: 100
Name  : john
Salary: 20000

Emp ID: 101
Name  : Harry
Salary: 10000

Emp ID: 100
Name  : john
Salary: 20000

Press any key to continue . . .

Explanation:

In the above program, we created a class Sample that contains data member emp_id, name, and salary. Here, we defined default and parameterized constructor to initialize the data members, and we also defined a Print() method to print the values of data members.

Now look to the Program class, the Program class contains the Main() method, which is the entry program of the program. Here, we created objects emp1 and emp2. Object emp1 initialized using the default constructor and emp2 initialized using the parameterized constructor.

Employee emp3 = emp1;

In the above statement, we assigned the referent of emp1 into emp3 that's why emp1 and emp3 will point the same object. Then we call Print() method values of emp1 and emp3, that will be the same.

Question 2:

using System;

namespace Demo
{
    class Employee
    {
        int     emp_id;
        string  name;
        int     salary;

        public Employee()
        {
            emp_id = 100;
            name = "john";
            salary = 20000;
        }
        public Employee(int id, string na, int sal)
        {
            emp_id  = id;
            name    = na;
            salary  = sal;
        }

        public Set(int id, string na, int sal)
        {
            emp_id = id;
            name = na;
            salary = sal;
        }

        public void Print()
        {
            Console.WriteLine("Emp ID: " + emp_id);
            Console.WriteLine("Name  : " + name  );
            Console.WriteLine("Salary: " + salary+"\n");
        }
    }

    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            Employee emp1 = new Employee();
            Employee emp2 = new Employee(101,"Harry",10000);

            Employee emp3 = emp1;

            emp3.Set(102,"Potter",30000);
            emp1.Print();
            emp2.Print();
            emp3.Print();
        }
    }
} 

Output:

main.cs(24,16): error CS1520: Class, struct, or interface method must have a return type

Explanation:

The above program will generate syntax error because the Set() method of Employee class does not have a return type, that's why it will generate a syntax error.

Question 3:

using System;

namespace Demo
{
    class Employee
    {
        int     emp_id;
        string  name;
        int     salary;

        public Employee()
        {
            emp_id = 100;
            name = "john";
            salary = 20000;
        }
        public Employee(int id, string na, int sal)
        {
            emp_id  = id;
            name    = na;
            salary  = sal;
        }

        public void Set(int id, string na, int sal)
        {
            emp_id = id;
            name = na;
            salary = sal;
        }

        public void Print()
        {
            Console.WriteLine("Emp ID: " + emp_id);
            Console.WriteLine("Name  : " + name  );
            Console.WriteLine("Salary: " + salary+"\n");
        }
    }

    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            Employee emp1 = new Employee();
            Employee emp2 = new Employee(101,"Harry",10000);

            Employee emp3 = emp1;

            emp3.Set(102,"Potter",30000);
            emp1.Print();
            emp2.Print();
            emp3.Print();
        }
    }
}

Output:

Emp ID: 102
Name  : Potter
Salary: 30000

Emp ID: 101
Name  : Harry
Salary: 10000

Emp ID: 102
Name  : Potter
Salary: 30000

Press any key to continue . . .

Explanation:

In the above program, we created a class Sample that contains data member emp_id, name, and salary. Here, we defined default and parameterized constructor to initialize the data members, and we also defined Set() and Print() method to set and print the values of data members.

Now look to the Program class, the Program class contains the Main() method, which is the entry program of the program. Here, we created objects emp1 and emp2. Object emp1 initialized using the default constructor and emp2 initialized using the parameterized constructor.

Employee emp3 = emp1;

In the above statement, we assigned the referent of emp1 into emp3 that's why emp1 and emp3 will point the same object. Then we set values of data members of emp3 using the Set() method that will also reflect in emp1, and then call Print() method values of emp1 and emp3, that will be same.

Question 4:

using System;

namespace Demo
{
    class Employee
    {
        int     emp_id;
        string  name;
        int     salary;

        public Employee()
        {
            emp_id = 100;
            name = "john";
            salary = 20000;
        }
        public Employee(int id, string na, int sal)
        {
            emp_id  = id;
            name    = na;
            salary  = sal;
        }

        public void Print()
        {
            Console.WriteLine("Emp ID: " + emp_id);
            Console.WriteLine("Name  : " + name  );
            Console.WriteLine("Salary: " + salary+"\n");
        }
    }

    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            new Employee().Print();
            new Employee(101,"Harry",10000).Print();
        }
    }
}

Output:

Emp ID: 100
Name  : john
Salary: 20000

Emp ID: 101
Name  : Harry
Salary: 10000

Press any key to continue . . .

Explanation:

In the above program, we created a class Sample that contains data member emp_id, name, and salary. Here, we defined default and parameterized constructor to initialize the data members, and we also defined a Print() method to print the values of data members.

Now look to the Program class, the Program class contains the Main() method, which is the entry program of the program. Here, we created two objects anonymous object and call Print() method to print values of data members.

Question 5:

using System;

namespace Demo
{
    class Employee
    {
        int     emp_id;
        string  name;
        int     salary;

        public Employee(int id, string na, int sal)
        {
            emp_id  = id;
            name    = na;
            salary  = sal;
        }

        public void Print()
        {
            Console.WriteLine("Emp ID: " + emp_id);
            Console.WriteLine("Name  : " + name  );
            Console.WriteLine("Salary: " + salary+"\n");
        }
    }

    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            Employee[] emp = { new Employee(100, "Herry", 1000), new Employee(101, "Potter", 2000) };

            emp[0].Print();
            emp[1].Print();

        }
    }
}

Output:

Emp ID: 100
Name  : Herry
Salary: 1000

Emp ID: 101
Name  : Potter
Salary: 2000

Press any key to continue . . .

Explanation:

In the above program, we created a class Sample that contains data member emp_id, name, and salary. Here, we defined a parameterized constructor to initialize the data members, and we also defined a Print() method to print the values of data members.

Now look to the Program class, the Program class contains the Main() method, which is the entry program of the program. Here, we created an array of objects and initialized using the parameterized constructor.





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.