动态数组类及其模板
先定义point类,再定义由point类的动态数组
#include <iostream>
#include <cassert>
using namespace std;
class Point {
private:
int x, y;
public:
Point() : x(0), y(0) {cout << "Point Default Constructor called." << endl;}
Point(int x, int y) : x(x), y(y) {cout << "Point Constructor called." << endl;}
~Point() { cout << "Point Destructor called." << endl; }
int getX() const { return x; }
int getY() const { return y; }
void move(int newX, int newY) {
x = newX;
y = newY;
}
void show(){ cout << "x = " << x << " , y = " << y << endl; }
friend ostream& operator<<(ostream& os, const Point& p);
};
ostream& operator<<(ostream& os, const Point& p) {
cout << "(" << p.x << "," << p.y << ")";
return os;
}
class ArrayOfPoints { //动态数组类
private:
Point *points; //指向动态数组首地址
int size; //数组大小
public:
ArrayOfPoints(int size) : size(size) {
points = new Point[size];
cout << "ArrayOfPoints Constructor called." << endl;
}
~ArrayOfPoints() {
cout << "Deleting...ArrayOfPoints Destructor called." << endl;
delete[] points;
}
Point& element(int index) {
assert(index >= 0 && index < size);
return points[index];
}
Point &operator[](int index) {
assert(index >= 0 && index < size);
return points[index];
}
};
int main() {
ArrayOfPoints points(10); //创建数组对象
//points.element(0).move(5, 0); //访问数组元素的成员
//points.element(1).move(15, 20); //访问数组元素的成员
points[0].show();
points[2].move(3,4);
points[2].show();
cout << points[2] << endl;
return 0;
}
这个是动态数组类模板
#include <cstring>
#include <iostream>
using namespace std;
class Point {
private:
int x, y;
public:
Point(int _x = 0, int _y = 0);// constructor
~Point();// deconstructor
friend ostream& operator<<(ostream& os, const Point& p);// cout << overload
Point(const Point &p){
x = p.x;y = p.y;
cout << "Point copy constructor" << endl;
}
};
Point::Point(int _x, int _y) {
this->x = _x;
this->y = _y;
cout << "Point is called!" << endl;
}
Point::~Point() { cout << "~Point is called!" << endl; }
ostream& operator<<(ostream& os, const Point& p) {
cout << "(" << p.x << "," << p.y << ")";
return os;
}
template <typename T>
class DynamicArray {
private:
T* array; // pointer
unsigned int mallocSize; // the length of dynamic array
public:
//Constructors// mallocSize=length; 设置每个元素的初始内容是 content;
DynamicArray(unsigned length, const T &content) ;
//Copy Constructor
DynamicArray(const DynamicArray<T> & anotherDA ) ;
// Destructors
~DynamicArray();
unsigned int capacity() const;
//自己定义个operator[] const 重载
T& operator[](unsigned int i) ;
//自己定义个 operator = 重载
DynamicArray<T>& operator=(const DynamicArray<T>& anotherDA);
};
template <typename T>
DynamicArray<T>::DynamicArray(unsigned length, const T& content) {
this->mallocSize = length;
this->array = new T[this->mallocSize];
cout << "new T[" << this->mallocSize << "] malloc " << this->mallocSize
<< "*" << sizeof(T) << "=" << this->mallocSize * sizeof(T)
<< " bytes memory in heap" << endl;
for (int i = 0; i < length; ++i) {this->array[i] = content;}
};
template <typename T>
DynamicArray<T>::~DynamicArray() {
cout << "delete[] array free " << this->mallocSize << "*" << sizeof(T)
<< "=" << this->mallocSize * sizeof(T) << " bytes memory in heap" << endl;
delete[] array;
};
template <typename T>
DynamicArray<T>::DynamicArray(const DynamicArray<T>& anotherDA) {
cout << "DynamicArray Copy Constructor is called" << endl;
this->mallocSize = anotherDA.mallocSize;
this->array = new T[this->mallocSize];
for (int i = 0; i < this->mallocSize; ++i)
this->array[i] = anotherDA.array[i];
};
template <typename T>
DynamicArray<T>& DynamicArray<T>::operator=(const DynamicArray<T>& anotherDA) {
cout << "DynamicArray operator = is called"<< endl;
if (this == &anotherDA) return *this;//等号两边相等,地址都是一样的,就没必要继续复制了
if (this->array) delete[] this->array; //如果this->array是非空有东西的,就先删除再重新new,赋值
this->mallocSize = anotherDA.mallocSize;
this->array = new T[this->mallocSize];
for (int i = 0; i < this->mallocSize; ++i)
this->array[i] = anotherDA.array[i];
return *this;
}
template <typename T>
unsigned int DynamicArray<T>::capacity() const {return this->mallocSize;}
template <typename T>
T& DynamicArray<T>::operator[](unsigned int i) {return this->array[i];}
int main(){
int length = 6,i;
DynamicArray<Point> iarray(length,Point(3));
cout << "111111111111111111111111111111" << endl;
DynamicArray<Point> iarray2(iarray);
cout << "222222222222222222222222222222" << endl;
DynamicArray<Point> iarray3(iarray2);
cout << "33333333333333333333333333333" << endl;
cout << endl;
for(i=0;i < length;i++) {cout << iarray3[i] <<" ";}
cout << endl;
for(i=0;i < length;i++) {iarray[i] = Point(i,i+1);}
cout << "4444444444444444444444444444" << endl;
iarray3 = iarray2; //因为两个是不同的动态数组,所以要先delete再new,逐个赋值
cout << "55555555555555555555555555555" << endl;
iarray2 = iarray;
cout << endl;
for(i=0;i<length;i++) {
cout << iarray3[i] <<" ";
}
cout << endl;
cout << "66666666666666666666666666666" << endl;
return 0;
}
结果
Point is called!
Point is called!
Point is called!
Point is called!
Point is called!
Point is called!
Point is called!
new T[6] malloc 6*8=48 bytes memory in heap
~Point is called!
111111111111111111111111111111
DynamicArray Copy Constructor is called
Point is called!
Point is called!
Point is called!
Point is called!
Point is called!
Point is called!
222222222222222222222222222222
DynamicArray Copy Constructor is called
Point is called!
Point is called!
Point is called!
Point is called!
Point is called!
Point is called!
33333333333333333333333333333
(3,0) (3,0) (3,0) (3,0) (3,0) (3,0)
Point is called!
~Point is called!
Point is called!
~Point is called!
Point is called!
~Point is called!
Point is called!
~Point is called!
Point is called!
~Point is called!
Point is called!
~Point is called!
4444444444444444444444444444
DynamicArray operator = is called
~Point is called!
~Point is called!
~Point is called!
~Point is called!
~Point is called!
~Point is called!
Point is called!
Point is called!
Point is called!
Point is called!
Point is called!
Point is called!
55555555555555555555555555555
DynamicArray operator = is called
~Point is called!
~Point is called!
~Point is called!
~Point is called!
~Point is called!
~Point is called!
Point is called!
Point is called!
Point is called!
Point is called!
Point is called!
Point is called!
(3,0) (3,0) (3,0) (3,0) (3,0) (3,0)
66666666666666666666666666666
delete[] array free 6*8=48 bytes memory in heap
~Point is called!
~Point is called!
~Point is called!
~Point is called!
~Point is called!
~Point is called!
delete[] array free 6*8=48 bytes memory in heap
~Point is called!
~Point is called!
~Point is called!
~Point is called!
~Point is called!
~Point is called!
delete[] array free 6*8=48 bytes memory in heap
~Point is called!
~Point is called!
~Point is called!
~Point is called!
~Point is called!
~Point is called!