Q:

C#.Net find output programs (Operator Overloading) | set 3

belongs to collection: C#.Net find output programs

0

Find the output of C#.Net programs | Operator Overloading | Set 3: Enhance the knowledge of C#.Net Operator Overloading concepts by solving and finding the output of some C#.Net programs.

Question 1:

using System;

namespace Demo
{
    class Sample
    {
        int val;

        public Sample(int v)
        {
            val = v;
        }

        public static bool operator==(Sample S1, Sample S2)
        {
            bool ret = false;

            ret = (S1.val == S2.val);
            return ret;
        }

    }

    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            Sample S1 = new Sample(10);
            Sample S2 = new Sample(20);
            Sample S3 = new Sample(10);

            if (S1 == S2)
                Console.WriteLine("Matched");
            else
                Console.WriteLine("Not Matched");

            if (S1 == S3)
                Console.WriteLine("Matched");
            else
                Console.WriteLine("Not Matched");
        }
    }
}

Question 2:

using System;

namespace Demo
{
    class Sample
    {
        int val;

        public Sample(int v)
        {
            val = v;
        }

        public static bool operator==(Sample S1, Sample S2)
        {
            bool ret = false;

            ret = (S1.val == S2.val);
            return ret;
        }
	 
	 public static bool operator !=(Sample S1, Sample S2)
        {
            bool ret = false;

            ret = (S1.val != S2.val);
            return ret;
        }


    }

    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            Sample S1 = new Sample(10);
            Sample S2 = new Sample(20);
            Sample S3 = new Sample(10);

            if (S1 == S2)
                Console.WriteLine("Matched");
            else
                Console.WriteLine("Not Matched");

            if (S1 == S3)
                Console.WriteLine("Matched");
            else
                Console.WriteLine("Not Matched");
        }
    }
}

Question 3:

using System;

namespace Demo
{
    class Sample
    {
        int val;

        public Sample(int v)
        {
            val = v;
        }

        public static int operator=(Sample S)
        {
            return S.val;
        }

        public void Print()
        {
            Console.WriteLine(val);
        }
    }

    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            Sample S1 = new Sample(10);
            Sample S2;
            
            S2 = S1;

            S2.Print();
        }
    }
}

Question 4:

using System;

namespace Demo
{
    class Sample
    {
        bool val;

        public Sample(bool v)
        {
            val = v;
        }

        public static Sample operator ||(Sample S1, Sample S2)
        {
            Sample S3 = new Sample(false);

            S3.val = S1.val || S2.val;
            return S3;
        }

        public void PrintVal()
        {
            Console.WriteLine(val);
        }
    }

    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            Sample S1 = new Sample(true);
            Sample S2 = new Sample(false);
            Sample S3;

            S3 = S1 || S2;
            
            S3.PrintVal();
        }
    }
}

Question 5:

using System;

namespace Demo
{
    class Sample
    {
        int val;

        public Sample(int v)
        {
            val = v;
        }

        public static bool operator<(Sample S1, Sample S2)
        {
            bool ret = false;

            ret = (S1.val < S2.val);
            return ret;
        }

        public static bool operator >(Sample S1, Sample S2)
        {
            bool ret = false;

            ret = (S1.val > S2.val);
            return ret;
        }


    }

    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            Sample S1 = new Sample(10);
            Sample S2 = new Sample(20);
            Sample S3 = new Sample(10);

            if (S1 < S2)
                Console.WriteLine("S1 is less than S2");
            else
                Console.WriteLine("S1 is not less than S2");

            if (S1 > S3)
                Console.WriteLine("S1 is greater than S2");
            else
                Console.WriteLine("S1 is not greater than S3");
        }
    }
}

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Answer1:

Output:

main.cs(14,28): error CS0216: The operator `Demo.Sample.operator ==(Demo.Sample, Demo.Sample)' requires a matching operator `!=' to also be defined
main.cs(5,11): warning CS0660: `Demo.Sample' defines operator == or operator != but does not override Object.Equals(object o)
main.cs(5,11): warning CS0661: `Demo.Sample' defines operator == or operator != but does not override Object.GetHashCode()

Explanation:

The above program will generate a compile-time error. If we overload "==" operator, then we need to implement "!=" also.

Answer 2:

Output:

Not Matched
Matched
Press any key to continue . . .

Explanation:

In the above program, we created a Sample class that contains a data member val, constructor, operator overloaded methods for "==" and "!=".

In the Main() method we created three objects S1, S2, and S3 initialized with 10, 20, and 10 respectively. Then we compared S1 and S2 then "Not Matched" printed on the console screen, then we compared S1 and S3 then "Matched" printed on the console screen.

Answer 3:

Output:

main.cs(14,35): error CS1519: Unexpected symbol `=' in class, struct, or interface member declaration
main.cs(14,36): error CS8124: Tuple must contain at least two elements
main.cs(15,9): error CS1519: Unexpected symbol `{' in class, struct, or interface member declaration
main.cs(15,9): error CS9010: Primary constructor body is not allowed

Explanation:

The above program will generate a compile-time error because we cannot overload assignment operator "=" in C#.

Answer 4:

Output:

main.cs(14,40): error CS1519: Unexpected symbol `||' in class, struct, or interface member declaration
main.cs(15,9): error CS1519: Unexpected symbol `{' in class, struct, or interface member declaration
main.cs(15,9): error CS9010: Primary constructor body is not allowed

Explanation:

The above program will generate a compile-time error because we cannot overload "||" operator in C#.

Answer 5:

Output:

S1 is less than S2
S1 is not greater than S3
Press any key to continue . . .

Explanation:

In the above program, we created a Sample class that contains a data member val, constructor, operator overloaded methods for "<" and ">".

In the Main() method we created three objects  S1, S2, and S3 initialized with 10, 20, and 10 respectively. Then we compared S1 and S2 using "<" operator then "S1 is less than S2" printed on the console screen, then we compared S1 and S3 using ">" operator then "S1 is not greater than S3" printed on the console screen.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C#.Net find output programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C#.Net find output programs (Namespace) | set 1... >>
<< C#.Net find output programs (Operator Overloading)...