.NET Practical Slip 14 Q B

Slip 14 Q.B) Problem Statement

Q. Write a C#.Net program to define a class Person having members – name and address. Create a subclass called Employee with members – staff_id and salary. Create ‘n’ objects of the Employee class and display all the details of the Employee. 25 Marks

Answer:

using System;
namespace EmployeeDetailsApp
{ 
    class Person
    {
        public string Name;
        public string Address;
        
        public void GetPersonDetails()
        {
            Console.Write("Enter Name: ");
            Name = Console.ReadLine();
            Console.Write("Enter Address: ");
            Address = Console.ReadLine();
        }
        // Method to display person details
        public void DisplayPersonDetails()
        {
            Console.WriteLine("Name: " + Name);
            Console.WriteLine("Address: " + Address);
        }
    }
    class Employee : Person
    {
        public int StaffId;
        public double Salary;

        public void GetEmployeeDetails()
        {
            GetPersonDetails(); // Get name and address from Person class
            Console.Write("Enter Staff ID: ");
            StaffId = int.Parse(Console.ReadLine());
            Console.Write("Enter Salary: ");
            Salary = double.Parse(Console.ReadLine());
        }

        // Method to display employee details
        public void DisplayEmployeeDetails()
        {
            DisplayPersonDetails(); // Display name and address from Person class
            Console.WriteLine("Staff ID: " + StaffId);
            Console.WriteLine("Salary: " + Salary);
            Console.WriteLine("--------------------------");
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Enter number of employees: ");
            int n = int.Parse(Console.ReadLine());

            Employee[] employees = new Employee[n];

            // Input details for each employee
            for (int i = 0; i < n; i++)
            {
                Console.WriteLine("\nEnter details for Employee " + (i + 1));
                employees[i] = new Employee();
                employees[i].GetEmployeeDetails();
            }

            // Display details of all employees
            Console.WriteLine("\n--- Employee Details ---");
            for (int i = 0; i < n; i++)
            {
                Console.WriteLine("Employee " + (i + 1) + ":");
                employees[i].DisplayEmployeeDetails();
            }

            Console.WriteLine("Press any key to exit...");
            Console.ReadKey();
        }
    }
}

Helpful Links

Spread the love

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top