Click here to Skip to main content
15,949,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
class Nybble
{
    int val; // underlying storage 

    public Nybble() { val = 0; }

    public Nybble(int i)
    {
        val = i;
        val = val & 0xF; // retain lower 4 bits 
    }

    // Overload binary + for Nybble + Nybble.  
    public static Nybble operator +(Nybble op1, Nybble op2)
    {
        Nybble result = new Nybble();

        result.val = op1.val + op2.val;

        result.val = result.val & 0xF; // retain lower 4 bits  

        return result;
    }

    // Overload binary + for Nybble + int.  
    public static Nybble operator +(Nybble op1, int op2)
    {
        Nybble result = new Nybble();

        result.val = op1.val + op2;

        result.val = result.val & 0xF; // retain lower 4 bits  

        return result;
    }

    // Overload binary + for int + Nybble.  
    public static Nybble operator +(int op1, Nybble op2)
    {
        Nybble result = new Nybble();

        result.val = op1 + op2.val;

        result.val = result.val & 0xF; // retain lower 4 bits  

        return result;
    }

    // Overload ++. 
    public static Nybble operator ++(Nybble op)
    {
        Nybble result = new Nybble();
        result.val = op.val + 1;

        result.val = result.val & 0xF; // retain lower 4 bits 

        return result;
    }

    // Overload >. 
    public static bool operator >(Nybble op1, Nybble op2)
    {
        if (op1.val > op2.val) return true;
        else return false;
    }

    // Overload <. 
    public static bool operator <(Nybble op1, Nybble op2)
    {
        if (op1.val < op2.val) return true;
        else return false;
    }

    // Convert a Nybble into an int. 
    public static implicit operator int(Nybble op)
    {
        Console.WriteLine("obj to int");
        return op.val;
    }

    // Convert an int into a Nybble. 
    public static implicit operator Nybble(int op)
    {
        Console.WriteLine("int to obj");
        return new Nybble(op);
    }
}

class NybbleDemo
{
    static void Main()
    {
        Nybble a = new Nybble(1);
        Nybble b = new Nybble(10);
        Nybble c = new Nybble();
        int t;

        Console.WriteLine("a: " + (int)a);//(1)
        Console.WriteLine("b: " + (int)b);

        // Use a Nybble in an if statement. 
        if (a < b) Console.WriteLine("a is less than b\n");

        // Add two Nybbles together. 
        c = a + b;
        Console.WriteLine("c after c = a + b: " + (int)c);

        // Add an int to a Nybble. 
        a += 5;
        Console.WriteLine("a after a += 5: " + (int)a);

        Console.WriteLine();

        // Use a Nybble in an int expression. 
        t = a * 2 + 3;
        Console.WriteLine("Result of a * 2 + 3: " + t);

        Console.WriteLine();

        // Illustrate int assignment and overflow. 
        a = 19;
        Console.WriteLine("Result of a = 19: " + (int)a);

        Console.WriteLine();

        // Use a Nybble to control a loop. //(2)                    Console.WriteLine("Control a for loop with a Nybble.");
        for (a = 0; a < 10; a++)
            Console.Write((int)a + " ");
        Console.WriteLine();
    }
}


The lines are labelled and underlined:
(1)Why do you need to explicitly cast it when I have already implemented a implicit coneversion?

(2)In the for loop, a = 0(initialised), hence the int to obj should be called first then it should return the new object reference then it should print obj to int then the value. But the output to this program is a little cofusing as it prints int to obj twice and the value and then obj to int once and furher it prints int to obj for every a++ in the loop? please explain.


Thanks in advance!
Posted

Okay, I'll try to explain the code in your for loop as clearly as I can.

Your first two lines of output was written when the code enter the for loop in this part:
C#
for (a = 0; a < 10; a++) //convert 0 and 10 to Nybble

10 converted to Nybble cause you only override operator < for Nybble to Nybble, not Nybble to int.
causing the code to write these lines to the compiler
int to obj<br />
int to obj


The next lines were written in your loop
C#
Console.Write((int)a + " "); //cast a to int and print the string

I assume you already know that above code will print this output:
obj to int<br />
0 
--There's a space after 0

So, why did the code print
obj to int<br />
0 int to obj 

instead of above desired output?

This is because you're implementing a one-line loop
Therefore, after the code print 0 the code will go back to check the loop here:
C#
for (a = 0; a < 10; a++) //execute a++ first, then check if a < 10

When the check to a < 10 is performed, the code will print int to obj next to your previous output
resulting in your output above

Now try to change the loop into this one:
C#
for (a = 0; a < 10; a++){
    Console.Write((int)a + " ");
    Console.WriteLine();
}

The output should be like this :
int to obj<br />
int to obj<br />
obj to int<br />
0<br />
int to obj<br />
obj to int<br />
1<br />
int to obj<br />
obj to int<br />
....
 
Share this answer
 
v2
i still am confused. Look at this portionof my implict converter.

C#
// Convert a Nybble into an int.
   public static implicit operator int(Nybble op)
   {
       Console.WriteLine("obj to int");
       return op.val;
   }
   // Convert an int into a Nybble.
   public static implicit operator Nybble(int op)
   {
       Console.WriteLine("int to obj");
       return new Nybble(op);
   }


I would appreciate if yo could run the program and explain the output of this program from the for loop part only. Thanks.
 
Share this answer
 
Comments
Alexandru Ghiondea 5-Aug-11 2:14am    
That is the because a for loop works like this:
1. Initialize (you are assigning a constant of type int (0) to a variable of type a)
2. Check if condition is valid (you are, again, invoking the > operator with an integer (10) and a variable of type a).
3. Increment -> you have your own increment operator defined for a nybble.


For the first one, you are assigning an integer to a Nybble - so the correct implicit conversion kicks in. (The one from int to Nybble)

For the second one, you are using the < operator. The < operator works on operands of the same type. So it will convert 10 to the Nybble type and the call your < operator (the one defined on Nybble).

Alex
For WriteLine, I would suggest that your class override ToString().

For the comparison a < 10, you have not define any operator that allows to compare a Nybble with an int. It look like the compiler will convert 10 to a Nybble every time it does the comparision.

Thus you could optimize your loop by writting something like that:

C#
class Nybble
{
    // Existing stuff...

    public override ToString() { return val.ToString(); }
}

Nybble limit = new Mybble(10);
for (a = 0; a < limit; ++a)
{
    Console.Write(a.ToString() + "");
}
 
Share this answer
 
v2
The reason why you need a cast in the first case is because you are using the + operator on strings.

When you are doing "a: " + a, the compiler will try to find an operator that goes from a Nybble to a string. Since it cannot find one, it will default to calling ToString() on the object. Adding an implicit operator that returns a string will solve that issue.

The second case also works as expected. You are first initializing the a to 0. Then you are verifying that a < 10 (which means you have to call the operator again) and then you are doing Console.WriteLine((int)a) which will call the operator that converts to int.

Does this make sense to you?

Thanks,
Alex
 
Share this answer
 
Comments
jackel7777 4-Aug-11 20:45pm    
1)i am clear on the first part though in my case can I overload the implicit operator to return string and the other int.

2) I am not following through with the for loop execution. I wil apreciate if you could step in the loop and explain why the output is such.
Alexandru Ghiondea 4-Aug-11 21:10pm    
When the loop "loops" it has to verify that the value of a is smaller than 10. Because 10 is an integer, it will have to apply the implicit conversion from a (which is a Nybble) to int. So your operator gets called.

Does this explain it? If not, could you be more explicit about what your question is? What is it that you find wrong in the output?
jackel7777 4-Aug-11 21:19pm    
the out put is this:

int to obj
int to obj
obj to int
0 int to obj
obj to int
1 int to obj
obj to int
....


the beginning of teh output has int to obj printed twice and followed by obj to int...Why?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900