Tuesday, April 16, 2013

Operator OverLoading in Hight Class ( Code )


Objective:
Write a program to calculate the height by using operator overloading method.
input height in feet and inches and calculate it.

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

namespace HeightCalcualtion
{
    class HEIGHT
    {
        int Feet, Inches;
        public HEIGHT(int f, int i)
        {
            Feet = f;
            Inches = i;
        }

        public static HEIGHT operator +(HEIGHT fh, HEIGHT sh)
        {
            int a, b;
            a = fh.Feet + sh.Feet;
            b = fh.Inches + sh.Inches;
            if(b>11)
            {
                b-= 12;
                a++;
            }
            HEIGHT h = new HEIGHT(a,b);
            return h;
        }
        class height
        {
            static void Main(string[] args)
            {
                HEIGHT h1 = new HEIGHT(20,13);
                HEIGHT h2 = new HEIGHT(18,8);
                HEIGHT h3 = new HEIGHT(12,9);
                HEIGHT h4 = h1 + h2 + h3;
                Console.WriteLine("Total height is {0} feet and {1} inches",h4.Feet,h4.Inches);
            }
        }
    }
}
Output:


No comments:

Post a Comment