C#.Net find output programs (Classes & Objects) | set 2

Find the output of C#.Net programs | Classes & Objects | Set 2: Enhance the knowledge of C#.Net Classes & Objects 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 Sample
    {
        int A=5;
        int B=10;

        public void set(int a, int b)
        {
            A = a;
            B = b;
        }

        public void print()
        {
            Console.WriteLine(A + "," + B);
        }
    }

    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            (new Sample()).print();
        }
    }
}

Output:

5,10
Press any key to continue . . .

Explanation:

In the above program, we created a class Sample that contains two private integer data members A and B that are initialized with 5 and 10 respectively. Here, we created two methods set() and print() inside the Sample class.

The set() method is used to set values inside the data members, and the print() method is used to print values of data members on the console screen.

Now look at the Program class, the Main() method contains inside the Program class, which is used as an entry point for the program.

In the Main() method, we created an anonymous object of Sample class and call method using an anonymous object to print the values of A and B on the console screen.

Question 2:

using System;

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

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

        public void print()
        {
            Console.WriteLine(emp_id + "," + name+","+salary);
        }
    }

    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            Employee emp = new Employee();

            emp.setEmp(10, "Virat", 10000);
            emp.print();
        }
    }
}

Output:

10,Virat,0
Press any key to continue . . .

Explanation:

In the above program, we created a class Employee that contains three private data members. Here, we created two methods setEmp() and print() inside the Sample class.

The setEmp() method is used to set values of employee data members, and the print() method used to value of employee object.

Now look at the Program class, the Main() method contains inside the Program class, which is used as an entry point for the program.

In the Main() method, we created an object of Employee class.

Here, we set employee id, employee name, and employee salary using the setEmp() method, but here we did not assign the value of salary, the below statement is wrong, because we assigned local variable sal by data member salary.

sal     = salary;

The correct statement is given below,

salary     = sal;

Then, we call print() method to print employee detail.

Question 3:

using System;

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

        public void setEmp(int id, string na, int sal);
        public void print();
    }

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

    public void Employee::print()
    {
        Console.WriteLine(emp_id + "," + name+","+salary);
    }

    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            Employee emp = new Employee();

            emp.setEmp(10, "Virat", 10000);
            emp.print();
        }
    }
}

Output:

main.cs(15,11): error CS1525: Unexpected symbol `void', expecting `class', `delegate', `enum', `interface', `partial', or `struct'
main.cs(17,8): error CS1525: Unexpected symbol `emp_id', expecting `class', `delegate', `enum', `interface', `partial', or `struct'
main.cs(20,5): error CS1514: Unexpected symbol `}', expecting `.' or `{'
main.cs(22,11): error CS1525: Unexpected symbol `void', expecting `class', `delegate', `enum', `interface', `partial', or `struct'

Explanation:

The above program will generate syntax error because we cannot define methods of a class outside the class.

Question 4:

using System;

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

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

        internal void print()
        {
            Console.WriteLine(emp_id + "," + name+","+salary);
        }
    }

    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            Employee emp = new Employee();

            emp.setEmp(10, "Virat", 10000);
            emp.print();
        }
    }
}

Output:

10,Virat,10000
Press any key to continue . . .

Explanation:

In the above program, we created a class Employee that contains three private data members. Here, we created two methods setEmp() and print() inside the Sample class.

Here, we used access specifier internal. The internal members of the class, can be accessed outside the class.

The setEmp() method is used to set values of employee data members, and the print() method used to value of employee object.

Now look at the Program class, the Main() method contains inside the Program class, which is used as an entry point for the program.

In the Main() method, we created an object of Employee class.

Here, we set employee id, employee name, and employee salary using the setEmp() method and then we call print() method to print employee detail.

Question 5:

using System;

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

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

        public void print()
        {
            Console.WriteLine(emp_id + "," + name+","+salary);
        }
    }

    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            Employee emp = new Employee();

            emp.setEmp(10, "Virat", 10000);
            emp.print();
        }
    }
}

Output:

main.cs(5,4): error CS1525: Unexpected symbol `const'
main.cs(5,15): error CS1514: Unexpected symbol `class', expecting `.' or `{'
main.cs(35,0): error CS1525: Unexpected symbol `}'

Explanation:

The above program will generate syntax error because we cannot create a const class in a C# program.





Comments and Discussions!

Load comments ↻





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