C# Hierarchical Inheritance example Program

Program

using System;

namespace AboutClasses
{
    class HierarchicalInheritanceExample
    {
        class person
        {
            string name, adhaar, city;
            public void getData()
            {
                Console.WriteLine("Enter name");
                name = Console.ReadLine();
                Console.WriteLine("Enter Adhaar");
                adhaar = Console.ReadLine();
                Console.WriteLine("Enter city");
                city = Console.ReadLine();
            }
            public void disData()
            {
                Console.WriteLine("Name  : {0}", name);
                Console.WriteLine("Adhaar: {0}", adhaar);
                Console.WriteLine("City  : {0}", city);
            }
        };

        class employee : person
        {
            int salary;
            public void getSalary()
            {
                Console.WriteLine("Enter Salary");
                salary = int.Parse(Console.ReadLine());
            }
            public void disSalary()
            {
                Console.WriteLine("Salary  : {0}", salary);
            }
        };

        class trainee : person
        {
            int schoAmt;
            public void getSchoAmt()
            {
                Console.WriteLine("Enter scholarship amount:");
                schoAmt = int.Parse(Console.ReadLine());
            }
            public void disSchoAmt()
            {
                Console.WriteLine("Scholarship : {0}", schoAmt);
            }
        };

        public static void Main(string[] args)
        {
            Console.WriteLine("1.Employee");
            Console.WriteLine("2.Trainee");

            Console.WriteLine("Enter person category (1/2)");
            int PersonCategory = int.Parse(Console.ReadLine());

            switch (PersonCategory)
            {
                case 1:
                    Console.WriteLine("Employee");
                    employee e = new employee();

                    e.getData();
                    e.getSalary();
                    e.disData();
                    e.disSalary();
                    break;
                case 2:
                    Console.WriteLine("Trainee");
                    trainee t = new trainee();

                    t.getData();
                    t.getSchoAmt();
                    t.disData();
                    t.disSchoAmt();
                    break;
                default:
                    Console.WriteLine("Invalid Input");
                    break;
            }
            Console.ReadKey();

        }//main

    }//main class

}//namespace


Output 1

1.Employee
2.Trainee
Enter person category (1/2)
1
Employee
Enter name
Sivakumar
Enter Adhaar Number
1010-2902-2393
Enter city
Coimbatore
Enter Salary
45000
Name    : Sivakumar
Adhaar  : 1010-2902-2393
City    : Coimbatore
Salary  : 45000

Output 2

1.Employee
2.Trainee
Enter person category (1/2)
2
Trainee
Enter name
Mathura
Enter Adhaar Number
3903-4934-4902
Enter city
Madurai
Enter scholarship amount
100000
Name        : Mathura
Adhaar      : 3903-4934-4902
City        : Madurai
Scholarship : 100000

No comments:

Post a Comment