Beispiel #1
0
 // 4) declare overloading of operator + to add 2 objects of Money
 public static Money operator +(Money money1, Money money2)
 {
     Money money = new Money();
     money.Amount = money1.Amount + money2.Amount;
     return money;
     
 }
Beispiel #2
0
 // 5) declare overloading of operator -- to decrease object of Money by 1
 public static Money operator --(Money money1)
 {
     //RV: I think that it is better to decrease the amount of the same object and not create the new one
     Money money = new Money();
     money.Amount = --money1.Amount;
     return money;
 }
 static void Main(string[] args)
 {
     // 10) declare 2 objects
     Money money1 = new Money(2, CurrencyTypes.UAH);
     Money money2 = new Money(7, CurrencyTypes.UAH);
     // 11) do operations
     // add 2 objects of Money
     Money money = new Money();
     money = money1 + money2;
     Console.WriteLine("Operator overload(money object1+money object2) +: {0}", money.Amount);
     money--;
     Console.WriteLine("Operator overload(decrease money object1) --: {0}",money.Amount);
     money = money1 * money2;
     Console.WriteLine("Operator overload(money object1*money object2*3) *: {0}",money.Amount);
     bool a;
     a = money1 > money2;
     Console.WriteLine("Operator overload(if(money object1>money object2) return true,else return false) >: {0}",a);
     a = money1 < money2;
     Console.WriteLine("Operator overload(if(money object1>money object2) return false,else return true) >: {0}",a);
     Console.WriteLine("Operator overload(return type of money Amount): {0}",money1.Amount.GetType());
     string i = money1;
     Console.WriteLine("Operator overload(return type of money Amount): {0}", i.GetType());
     Console.ReadKey();  
 }