fork(1) download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. const int DefaultSize = 10;
  5.  
  6. class Animal
  7. {
  8. public:
  9. Animal(int);
  10. Animal();
  11. ~Animal() { }
  12.  
  13. int GetWeight() const { return itsWeight; }
  14. void Display() const { cout << itsWeight; }
  15.  
  16. private:
  17. int itsWeight;
  18.  
  19. };
  20.  
  21. Animal::Animal(int weight): itsWeight(weight)
  22. { }
  23.  
  24. Animal::Animal(): itsWeight(0)
  25. { }
  26.  
  27. template <class T>
  28. class Array
  29. {
  30. public:
  31. Array(int itsSize = DefaultSize);
  32. Array( Array &rhs);
  33. ~Array()
  34. {
  35. delete [] pType;
  36. }
  37.  
  38. Array& operator= (const Array&);
  39. T& operator[] (int offset)
  40. {
  41. return pType[offset];
  42. }
  43.  
  44. int GetSize () const
  45. {
  46. return itsSize;
  47. }
  48. template< class U >
  49. friend ostream& operator<< (ostream&, Array<U>&);
  50.  
  51. private:
  52. T *pType;
  53. int itsSize;
  54. };
  55.  
  56.  
  57. template <class T>
  58. ostream& operator<< (ostream& output, Array<T>& theArray)
  59. {
  60. for (int i = 0; i < theArray.GetSize(); ++i)
  61. {
  62. output << "[" << i << "]" << theArray[i] << endl;
  63. }
  64. return output;
  65. }
  66.  
  67.  
  68. template <class T>
  69. Array<T>::Array(int size): itsSize(size)
  70. {
  71. pType = new T[size];
  72. for (int i = 0; i < size; ++i)
  73. pType[i] = 0;
  74. }
  75.  
  76. //конструктор копирования
  77. template <class T>
  78. Array<T>::Array(Array &rhs)
  79. {
  80. itsSize = rhs.GetSize();
  81. pType = new T[itsSize];
  82. for (int i = 0; i < itsSize; ++i)
  83. {
  84. pType[i] = rhs[i];
  85. }
  86. }
  87.  
  88. //оператор присваения
  89. template <class T>
  90. Array<T>& Array<T>::operator= (const Array &rhs)
  91. {
  92. if (this == &rhs)
  93. return *this;
  94.  
  95. delete [] pType;
  96. itsSize = rhs.GetSize();
  97. pType = new T[itsSize];
  98.  
  99. for (int i = 0; i < itsSize; ++i)
  100. {
  101. pType[i] = rhs[i];
  102. }
  103.  
  104. return *this;
  105. }
  106.  
  107. ///////////////////////////////////////////////////////////////////
  108.  
  109. int main()
  110. {
  111. bool Stop = false;
  112. int offset, value;
  113. Array<int> theArray;
  114.  
  115. while (!Stop)
  116. {
  117. cout << "Enter an offset (0-9) ";
  118. cout << "and a value. (-1 to stop): ";
  119. cin >> offset >> value;
  120.  
  121. if (offset < 0)
  122. break;
  123.  
  124. if(offset > 9)
  125. {
  126. cout << "***Please use value between 0 and 9*** \n";
  127. continue;
  128. }
  129.  
  130. theArray[offset] = value;
  131. }
  132. cout << "\n Here's the entire array: \n";
  133. cout << theArray << endl;
  134.  
  135.  
  136. return 0;
  137. }
Success #stdin #stdout 0s 3476KB
stdin
3 2
-1 0
stdout
Enter an offset (0-9) and a value. (-1 to stop): Enter an offset (0-9) and a value. (-1 to stop): 
 Here's the entire array: 
[0]0
[1]0
[2]0
[3]2
[4]0
[5]0
[6]0
[7]0
[8]0
[9]0