Home »
C#.Net »
C#.Net find output programs
C#.Net find output programs (Operator Overloading) | set 2
Find the output of C#.Net programs | Operator Overloading | Set 2: Enhance the knowledge of C#.Net Operator Overloading concepts by solving and finding the output of some C#.Net programs.
Submitted by Nidhi, on February 10, 2021
Question 1:
using System;
namespace Demo
{
class Sample
{
int count;
public static Sample operator++(Sample C)
{
C.count = C.count + 1;
return C;
}
public void PrintCount()
{
Console.WriteLine(count);
}
}
class Program
{
//Entry point of the program
static void Main(string[] args)
{
Sample S = new Sample();
Sample.operator++(S);
Sample.operator++(S);
S.PrintCount();
}
}
}
Output:
main.cs(28,19): error CS1041: Identifier expected, `operator' is a keyword
main.cs(29,19): error CS1041: Identifier expected, `operator' is a keyword
Explanation:
The above program will generate compile-time errors due to the below statements in the Main() method,
Sample.operator++(S);
Sample.operator++(S);
We cannot call the operator overloaded method using the operator keyword in C#, it can be done in C++.
Question 2:
using System;
namespace Demo
{
class Sample
{
int num;
public Sample(int n)
{
num = n;
}
public Sample operator%(Sample S)
{
Sample temp = new Sample(0);
temp.num = this.num % S.num;
return temp;
}
public void Print()
{
Console.WriteLine(num);
}
}
class Program
{
//Entry point of the program
static void Main(string[] args)
{
Sample S1 = new Sample(20);
Sample S2 = new Sample(3);
Sample S3;
S3 = S1 % S2;
S3.Print();
}
}
}
Output:
main.cs(14,23): error CS1019: Overloadable unary operator expected
Explanation:
The above program will generate a compile-time error. In the above program we implemented a method to overload binary operator '%', but we passed only one argument in the operator method, to overload binary operator we need to pass two arguments of class type.
Question 3:
using System;
namespace Demo
{
class Sample
{
int num;
public Sample(int n)
{
num = n;
}
public static Sample operator %(Sample S1, Sample S2)
{
Sample S3 = new Sample(0);
S3.num = S1.num % S2.num;
return S3;
}
public void Print()
{
Console.WriteLine(num);
}
}
class Program
{
//Entry point of the program
static void Main(string[] args)
{
Sample S1 = new Sample(20);
Sample S2 = new Sample(3);
Sample S3;
S3 = S1 % S2;
S3.Print();
}
}
}
Output:
2
Press any key to continue . . .
Explanation:
The above program will print 2 on the console screen. In the above program we created a class Sample that contains a data member num, default constructor, operator overloaded method, and Print() method.
Here, we overloaded modulus operator '%', and the Print() method is used to print the value of data member num.
In the Main() method we created two objects S1 and S2 initialized with 20 and 3 respectively. We also created a reference S3,
S3 = S1 % S2;
Using the above statement data member of S3 will be 2 because 20%3 is 2. Then we printed the value of data member num using the Print() method.
Question 4:
using System;
namespace Demo
{
class Sample
{
bool val;
public Sample(bool v)
{
val = v;
}
public static Sample operator !(Sample C)
{
C.val = !C.val;
return C;
}
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);
S1 = !S1;
S2 = !S2;
S1.PrintVal();
S2.PrintVal();
}
}
}
Output:
False
True
Press any key to continue . . .
Explanation:
In the program we created a Sample class that contains a data member val, here we implemented a method to overload unary operator '!', And we created PrintVal() method to print the value of data member val.
Let's look to the Main() method of Program class, The Main() method is the entry point of the program, here we created two objects S1 and S2 of Sample class initialized with "True" and "False" respectively.
S1 = !S1;
S2 = !S2;
Here, we used unary operator '!' with S1 and S2 that will change the value of data member val. Then we called PrintVal() method with both objects then modified values of val will be printed on the console screen.
Question 5:
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();
}
}
}
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#.