Rotate your phone or change to desktop for better experience

Rotate your phone or change to desktop for better experience

8.Demonstrate operator overloading two complex numbers

 using System;

using System.Collections.Generic;

namespace ConsoleApplication7

{

class complex

{

int a, b;

public complex()

{

}

public complex(int x, int y)

{

a = x;

b = y;

}

public static complex operator +(complex c1, complex c2)

{

complex c3 = new complex();

c3.a = c1.a + c2.a;

c3.b = c1.b + c2.b;

return (c3);

}

public void display()

{

Console.WriteLine(a + "+i" + b);

}

}

class complexmain

{

public static void Main()

{

complex c = new complex();

complex c1=new complex(4,5);

complex c2 = new complex(7, 2);

c = c1 + c2;

c1.display();

c2.display();

Console.WriteLine("---------");

c.display();

Console.ReadLine();

}

}

Post a Comment

0 Comments