Definitely every programmer who is familiar a little bit with OOP knows the
meaning of overloading. In general, "overloading" means defining an additional
functionality for an element (method, operator or ...). For example, look at
the definition of the following class:
public class CAccount
{
protected decimal balance;
public CAccount()
{
balance = 0;
}
public CAccount(decimal
InitialBalance)
{
balance =
InitialBalance;
}
} |
Two definitions for CAccount constructor give the
object client the chance to use the constructor with two different options.
CAccount a,b;
a = new CAccount(); //Initial balance will be 0
b = new CAccount(100); //Initial balance will be 100
|
Ok, now think of a scenario that we want to merge to
different accounts and make one single account as combination. Let say a bank
customer (account holder) has tow accounts in the bank and he wants to close
one account and transfer the balance to another account. In this case we need
an account that that has the balance which is the sum of both accounts
balances. Great, how can we implement it? What about this code?
public class CAccount
{
protected decimal balance;
public CAccount() //Constructor without initial
balance
{
balance = 0;
}
public CAccount(decimal InitialBalance) //Constructor
with initial balance
{
balance =
InitialBalance;
}
public void Credit(decimal amount)
{
balance += amount;
}
public void Debit(decimal amount)
{
balance -= amount;
}
public decimal GetBalance() // Accessor function to
read the protected balance
{
return balance;
}
// static function to merge two accounts
public static
CAccount MergeAccounts(CAccount acc1, CAccount acc2)
{
CAccount NewAccount =
new CAccount();
NewAccount.Credit(acc1.GetBalance() + acc2.GetBalance());
return NewAccount;
}
} |
Not that bad!! The client can perform this merge by
running this code:
CAccount a,b;
a = new CAccount(200); //Initial balance will be 200
b = new CAccount(100); //Initial balance will be 100
CAccount c;
c = CAccount.MergeAccounts(a,b); //Account c will have the
balance of 300 |
By now every thing looks normal. But what if there is
a way to make this job with a "+" Operator in a client code
like this:
CAccount a,b;
a = new CAccount(200); //Initial balance will be 200
b = new CAccount(100); //Initial balance will be 100
CAccount c;
c = a + b; //of course this code cannot be compiled |
The above code will not compile for sure because the
compiler doesn't know what to do with the plus sign when it is between the two
CAccount objects. But what if I can define the operator "+" in a way that the
compiler performs the same Merge job with the "+" synthax? This is exactly what
we mean by operator overloading. The "+" operator can add two numeric values
when it is between them. Very well, now we overload it with a new functionality
to do what we want when it comes between two objects of type CAccount. Now,
what do you think about this code?
public class CAccount
{
protected decimal balance;
public CAccount() //Constructor without initial
balance
{
balance = 0;
}
public CAccount(decimal InitialBalance) //Constructor
with initial balance
{
balance =
InitialBalance;
}
public void Credit(decimal amount)
{
balance += amount;
}
public void Debit(decimal amount)
{
balance -= amount;
}
public decimal GetBalance() // Accessor function to
read the protected balance
{
return balance;
}
// static function to merge two accounts
public static
CAccount MergeAccounts(CAccount acc1, CAccount acc2)
{
CAccount NewAccount =
new CAccount();
NewAccount.Credit(acc1.GetBalance() + acc2.GetBalance());
return NewAccount;
}
// static operator to merge two accounts
public
static CAccount operator+(CAccount acc1, CAccount acc2)
{
CAccount NewAccount =
new CAccount();
NewAccount.Credit(acc1.GetBalance() + acc2.GetBalance());
return NewAccount;
}
} |
Looks nice. Just look into the operator definition.
The syntax of the operator can be simply compared with the "MergeAccounts"
method. Actually I just changed the method signature to represent an operator
definition. In better words, I have overloaded the "+" operator to behave like
this once it comes between the two objects of type CAccount.
Now a sweet client code like the one below works happily and the balance in object "c" will be 300 which is the combination of "a" and "b".
CAccount a,b;
a = new CAccount(200); //Initial balance will be 200
b = new CAccount(100); //Initial balance will be 100
CAccount c;
c = a + b; //This time the code can be compiled happily |
Enjoy the fun of using it.