Operator overloading Issue
can anyone tell me ?why it is printing 76 instead of 86. return new Time(first.hours+second.hours, first.minutes + second.minutes);
here is the following code .what i have written.kindly let me know please...
Code:
using System;
using System.Collections.Generic;
using System.Text;
namespace oExample8
{
public struct Time
{
public Time(int hours, int minutes)
{
this.hours = hours;
this.minutes = minutes;
}
int hours, minutes;
public static Time operator +(Time first, Time second)
{
return new Time(first.hours+second.hours, first.minutes + second.minutes);
}
public static void Main()
{
Time start = new Time();
Time duration = new Time();
Time finish = new Time();
start.hours = 12;
duration.minutes = 10;
duration.hours = 1;
duration.minutes = 76;
finish = start + duration;
Console.WriteLine("Finish time would be :{0} hours and {1} minutes.", finish.hours, finish.minutes);
Console.Read();
}
}
}
Re: Operator overloading Issue
Thats because you set duration.minutes to 76. It doesn't matter that you set it to 10 earlier, because you are overwriting that values with 76 two lines down.
Re: Operator overloading Issue
Quote:
Thats because you set duration.minutes to 76. It doesn't matter that you set it to 10 earlier, because you are overwriting that values with 76 two lines down.
but in the case of hours it is printing properly 12+1.if possible kindly demostrate any small example regarding operator overloading.
Re: Operator overloading Issue
I think this is a simple case of you just not reading your own code properly. Look at this form your own code:
Code:
start.hours = 12;
duration.minutes = 10;
duration.hours = 1;
duration.minutes = 76;
Presumably that should be:
Code:
start.hours = 12;
start.minutes = 10;
duration.hours = 1;
duration.minutes = 76;
We can all overlook the obvious at times but in this case you obviously didn't bother to do any debugging either, or you'd have seen that start.minutes was zero.
Re: Operator overloading Issue
Re: Operator overloading Issue
>>, <<, +, -, /, *, %, ==, !=.
And you could have found that in 3 seconds using Google.
Re: Operator overloading Issue
Execuse me .Can you tell me ?why the following code is Not Compiling?.let me know some hints please.
Code:
using System;
using System.Collections.Generic;
using System.Text;
namespace Example13
{
class Boxme
{
int itakeObj(Object x)
{
Console.WriteLine(x);
}
object iSpitObs()
{
int v;
v=5;
BoxMe Up=new Boxme();
up.itakeObjs(virtual);
int boxBox=(int)up.isitObjs();
}
}
}
Re: Operator overloading Issue
Quote:
Originally Posted by
firoz.raj
Execuse me .Can you tell me ?why the following code is Not Compiling?.let me know some hints please.
Code:
using System;
using System.Collections.Generic;
using System.Text;
namespace Example13
{
class Boxme
{
int itakeObj(Object x)
{
Console.WriteLine(x);
}
object iSpitObs()
{
int v;
v=5;
BoxMe Up=new Boxme();
up.itakeObjs(virtual);
int boxBox=(int)up.isitObjs();
}
}
}
This question seems unrelated to the topic of this thread so it should have been posted in a new thread. Please keep each thread to a single topic and each topic to a single thread.
You have no variable named 'virtual' so you can't pass a variable named 'virtual' to a method.
You have a function whose type is not void that doesn't execute a 'return' statement no matter which path execution takes.