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");
}
}
}
Answer1:
Output:
Explanation:
The above program will generate a compile-time error. If we overload "==" operator, then we need to implement "!=" also.
Answer 2:
Output:
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:
Explanation:
The above program will generate a compile-time error because we cannot overload assignment operator "=" in C#.
Answer 4:
Output:
Explanation:
The above program will generate a compile-time error because we cannot overload "||" operator in C#.
Answer 5:
Output:
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