|
-
Jul 17th, 2010, 12:11 AM
#1
Thread Starter
Frenzied Member
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();
}
}
}
Last edited by firoz.raj; May 30th, 2011 at 02:08 PM.
-
Jul 17th, 2010, 12:36 AM
#2
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.
-
Jul 17th, 2010, 12:40 AM
#3
Thread Starter
Frenzied Member
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.
but in the case of hours it is printing properly 12+1.if possible kindly demostrate any small example regarding operator overloading.
-
Jul 17th, 2010, 01:00 AM
#4
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.
-
Jul 18th, 2010, 11:40 PM
#5
Thread Starter
Frenzied Member
Re: Operator overloading Issue
Last edited by firoz.raj; Jul 19th, 2010 at 11:21 PM.
-
Jul 19th, 2010, 03:28 PM
#6
Re: Operator overloading Issue
>>, <<, +, -, /, *, %, ==, !=.
And you could have found that in 3 seconds using Google.
-
Jul 19th, 2010, 11:32 PM
#7
Thread Starter
Frenzied Member
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();
}
}
}
Last edited by firoz.raj; May 30th, 2011 at 02:08 PM.
-
Jul 19th, 2010, 11:56 PM
#8
Re: Operator overloading Issue
 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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|