Объясните, пожалуйста код - C#

Узнай цену своей работы

Формулировка задачи:

Учу язык недавно, читаю Шилдта. В теме о перегрузке конструкторов есть пример программы:
Листинг программы
  1. // A stack class for characters.
  2. using System;
  3. class Stack {
  4. // These members are private.
  5. char[] stck; // holds the stack
  6. int tos; // index of the top of the stack
  7. // Construct an empty Stack given its size.
  8. public Stack(int size) {
  9. stck = new char[size]; // allocate memory for stack
  10. tos = 0;
  11. }
  12. // Construct a Stack from a stack.
  13. public Stack(Stack ob) {
  14. // Allocate memory for stack.
  15. stck = new char[ob.stck.Length];
  16. // Copy elements to new stack.
  17. for(int i=0; i < ob.tos; i++)
  18. stck[i] = ob.stck[i];
  19. // Set tos for new stack.
  20. tos = ob.tos;
  21. }
  22. // Push characters onto the stack.
  23. public void Push(char ch) {
  24. if(tos==stck.Length) {
  25. Console.WriteLine(" -- Stack is full.");
  26. return;
  27. }
  28. stck[tos] = ch;
  29. tos++;
  30. }
  31. // Pop a character from the stack.
  32. public char Pop() {
  33. if(tos==0) {
  34. Console.WriteLine(" -- Stack is empty.");
  35. return (char) 0;
  36. }
  37. tos--;
  38. return stck[tos];
  39. }
  40. // Return true if the stack is full.
  41. public bool IsFull() {
  42. return tos==stck.Length;
  43. }
  44. // Return true if the stack is empty.
  45. public bool IsEmpty() {
  46. return tos==0;
  47. }
  48. // Return total capacity of the stack.
  49. public int Capacity() {
  50. return stck.Length;
  51. }
  52. // Return number of objects currently on the stack.
  53. public int GetNum() {
  54. return tos;
  55. }
  56. }
  57. // Demonstrate the Stack class.
  58. class StackDemo {
  59. static void Main() {
  60. Stack stk1 = new Stack(10);
  61. char ch;
  62. int i;
  63. // Put some characters into stk1.
  64. Console.WriteLine("Push A through J onto stk1.");
  65. for(i=0; !stk1.IsFull(); i++)
  66. stk1.Push((char) ('A' + i));
  67. // Create a copy of stck1.
  68. Stack stk2 = new Stack(stk1);
  69. // Display the contents of stk1.
  70. Console.Write("Contents of stk1: ");
  71. while( !stk1.IsEmpty() ) {
  72. ch = stk1.Pop();
  73. Console.Write(ch);
  74. }
  75. Console.WriteLine();
  76. Console.Write("Contents of stk2: ");
  77. while ( !stk2.IsEmpty() ) {
  78. ch = stk2.Pop();
  79. Console.Write(ch);
  80. }
  81. Console.WriteLine("\n");
  82. }
  83. }
Подскажите, пожалуйста, что делает команда на 19 строке: stck = new char[ob.stck.Length];

Решение задачи: «Объясните, пожалуйста код»

textual
Листинг программы
  1. public Stack(Stack ob) {
  2.     stck = ob.stck;
  3.     tos = ob.tos;
  4. }

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

11   голосов , оценка 4 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут
Похожие ответы