Reputation: 221
Hi I have the below program and i am new to C#,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace UnaryOperatorOverLoad
{
public class UnaryOperator
{
private int Number1;
private int Number2;
private int Result;
public UnaryOperator() { }
public UnaryOperator(int number1, int number2)
{
Number1 = number1;
Number2 = number2;
}
public static UnaryOperator operator +(UnaryOperator opr)
{
UnaryOperator obj = new UnaryOperator();
obj.Result = obj.Number1 + obj.Number2;
return obj;
}
public void showdata()
{
Console.WriteLine("The Sum of Two Numbers is : " + Result);
}
}
public class Program
{
static void Main(string[] args)
{
UnaryOperator opr = new UnaryOperator(20, 30);
opr.showdata();
Console.ReadLine();
}
}
}
When i execute the code , i am getting the result is 0. I am not sure where i went wrong.Please help me to rectify the code.
Upvotes: 0
Views: 111
Reputation: 53958
This line of code
UnaryOperator opr = new UnaryOperator(20, 30);
Creates a new UnaryOPerator
objcet and sets the Number1
and Number2
equal to 20 and 30 correspondingly.
Then you call opr.showdata();
, which will print the value of the Result
variable to the console. However, since you haven't performed any addition, the value of the variable would be the default, 0. That's the reason why you got 0.
Side note: I can't get why you are tyring to overload the addition the way you do it. It's reasonable to want to overload the addition of two custom objects, like UnaryOperator. However it's seems to me a bit strange that you are trying to overload the addition using the way you do it. Also, since the +
operator has two operands, you have to define them like below (that's may not the overload you thought about, but this is the proper form in general terms):
public static UnaryOperator operator +(UnaryOperator unaryOperator1, UnaryOperator unaryOperator2)
{
return new UnaryOperator(unaryOperator1.Number1+unaryOperator2.Number1,
unaryOperator1.Number2+UnaryOperator2.Number2);
}
In order for this work, you would have to change the definition of your class to the following one:
public class UnaryOperator
{
public int Number1 { get; set; }
public int Number2 { get; set; }
public UnaryOperator(int number1, int number2)
{
Number1 = number1;
Number2 = number2;
}
public static UnaryOperator operator +(UnaryOperator unaryOperator1, UnaryOperator unaryOperator2)
{
return new UnaryOperator(unaryOperator1.Number1+unaryOperator2.Number1,
unaryOperator1.Number2+unaryOperator2.Number2);
}
public void ShowData()
{
Console.WriteLine("Number1: {0}, Number2: {1}", Number1, Number2);
}
}
The the Main
method should change to the following one:
public class Program
{
static void Main(string[] args)
{
// Declare the first UnaryOperator object.
UnaryOperator opr1 = new UnaryOperator(20, 30);
// Declare the second UnaryOperator object.
UnaryOperator opr2 = new UnaryOperator(10, 40);
// Add them using the overloaded version of the + operator
UnaryOperator result = opr1+opr2;
// Show the results
result.ShowData();
Console.ReadLine();
}
}
As I stated above, this may not be exactly the version of your +
operator overload, but I wrote an implementation of this overload to see it how it works. For further documentation, please look here.
Upvotes: 0
Reputation: 26635
Your mistakes:
UnaryOperator obj = new UnaryOperator();
in operator overloading function. So obj.Number1
and obj.Number2
's value is 0
, because you have just created obj
instance.opr = +opr;
statement in to the Main
method.Change your code as:
public class UnaryOperator
{
private int Number1;
private int Number2;
private int Result;
public UnaryOperator() { }
public UnaryOperator(int number1, int number2)
{
Number1 = number1;
Number2 = number2;
}
public static UnaryOperator operator +(UnaryOperator opr)
{
opr.Result = opr.Number1 + opr.Number2; // Change this line
return opr;
}
public void showdata()
{
Console.WriteLine("The Sum of Two Numbers is : " + Result);
}
}
public class Program
{
static void Main(string[] args)
{
UnaryOperator opr = new UnaryOperator(20, 30);
opr = +opr; // Add this statement
opr.showdata();
Console.ReadLine();
}
}
Upvotes: 1