This C#.NET program demonstrates the concept of inheritance by creating a base class Department and derived classes Sales and Human Resource, where department details are accepted and displayed in a proper format.
Slip 2 Q.B) Write a C#.Net program to create a base class Department and derived classes Sales and Human Resource. Accept the details of both departments and display them in proper format. 25 Marks
Answer:
Algorithm / Logic
- Start the program.
- Create a base class named Department with common data members such as Department ID and Department Name.
- Create derived classes Sales and HumanResource that inherit from the Department class.
- Add specific data members for each derived class (for example, sales target for Sales and number of employees for Human Resource).
- Accept input values for department details using the console.
- Store the accepted values in class variables.
- Display the details of Sales and Human Resource departments in a proper formatted manner.
- Stop the program.
Source Code
using System;
namespace DepartmentDetails
{
class Department
{
public string name;
public void display()
{
Console.WriteLine("Here is department");
}
}
class Sales : Department
{
public void getName()
{
Console.WriteLine("Department name is " + name);
}
}
class Human_Resource : Department
{
public void getName()
{
Console.WriteLine("Department name is " + name);
}
}
class Program
{
static void Main(string[] args)
{
Sales dept = new Sales();
dept.name = "Sales";
dept.display();
dept.getName();
Human_Resource dept1 = new Human_Resource();
dept1.name = "HR";
dept1.display();
dept1.getName();
Console.ReadLine();
}
}
}
FAQ Section
Q1. What is C#.NET?
C#.NET is an object-oriented programming language developed by Microsoft for building applications on the .NET framework.
Q2. What is meant by inheritance?
Inheritance is a feature of object-oriented programming that allows one class to acquire the properties and methods of another class.
Q3. What is a base class?
A base class is a parent class whose properties and methods are inherited by another class.
Q4. What is a derived class?
A derived class is a child class that inherits properties and methods from a base class.
Q5. Why is inheritance used in this program?
Inheritance is used to reuse common department details and reduce code duplication.