0% found this document useful (0 votes)
4 views2 pages

Experiment No.9 C#docx

The document contains two C# programs demonstrating operator overloading in a Calculator class. The first program implements a unary operator to negate two numbers, while the second program implements a binary operator to add two numbers. Both programs include a print method to display the results of the operations.

Uploaded by

devab52664
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views2 pages

Experiment No.9 C#docx

The document contains two C# programs demonstrating operator overloading in a Calculator class. The first program implements a unary operator to negate two numbers, while the second program implements a binary operator to add two numbers. Both programs include a print method to display the results of the operations.

Uploaded by

devab52664
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Experiment no.

:09

Input: Unary

using System;
namespace ConsoleApp6 {
class Calculator
{
public int number1, number2;
public Calculator(int num1, int num2)
{
number1 = num1;
number2 = num2;
}
public static Calculator operator -(Calculator C1)
{
C1.number1 = -C1.number1;
C1.number2 = -C1.number2;
return C1;
}
public void print()
{
Console.WriteLine("Number1= " + number1 + " Number2= " + number2);
Console.WriteLine();
}
}
class Program
{
static void Main(String[] args)
{
Calculator calc = new Calculator(15, -25);
calc.print();
calc = -calc;
calc.print();
}
}
}

Output:-
Input: Binary

using System;
using System.Runtime.Intrinsics.Arm;
namespace ConsoleApp6 {
class Calculator
{
Calculator()
{

}
public int number1;
public Calculator(int num1)
{
number1 = num1;

}
public static Calculator operator +(Calculator C1,Calculator C2)
{
Calculator C3 = new Calculator();
C3.number1 = C1.number1+C2.number1;

return C3;
}
public void print()
{
Console.WriteLine("Number1= " + number1);

}
}
class Program
{
static void Main(String[] args)
{
Calculator c2 = new Calculator(200);
Calculator c1 = new Calculator(15);
Calculator c3 = new Calculator(0);
c3=c1 + c2;
c1.print();
c2.print();
c3.print();
}
}
}

Output:

You might also like