Advertisement
Guest User

Untitled

a guest
Dec 7th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.47 KB | None | 0 0
  1. //Punct.cs
  2. //
  3.  
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9.  
  10. namespace _028_mostenire
  11. {
  12. class Punct
  13. {
  14. protected int x, y;
  15. // le-am declarat protected pentru ca ele sa fie private, dar accesibile in clasele derivate din Punct
  16. public Punct(int _x, int _y)
  17. {
  18. x = _x;
  19. y = _y;
  20. }
  21. public int X
  22. {
  23. get { return x; }
  24. }
  25. public int Y
  26. {
  27. get { return y; }
  28. }
  29. public override string ToString()
  30. {
  31. return "(" + X + "," + Y + ")";
  32. }
  33. }
  34.  
  35. //Clasa Cerc este clasa derivata din Punct
  36. class Cerc : Punct
  37. {
  38. private double raza;
  39. // constructor
  40. public Cerc(int _x, int _y, double R)
  41. : base(_x, _y)
  42. {
  43. raza = R;
  44. }
  45. protected double Raza
  46. {
  47. get { return raza; }
  48. }
  49.  
  50. public double Diametru
  51. {
  52. get { return 2 * raza; }
  53. }
  54.  
  55. public double LungimeCerc()
  56. {
  57. return Math.PI * Diametru;
  58. }
  59.  
  60. /*
  61. * Am definit functie virtuala pentru ca eventualele clase derivate din clasa Cerc
  62. * sa poate redefini (override) metoda Aria()
  63. * */
  64.  
  65. public virtual double Aria()
  66. {
  67. return Math.PI * Raza * Raza;
  68. }
  69.  
  70. public override string ToString()
  71. {
  72. return "Cerc cu centrul ( " + x + "," + y + " ) si raza " + raza + "\n";
  73. }
  74. }
  75. class Cilindru : Cerc
  76. {
  77. protected double h; // initializarea cilindrului
  78. //constructor
  79. public Cilindru(int _x, int _y, double R, double _h)
  80. : base(_x, _y, R)
  81. {
  82. h = _h;
  83. }
  84. public override double Aria()
  85. {
  86. return 2 * base.Aria();
  87. }
  88. }
  89. }
  90.  
  91.  
  92. //Program.cs
  93. //
  94.  
  95. using System;
  96. using System.Collections.Generic;
  97. using System.Linq;
  98. using System.Text;
  99. using System.Threading.Tasks;
  100.  
  101. namespace _028_mostenire
  102. {
  103. class Program
  104. {
  105. static void Main(string[] args)
  106. {
  107. Punct P = new Punct(1, 1);
  108. Cerc C = new Cerc(3, 3, 2);
  109. Console.WriteLine(P);
  110. Console.WriteLine(C);
  111. Console.WriteLine("Aria = " + C.Aria());
  112.  
  113. }
  114. }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement