5
1. ++, , .. , - , .
2. .
1. . :
, . , - . , ( ). .
2. . :
, . , . , . . , , . . , . . .
3. . :
, . , , , , , , . , . , . . , .
4. . :
, . :
;
;
.
:
;
, , , , ;
, , , , ;
|
|
, , , .
5. . :
, . :
;
;
;
.
:
;
;
;
;
.
6. . :
, .
:
;
;
;
;
.
:
;
, ; ;
, .
7. . :
, . :
;
;
;
.
:
;
;
;
.
8. . , .
9. . , . , .
10. . . , . , .
. , ( , ).
#include <iostream>
const int DefaultSize = 10;
// Animal,
//
class Animal
{ public:
Animal(int);
Animal();
~Animal() {}
int GetWeight() const { return itsWeight;)
void Display() const { std::cout < < itsWeight; }
private:
int itsWeight; };
Animal:: Animal(int weight): itsWeight(weight) {}
Animal::Animal(): itsWeight(0) { }
template <class T> // , ,
|
|
// T
//
class Array
{ public:
//
Array(int itsSize = DefaultSize); //
// ,
// DefaultSize
Array(const Array &rhs);
~Array() { delete [ ] pType; }
//
Array& operator = (const Array&); //
//
T& operator [ ] (int offSet) { return pType [ offSet ]; }
//
const T& operator [ ] (int offSet) const { return pType [ offset ]; }
// -
int GetSize() const { return itsSize; }//
private:
*;
int itsSize; };
// ...
//
template <class T>
<>:: Array(int size):
itsSize(size)
{ = new T[ size ];
for (int i = 0; i < size; i++)
pType[ i ] = 0; }
//
template <class T>
Array<T>:: Array(const Array &rhs)
{ itsSize = rhs.GetSize();
pType = new T[ itsSize ];
for (int i = 0; i < itsSize; i++)
pType[ i ] = rhs[ i ]; }
// operator =
template <class T>
Array<T>& Array<T>:: operator=(const Array &rhs)
{ if (this == &rhs)
return *this;
delete [ ] pType;
itsSize = rhs. GetSize();
pType = new T[ itsSize ];
for (int i = 0; i < KitsSize; i++)
pType [ i ] = rhs [ i ];
return *this; }
//
int main()
{ Array<int> theArray; //
Array<Animal> theZoo; //
Animal *pAnimal;
//
for (int i = 0; i < theArray. GetSize(); i++)
{ theArray[ i ] = i*2;
pAnimal = new Animal(i*3);
theZoo[ i ] = *pAnimal;
delete pAnimal;
}
//
for (int j = 0; j < theArray.GetSize(); j++)
{
Std:: cout << "theArray ["<< j << "]:\t";
Std:: cout << theArray[j] << "\t\t";
Std:: cout << "theZoo [" << j << "]:\t";
theZoo[j].Display();
std:: cout << std:: endl;
}
return 0;
}
theArray [0]: 0 theZoo [0]: 0
theArray [1]: 2 theZoo [0]: 3
theArray [2]: 4 theZoo [0]: 6
theArray [3]: 6 theZoo [0]: 9
theArray [4]: 8 theZoo [0]: 12
theArray [5]: 10 theZoo [0]: 15
theArray [6]: 12 theZoo [0]: 18
theArray [7]: 14 theZoo [0]: 21
theArray [8]: 16 theZoo [0]: 24
theArray [9]: 18 theZoo [0]: 27