paraschhabra96

operator overloading

Jun 24th, 2016
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.26 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace ConsoleApplication1
  8. {
  9. class Program
  10. {
  11. class Name
  12. {
  13. private string fname, lname;
  14. public Name(string f, string l)
  15. {
  16. fname = f;
  17. lname = l;
  18. }
  19. public string retf()
  20. {
  21. return fname;
  22. }
  23. public string retl()
  24. {
  25. return lname;
  26. }
  27. public static Boolean operator==(Name n1, Name n2)
  28. {
  29. Boolean ans;
  30. if((n1.retf()==n2.retf())&&(n1.retl()==n2.retl()))
  31. ans=true;
  32. else
  33. ans=false;
  34.  
  35. return ans;
  36. }
  37. public static Boolean operator!=(Name n1, Name n2)
  38. {
  39. Boolean ans;
  40. if ((n1.retf() == n2.retf()) && (n1.retl() == n2.retl()))
  41. ans = false;
  42. else
  43. ans = true;
  44.  
  45. return ans;
  46. }
  47. }
  48. static void Main(string[] args)
  49. {
  50. Console.WriteLine("Enter The name of first member");
  51. Console.WriteLine("\nFirst Name: ");
  52.  
  53. string input1 = Console.ReadLine();
  54. Console.WriteLine("Last name: ");
  55. string input2 = Console.ReadLine();
  56.  
  57. Name A = new Name(input1, input2);
  58.  
  59. Console.WriteLine("Enter The name of second member");
  60. Console.WriteLine("\nFirst Name: ");
  61.  
  62. input1 = Console.ReadLine();
  63. Console.WriteLine("Last name: ");
  64. input2 = Console.ReadLine();
  65.  
  66. Name B = new Name(input1, input2);
  67.  
  68. if (A == B)
  69. {
  70. Console.WriteLine("\nBoth the members have same name");
  71. }
  72. else if (A != B)
  73. {
  74. Console.WriteLine("\nBoth the members have different name");
  75. }
  76. else
  77. {
  78. Console.WriteLine("\nSome error occurred");
  79. }
  80.  
  81. Console.ReadLine();
  82.  
  83. }
  84. }
  85. }
Add Comment
Please, Sign In to add comment