Ishu_15hu

Operator Overloading C#

Dec 1st, 2020
509
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2.                    
  3. public class Program
  4. {
  5.     public class Sample
  6.     {
  7.         public int a,b;
  8.         public Sample(){a=0;b=0;} //Constructor
  9.         public Sample(int A, int B){a=A;b=B;} //Constructor
  10.        
  11.         public static Sample operator +(Sample ob1, Sample ob2) // Operator overloading
  12.         {
  13.             Sample ob3= new Sample();
  14.             ob3.a = ob1.a + ob2.a;
  15.             ob3.b = ob1.b + ob2.b;
  16.             return ob3;
  17.         }
  18.     }
  19.    
  20.     public static void Main()
  21.     {
  22.         Sample ob = new Sample(10,11);
  23.         Sample ob1 = new Sample(20,21);
  24.         Sample ob2 = new Sample();
  25.         ob2 = ob1+ob;
  26.         Console.WriteLine("ob2.a = "+ ob2.a);
  27.         Console.WriteLine("ob2.b = "+ ob2.b);
  28.     }
  29. }
RAW Paste Data