Освой самостоятельно С++ за 21 день.
Шрифт:
91:
92: int main
93: {
94: bool Stop = false; // признак для цикла
95: int offset, value;
96: Array<int> theArray;
97:
98: while (!Stop)
99: {
100: cout << "Enter an offset (0-9) ";
101: cout << "and a value. (-1 to stop): ";
102: cin >> offset >> value;
103:
104: if (offset < 0)
105: break;
106:
107: if (offset > 9)
108: {
109: cout << "***Please use values between 0 and 9.***\n";
110: continue;
111: }
112:
113: theArray[offset] = value;
114: }
115:
116: cout << "\nHere's the entire array:\n";
117: cout << theArray << endl;
118: return 0;
119: }
Результат:
Enter an offset (0 -9 and а value. (-1 to stop) 1 10
Enter an offset (0 -9 and а value. (-1 to stop) 2 20
Enter an offset (0 -9 and а value. (-1 to stop) 3 30
Enter an offset (0 -9 and а value. (-1 to stop) 4 40
Enter an offset (0 -9 and а value. (-1 to stop) 5 50
Enter an offset (0 -9 and а value. (-1 to stop) 6 60
Enter an offset (0 -9 and а value. (-1 to stop) 7 70
Enter an offset (0 -9 and а value. (-1 to stop) 8 80
Enter an offset (0 -9 and а value. (-1 to stop) 9 90
Enter an offset (0 -9 and а value. (-1 to stop) 1С 10
***Please use values between 0 and 9.* >>*
Enter an offset (0 -9) and а value. (-1 to stop) -1 -1
Here's the entire array:
[0] 0
[1] 10
[2] 20
[3] 30
[4] 40
[5] 50
[6] 60
[7] 70
[8] 80
[9] 90
Анализ:
Использование экземпляров шаблона
С экземплярами шаблона можно обращаться так же, как с любыми другими типами данных. Их можно передавать в функции как ссылки или как значения и возвращать как результат выполнения функции (тоже как ссылки или как значения). Способы передачи экземпляров шаблона показаны в листинге 19.5.
Листинг 19.5. Передача в функцию экземпляра шаблона
1: #include <iostream.h>
2:
3: const int DefaultSize = 10;
4:
5: // Обычный класс, из объектов которого будет состоять массив
6: class Animal
7: {
8: public:
9: // конструкторы
10: Animal(int);
11: Animal;
12: ~Animal;
13:
14: //
методы доступа15: int GetWeight const { return itsWeight; }
16: void SetWeight(int theWeight) { itsWeight = theWeight; }
17:
18: // дружественные операторы
19: friend ostream& operator<< (ostream&, const Animal&);
20:
21: private:
22: int itsWeight;
23: };
24:
25: // оператор вывода объектов типа Animal
26: ostream& operator<<
27: (ostream& theStream, const Animal& theAnimal)
28: {
29: theStream << theAnimal.GetWeight;
30: return theStream;
31: }
32:
33: Animal::Animal(int weight):
34: itsWeight(weight)
35: {
36: // cout << "Animal(int)\n";
37: }
38:
39: Animal::Animal:
40: itsWeight(0)
41: {
42: // cout << "Animal\n";
43: }
44:
45: Animal::~Animal
46: {
47: // cout << "Destroyed an animal...\n";
48: }
49:
50: template <class T> // объявление шаблона и параметра
51: class Array // параметризованный класс
52: {
53: public:
54: Array(int itsSlze = DefaultSize);
55: Array(const Array &rhs);
56: ~Array { delete [] pType; }
57:
56: Array& operator=(const Array&);
59: T& operator[](int offSet) { return pType[offSet]; }
60: const T& operator[](int offSet) const
61: { return pType[offSet]; }
62: int GetSize const { return itsSize; }
63:
64: // функция-друг
65: friend ostream& operator<< (ostream&, const Array<T>&);
66:
67: private:
68: T *рТуре;
69: int itsSize;
70: };
71:
70: template <class T>
72: ostream& operator<< (ostream& output, const Array<T>& theArray)
73: {
74: for (int i = 0; i<theArray.GetSize; i++)
75: output << "[" << i << "] " << theArray[i] << endl;
76: return output;
77: }
78:
79: // Ряд выполнений...
80:
81: // выполнение конструктора
82: template <class T>
83: Array<T>::Array(int size):
84: itsSize(size)
85: {
86: рТуре = new T[size];
67: for (int i = 0; i<size; i++)
88: pType[i] = 0;
89: }
90:
91: // конструктор-копировщик
92: template <class T>
93: Array<T>::Array(const Array &rhs)
94: {
95: itsSize = rhs.GetSize;
96: рТуре = new T[itsSize];
97: for (int i = 0; i<itsSize; i++)