📄 polygon.cpp
字号:
// Chapter 8 of C++ How to Program
// Coding Exercises
// polygon.cpp
// class Polygon definition
class Polygon {
public:
Polygon( int = 0, int = 0 );
~Polygon();
void addVertex( int, int );
int getNumberOfVertices() const;
private:
int *xPts;
int *yPts;
int numberOfVertices;
}; // end class Polygon
// default constructor
Polygon::Polygon( int x, int y )
{
xPts = new int[ 1 ];
yPts = new int[ 1 ];
xPts[ 0 ] = x;
yPts[ 0 ] = y;
numberOfVertices = 1;
} // end class Polygon constructor
// destructor
Polygon::~Polygon()
{
delete [] xPts;
delete [] yPts;
} // end class Polygon destructor
// function addVertex definition
void Polygon::addVertex( int x, int y )
{
int *copyX = new int[ numberOfVertices + 1 ];
int *copyY = new int[ numberOfVertices + 1 ];
for ( int i = 0; i < numberOfVertices; i++ ) {
copyX[ i ] = xPts[ i ];
copyY[ i ] = yPts[ i ];
} // end for
copyX[ numberOfVertices ] = x;
copyY[ numberOfVertices ] = y;
delete [] xPts;
delete [] yPts;
xPts = copyX;
yPts = copyY;
numberOfVertices++;
} // end function addVertex
// function getNumberOfVertices
int Polygon::getNumberOfVertices() const
{
return numberOfVertices;
} // end function getNumVertices
/**************************************************************************
* (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice *
* Hall. All Rights Reserved. *
* *
* DISCLAIMER: The authors and publisher of this book have used their *
* best efforts in preparing the book. These efforts include the *
* development, research, and testing of the theories and programs *
* to determine their effectiveness. The authors and publisher make *
* no warranty of any kind, expressed or implied, with regard to these *
* programs or to the documentation contained in these books. The authors *
* and publisher shall not be liable in any event for incidental or *
* consequential damages in connection with, or arising out of, the *
* furnishing, performance, or use of these programs. *
*************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -