Find the output of C#.Net programs | Operator Overloading | Set 1: 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 count;
public static void operator++()
{
count = count+1;
}
public void PrintCount()
{
Console.WriteLine(count);
}
}
class Program
{
//Entry point of the program
static void Main(string[] args)
{
Sample S = new Sample();
++S;
++S;
++S;
S.PrintCount();
}
}
}
Question 2:
using System;
namespace Demo
{
class Sample
{
int count;
public 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();
++S;
++S;
++S;
S.PrintCount();
}
}
}
Question 3:
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();
++S;
++S;
++S;
S.PrintCount();
}
}
}
Question 4:
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();
S++;
S++;
S++;
S.PrintCount();
}
}
}
Question 5:
using System;
namespace Demo
{
class Sample
{
int count;
public static Sample operator ++()
{
this.count = this.count + 1;
return this;
}
public void PrintCount()
{
Console.WriteLine(count);
}
}
class Program
{
//Entry point of the program
static void Main(string[] args)
{
Sample S = new Sample();
S++;
S++;
S++;
S.PrintCount();
}
}
}
Answer 1:
Output:
Explanation:
The above program will generate a compile-time error, because if you want to overload a unary operator then you need to pass at least one argument of class type. The correct overloaded method is given below:
Answer 2:
Output:
Explanation:
The above program will generate a compile-time error. In the above program, we created a method to overload increment operator '++', In the C# method for operator overloading must be declared as static and public. Here, we did not define the overloaded method as static.
Answer 3:
Output:
Explanation:
The above program will print 3 on the console screen. In the program we created a Sample class that contains a data member count, here we implemented a method to overload increment operator '++', And we created PrintCount() method to print the value of data member count.
Let's look to the Main() method of Program class, the Main() method is the entry point of the program, here we created the object of Sample class and use pre-increment operator with object S and finally print the value of data member count using PrintCount() method of Sample class.
Answer 4:
Output:
Explanation:
The above program will print 3 on the console screen. In the program we created a Sample class that contains a data member count, here we implemented a method to overload increment operator '++', And we created PrintCount() method to print the value of data member count.
Let's look to the Main() method of Program class, the Main() method is the entry point of the program, here we created an object of Sample class and use post-increment operator with object S and finally print the value of data member count using PrintCount() method of Sample class.
Answer 5:
Output:
Explanation:
The above program will generate the compile-time error, because if you want to overload a unary operator then you need to pass at least one argument of class type, and this object cannot be used in a static method.
need an explanation for this answer? contact us directly to get an explanation for this answer