In a dynamic array class, access to an array element through a member function of a class allows you to check whether the subscript is out of bounds before each visit, making it possible to detect an error that crosses the bounds of an array. This check can be done through the Assert for C + +. Assert means "assertion", a macro defined in the Cassert header file of standard C + + to determine whether the value of a conditional expression is true, and if it is not true, the program aborts and an error is reported, which makes it easy to locate the error.
The following is a simple example of a dynamic array class
#include <iostream> #include <cassert> using namespace std; Class point{Public:point (): X (0), y (0) {cout<< "default constructor called."
<<endl; Point (int x,int y): x (x), Y (y) {cout<< "constructor called."
<<endl; } ~point () {cout<< "destructor called."
<<endl;
int Getx () const {return x;}
int gety () const {return y;}
void movee (int newx,int newy) {x=newx;
Y=newy;
} Private:int X,y;
};
Class Arrayofpoints {public:arrayofpoints (int sizee): Sizee (Sizee) {points=new Point[sizee];
} ~arrayofpoints () {cout<< "deleting ..." <<endl;
Delete[] points; //Get the array element point (int index) {assert (Index>=0&&index<sizee) subscript index,//If the array subscript is out of bounds,
Program abort return Points[index];
} Private:point *points;
int Sizee;
};
int main () {int countt; cout<< "Please enter the count of points:";
cin>>countt;
Arrayofpoints points (Countt);//Create Object array points.element (0). Movee (5,0);//access members of the array element points.element (1). Movee (15,20);
return 0;
}
Personal understanding: Simply by the combination of classes, the realization of a class point is another element of the array class arrayofpoints, if the understanding is not in place, welcome to correct.