Implementing Multi Level Inheritance


  • In single Inheritance we have derived a new class ClsEmployee from ClsBranch class
  • Later during period of time, let us say company asked to computerize salary details of an employee. Then, for every salary details we need to have respective branch details
  • Then we derive a new class say ClsSalary from the ClsEmployee class like…

Example :-


Class Diagram :-



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CAMInheritance
{
    class ClsBranch
    {
        int BCode;
        string BName;
        string BAddress;
        public void GetBData()
        {
            Console.WriteLine("Enter the Bcode,BName,BAddress");
            BCode = Convert.ToInt32(Console.ReadLine());
            BName = Console.ReadLine();
            BAddress = Console.ReadLine();
        }
        public void DisplayBData()
        {
            Console.WriteLine("Branch Details Are:-");
            Console.WriteLine("Branch Code is           :-   "+BCode);
            Console.WriteLine("Branch Name is           :-   "+BName);
            Console.WriteLine("Branch Address is        :-   "+BAddress);
        }

    }
    class ClsEmployee:ClsBranch
    {
        int EmpId;
        string EName;
        int EAge;
        string EAddress;
        public void GetEmpData()
        {
            Console.WriteLine("Enter the EmpId,EName,EAge,EAddress");
            EmpId = Convert.ToInt32(Console.ReadLine());
            EName = Console.ReadLine();
            EAge = Convert.ToInt32(Console.ReadLine());
            EAddress = Console.ReadLine();
        }
        public void DisplayEmpData()
        {
            Console.WriteLine("Employee Details Are");
            Console.WriteLine("Employee Id is           :-      "+EmpId);
            Console.WriteLine("Employee Name            :-      "+EName);
            Console.WriteLine( "Employee Age is         :-      "+EAge);
            Console.WriteLine("Employee Address is      :-      "+EAddress);
        }
    }
    class ClsSalary:ClsEmployee
    {

        double Basic;
        double Da;
        double Hra;
        double Gross;
        public void GetSalData()
        {
            Console.WriteLine("Enter the Basic Salary");
            Basic = Convert.ToInt32(Console.ReadLine());

        }
        public void Caliculate()
        {
            Da = 0.4 * Basic;
            Hra = 0.3 * Basic;
            Gross = Basic + Da + Hra;

        }
        public void DisplaySalData()
        {
            Console.WriteLine("Employee Salary Details Are:-");
            Console.WriteLine("Employee Basic Salary is :-       "+Basic);
            Console.WriteLine("Employee Da is           :-       "+Da);
            Console.WriteLine("Employee Hra is          :-       "+Hra);
            Console.WriteLine("Employee Gross Salary is :-       "+Gross);

        }
    }
    class ClsMInheritance
    {
        static void Main(string[] args)
        {
            ClsSalary obj1 = new ClsSalary();
            obj1.GetEmpData();
            obj1.GetBData();
            obj1.GetSalData();
            obj1.Caliculate();
            obj1.DisplayEmpData();
            obj1.DisplayBData();
            obj1.DisplaySalData();
            Console.ReadLine();
        }
    }
}




Output :-

No comments:

Post a Comment