











Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
A Constructor is a method for a class that gets called automatically whenever an object of the class is created.
Typology: Exercises
1 / 19
This page cannot be seen from the preview
Don't miss anything!
A Destructor is a method for a class that gets called automatically whenever an object of the class is deleted. Its name is always the class name preceded by a ~, it takes no arguments, and there is no return value, not even void. class Point{ private : int x, y; public : Point(); ~Point(); }; destructor
Constructor and Destructor Definition
Point::Point(){ x = y = 0; } Point::Point( int x0, int y0){ x = x0; y = y0; } Point::Point( const Point &pt){ x = pt.x; y = pt.y; }
Point::~Point(){ } default constructor initializing constructor copy constructor destructor, note there is nothing to do in this case
A Constructor is called whenever a variable of its class is created. Here are some examples Point p0; Point p1(10, 50); Point plist[20]; Point p2(pt); Point *pt0 = new Point; Point *pt1 = new Point(10, 50); Point *pt2 = new Point(pt); Point *ptable = new Point[20]; default constructor, p0 will have coordinates (0, 0) initializing constructor, p1 will have coordinates (10, 50) default constructor, each element of array plist will have coordinates (0, 0) default constructor, *pt0 will have coordinates (0, 0) initializing constructor, *pt1 will have coordinates (10, 50) default constructor, every element of array ptable will have coordinates (0, 0) copy constructor, p2 will have the same coordinates as the Point pt copy constructor, *pt2 will have the same coordinates as the Point pt
Point::Point( int x0 = 0 , int y0 = 0 ): x(x0), y(y0){ }
Point p0;
Point p1(10, 50);
Point p2(10);
Example Polygon Class Initialization of Static Variable int Polygon::numpolygons = 0 ;
Example Polygon Class Default Constructor // Default constructor makes sure vertices pointer is NULL // and that the number of vertices is 0. // It also increments the count of the number of polygons. Polygon::Polygon(): vertices( NULL ), nverts( 0 ){ numpolygons++; } initializer list increment count of polygons
Example Polygon Class Adding a Single Vertex void Polygon::addvertex( const Point &pt){ Point *oldverts = vertices; vertices = new Point[++nverts]; for ( int i = 0 ; i < nverts - 1 ; i++) vertices[i] = oldverts[i]; vertices[nverts - 1 ] = pt; delete []oldverts; } increment number of vertices and allocate space for additional vertex copy the old vertex array to the new vertex array store the new vertex at the end of the new array free up the space for the old array
Example Polygon Class Accessing and Setting Vertices Point *Polygon::getvertices() const { Point *vtxcopy = new Point[nverts]; for ( int i = 0 ; i < nverts; i++) vtxcopy[i] = vertices[i]; return vtxcopy; } void Polygon::setvertices( int n, Point *verts){ if (n != nverts){ delete []vertices; nverts = n; vertices = new Point[nverts]; } for ( int i = 0 ; i < nverts; i++) vertices[i] = verts[i]; } allocate space for a copy of the vertices copy the vertex array return the copied array if the number of new vertices is different from the current number, delete the old vertices array and make a new one the right size copy in the contents of the verts array
Code Using Polygon Class #include “Polygon.h" #include
Code Would be Distributed Over Several Files Point.cpp Polygon.cpp Declarations Definitions Usage Point.h trypolygon.cpp Polygon.h
Download from the schedule page