Dynamic Array Class

Source: Internet
Author: User
Tags assert

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.