What is Operator Overloading?
We know that standard data types supplied by languages are well known and there will be operators like +,* ,% that operate on these data types. But, what is the case if it is user-defined types say a 3dpoint class, which is the combination of three integers. Well, all languages that supports operator overloading says, “It is your Type. Please you say how the operator + should work”.
If you say “How the Operator + should work for 3point class”, then you are overloading the + operator. Because, it will now know how to add two integer and how to add two 3dpoint.
Below are the types of overloading that I will demonstrate in this article:
- Implicit Conversion Operator
- Explicit Conversion Operator
- Binary Operator
Let us start with TimeHHMMSS Class
Before we move on to the Overloading, first let me explain what this class will do. The class is used to store the time in Hour, Minute and Seconds. There are three members defined for that. The class looks like:
class TimeHHMMSS
{
public int m_hour;
public int m_minute;
public int m_sec;
The default constructor will set all the members to zero. And a overloaded version will accept hour, minute and seconds. Below is the code for Constructors:
public TimeHHMMSS()
{
m_hour = 0;
m_minute = 0;
m_sec = 0;
}
public TimeHHMMSS(int hr, int min, int sec)
{
m_hour = hr;
m_minute = min;
m_sec = sec;
}
All classes in C# have Object as their base class. We will override the ToString method our own way.
public override string ToString()
{
return string.Format("{0}:{1}:{2}", m_hour, m_minute, m_sec);
}
Implicit Conversion – (A) Integer to TimeHHMMSS
Below is the syntax for Implicit conversion:
public static implicit operator ( Variable)
We will get the integer as parameter. So the time is specified in smaller units, say in seconds passed as an integer parameter. 1 Hour, 10 minutes, 15 Seconds can be specified in seconds as 4215. These “seconds” taken as an integer parameter is processed to split into Hour, Minutes, and Seconds. After the Split, we have all the member variables of the class ready to return back. Below is the Conversion operator:
public static implicit operator TimeHHMMSS(int totalSeconds)
{
int hour, min, seconds;
int RemainingSeconds;
TimeHHMMSS returnobject = new TimeHHMMSS();
seconds = totalSeconds % 60;
returnobject.m_sec = seconds;
RemainingSeconds = totalSeconds - seconds;
min = RemainingSeconds % ( 60 * 60 ); returnobject.m_minute = min / 60 ;
RemainingSeconds = totalSeconds -
( returnobject.m_minute * 60 + returnobject.m_sec );
hour = RemainingSeconds % (60 * 60 * 24 ); returnobject.m_hour = hour / 3600;
return returnobject;
}
OK, how is the above operator used? If we assume t2 is the TimeHHMMSS object constructed using the default constructor, the below statement will invoke the overloaded implicit conversion t2 = 33175;
Implicit Conversion – (B) TimeHHMMSS to Integer
Now, we will implement the same implicit conversion in the reverse; that is, we will make conversion from Time Object to integer. I hope as looked at the previous function, you will know what the implementation is. Below is the function:
public static implicit operator int(TimeHHMMSS time)
{
return (time.m_hour * 60 * 60) + (time.m_minute * 60 ) + time.m_sec ;
}
When will the calling code invoke the above implicit conversion? The code is below:
int timeinSeconds;
timeinSeconds = t2;
Explicit Conversion- Conversion from Float to TimeHHMMSS
In the above conversion, we saw that the conversion takes place even we do not explicitly specify that. The way we implement the Time class is we recommend integer data type, so we make the conversion for integer implicit. For a float data type, we will implement (not actually an implementation, we will handle if a user accidentally makes a float conversion explicitly, and re-direct to int implicit conversion).
Below is the code that will take a float, re-directs to integer implicit conversion. From the client point of view, it is an explicit conversion:
public static explicit operator TimeHHMMSS(float totalSeconds) {
TimeHHMMSS returnobject = new TimeHHMMSS();
returnobject = (int) totalSeconds; return returnobject;
}
Below is the piece of client code which will make a call to our explicit overloaded operator:
TimeHHMMSS t3 = (TimeHHMMSS) 23213.67f;
Binary Operator Overloading – Overload + Operator
Now, it is time to overload the binary operator +. With this, we can add two TimeHHMMSS class and get back the added TimeHHMMSS.
The call will write statement something like the one below:
TimeHHMMSS t4 = t1 + t2;
Our + overloading function will take TimeHHMMSS t1, t2 as two parameters in the same order; that is t1 as first parameter and t2 as second parameter. Then, the added TimeHHMMSS is returned back to the t4 object of the same type. We will use the implicit Time->Integer conversion on t1 and t2. Add the integer values then return the added value. Since the return type we specify as TimeHHMMSS, once again an implicit conversion in reverse direction takes place; that's a conversion from integer->TimeHHMMSS. The overloaded + operator is shown below:
public static TimeHHMMSS operator+ (TimeHHMMSS operandLH, TimeHHMMSS OperandRH)
{
int totalSeconds, LHS_Seconds, RHS_Seconds;
LHS_Seconds = operandLH; RHS_Seconds = OperandRH; totalSeconds = LHS_Seconds + RHS_Seconds;
return totalSeconds; }
Closing Notes
- The binary operator will work for the following statements also:
t4 = t4 + 4215;
t4 = t4 + (TimeHHMMSS) 54321.456f ;
Debug to see what kind of conversion takes place.
- If you overload an
== operator, then you should overload !=. Same holds for operator like < and >, etc.
Attachments
003_Exe: Run it in the console to see the output. Below is the code that will use the TimeHHMMSS.
static void Main(string[] args)
{
TimeHHMMSS t1 = new TimeHHMMSS(0,20,10);
Console.WriteLine("The time is: {0}", t1.ToString());
TimeHHMMSS t2 = new TimeHHMMSS();
t2 = 33175; Console.WriteLine("The time is: {0}", t2.ToString());
int timeinSeconds;
timeinSeconds = t2; Console.WriteLine("The time {0} in Seconds (2)", t2.ToString(),timeinSeconds );
TimeHHMMSS t3 = (TimeHHMMSS) 23213.67f;
Console.WriteLine("The time is: {0}", t2.ToString());
TimeHHMMSS t4 = t1 + t2;
Console.WriteLine("The time is: {0}", t4.ToString());
t4 = t4 + 4215;
Console.WriteLine("The time is: {0}", t4.ToString());
t4 = t4 + (TimeHHMMSS) 54321.456f ;
}
003_Src: Project source code written in .NET 2003 IDE. Convert the project for later version. When prompted by (IDE 2005 or later), click yes. Put a break point static void main, and examine the code with informative comments.
Hope you like this article.
History
- 23rd October, 2010: Initial version