0% found this document useful (0 votes)
41 views

Time Operator Overloading

This document defines a Time class that represents a time with hours, minutes, and seconds. It overloads the +, -, *, and / operators to perform arithmetic on Time objects by adding/subtracting/multiplying/dividing the hour, minute, and second components. The main method demonstrates adding, subtracting, multiplying, and dividing two Time objects and displaying the results.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views

Time Operator Overloading

This document defines a Time class that represents a time with hours, minutes, and seconds. It overloads the +, -, *, and / operators to perform arithmetic on Time objects by adding/subtracting/multiplying/dividing the hour, minute, and second components. The main method demonstrates adding, subtracting, multiplying, and dividing two Time objects and displaying the results.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

using System;

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

namespace timeOperatorOverloading
{

class Time
{
double hour;
double minute;
double sec;

public Time (double h, double m, double s)


{

this.hour = h;
this.minute = m;
this.sec = s;
}

public static Time operator +(Time t1, Time t2)


{
double h = t1.hour + t2.hour;
double m = t1.minute + t2.minute;
double s = t1.sec + t2.sec;

if (s >= 60)
{
double a = s % 60;
int b = (int)s / 60;
s = a;
m = m + b;

if (m >= 60)
{
double a = m % 60;
int b = (int)m / 60;
m = a;
h = h + b;
}

Time box = new Time (h, m, s);


return box;
}

public static Time operator -(Time t1, Time t2)


{

double h = t1.hour - t2.hour;


if (h < 0)
{
h = h * -1;
}

double m = t1.minute - t2.minute;


if (m < 0)
{
m = m * -1;
}

double s = t1.sec - t2.sec;


if (s < 0)
{
s = s * -1;
}

Time box = new Time(h, m, s);


return box;
}

public static Time operator *(Time t1, Time t2)


{
double h = t1.hour * t2.hour;
double m = t1.minute * t2.minute;
double s = t1.sec * t2.sec;

if (s >= 60)
{
double a = s % 60;
int b = (int)s / 60;
s = a;
m = m + b;
}

if (m >= 60)
{
double a = m % 60;
int b = (int)m / 60;
m = a;
h = h + b;
}

Time box = new Time(h, m, s);


return box;
}

public static Time operator /(Time t1, Time t2)


{
double h = t1.hour / t2.hour;
double m = t1.minute / t2.minute;
double s = t1.sec / t2.sec;

if (s >= 60)
{
double a = s % 60;
int b = (int)s / 60;
s = a;
m = m + b;

if (m >= 60)
{
double a = m % 60;
int b = (int)m / 60;
m = a;
h = h + b;
}

Time box = new Time(h, m, s);


return box;
}

public void display()


{

Console.WriteLine("{0} {1} {2}", this.hour + " hours", this.minute + " minutes", this.sec
+ " seconds");

}
class Program
{
static void Main(string[] args)
{
Time t1 = new Time(12, 40, 40);
Time t2 = new Time(6, 20, 10);

Time netTime1 = t1 + t2;


Console.Write("Sum is: ");
netTime1.display();
Console.WriteLine("");

Time netTime2 = t1 - t2;


Console.Write("Difference is: ");
netTime2.display();
Console.WriteLine("");

Time netTime3 = t1 * t2;


Console.Write("Product is: ");
netTime3.display();
Console.WriteLine("");

Time netTime4 = t1 / t2;


Console.Write("t1/t2: ");
netTime4.display();
Console.WriteLine("");

}
}
}

You might also like