<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">\begin{fileverb}
/*----------------------------------------------------------------------*/
/*  FILE:  shapes_v4.cpp (Eighth C++ Lab Exercise 4)                    */
/*  WGET:  wget http://oceanai.mit.edu/cpplabs/shapes_v4.cpp            */
/*  RUN:   shapes_v4                                                    */
/*----------------------------------------------------------------------*/

#include &lt;iostream&gt;      // For use of the cout function
#include "TriangleV4.h"
#include "SquareV4.h"

using namespace std;

int main()
{
  TriangleV4 triangle;
  Vertex t_vertex1(0,0);
  Vertex t_vertex2(10,0);
  Vertex t_vertex3(5,5);
  triangle.addVertex(t_vertex1);
  triangle.addVertex(t_vertex2);
  triangle.addVertex(t_vertex3);

  SquareV4 square;
  Vertex s_vertex1(0,0);
  Vertex s_vertex2(10,0);
  Vertex s_vertex3(10,10);
  Vertex s_vertex4(0,10);
  square.addVertex(s_vertex1);
  square.addVertex(s_vertex2);
  square.addVertex(s_vertex3);
  square.addVertex(s_vertex4);

  SegListV4 *shape1 = &amp;triangle;
  SegListV4 *shape2 = &amp;square;

  cout &lt;&lt; "Triangle: " &lt;&lt; shape1-&gt;getSpec() &lt;&lt; endl;
  cout &lt;&lt; "    Area: " &lt;&lt; shape1-&gt;getArea() &lt;&lt; endl;
  cout &lt;&lt; "Square:   " &lt;&lt; shape2-&gt;getSpec() &lt;&lt; endl;
  cout &lt;&lt; "    Area: " &lt;&lt; shape2-&gt;getArea() &lt;&lt; endl;

  return(0);
}
\end{fileverb}

\begin{fileverb}
/*----------------------------------------------------------------------*/
/*  FILE:  SegListV4.h                                                  */
/*  WGET:  wget http://oceanai.mit.edu/cpplabs/SegListV4.h              */
/*----------------------------------------------------------------------*/

#ifndef SEGLIST_V4_HEADER
#define SEGLIST_V4_HEADER

#include &lt;string&gt;
#include &lt;vector&gt;
#include "Vertex.h"

class SegListV4 {
 public:
  SegListV4() {};
  ~SegListV4() {};

  void addVertex(Vertex vertex) {m_vertices.push_back(vertex);};

  std::string getSpec() const;

  virtual double getArea() const {return(0);}; 

  double getMinX() const;
  double getMaxX() const;
  double getMinY() const;
  double getMaxY() const;

 protected:
  std::vector&lt;Vertex&gt; m_vertices;
};
#endif
\end{fileverb}

\begin{fileverb}
/*----------------------------------------------------------------------*/
/*  FILE:  SegListV4.cpp                                                */
/*  WGET:  wget http://oceanai.mit.edu/cpplabs/SegListV4.cpp            */
/*----------------------------------------------------------------------*/

#include "SegListV4.h"

using namespace std;

//-----------------------------------------------------------------------
// Procedure: getSpec

string SegListV4::getSpec() const
{
  string spec;
  for(unsigned int i=0; i&lt;m_vertices.size(); i++) {
    if(i != 0)
      spec += ",";
    spec += m_vertices[i].getSpec();
  }
  return(spec);
}


//-----------------------------------------------------------------------
// Procedure: getMinX()

double SegListV4::getMinX() const
{
  if(m_vertices.size() == 0)
    return(0);
  double min_x = m_vertices[0].getX();
  for(unsigned int i=1; i&lt;m_vertices.size(); i++) {
    if(m_vertices[i].getX() &lt; min_x)
      min_x = m_vertices[i].getX();
  }
  return(min_x);
}

//-----------------------------------------------------------------------
// Procedure: getMaxX()

double SegListV4::getMaxX() const
{
  if(m_vertices.size() == 0)
    return(0);
  double max_x = m_vertices[0].getX();
  for(unsigned int i=1; i&lt;m_vertices.size(); i++) {
    if(m_vertices[i].getX() &gt; max_x)
      max_x = m_vertices[i].getX();
  }
  return(max_x);
}
\end{fileverb}

\begin{fileverb}
//-----------------------------------------------------------------------
// Procedure: getMinY()

double SegListV4::getMinY() const
{
  if(m_vertices.size() == 0)
    return(0);
  double min_y = m_vertices[0].getY();
  for(unsigned int i=1; i&lt;m_vertices.size(); i++) {
    if(m_vertices[i].getY() &lt; min_y)
      min_y = m_vertices[i].getY();
  }
  return(min_y);
}

//-----------------------------------------------------------------------
// Procedure: getMaxY()

double SegListV4::getMaxY() const
{
  if(m_vertices.size() == 0)
    return(0);
  double max_y = m_vertices[0].getY();
  for(unsigned int i=1; i&lt;m_vertices.size(); i++) {
    if(m_vertices[i].getY() &gt; max_y)
      max_y = m_vertices[i].getY();
  }
  return(max_y);
}
\end{fileverb}

\begin{fileverb}
/*----------------------------------------------------------------------*/
/*  FILE:  TriangleV4.h                                                 */
/*  WGET:  wget http://oceanai.mit.edu/cpplabs/TriangleV4.h             */
/*----------------------------------------------------------------------*/

#ifndef TRIANGLE_V4_HEADER
#define TRIANGLE_V4_HEADER

#include &lt;string&gt;
#include &lt;vector&gt;
#include "SegListV4.h"

class TriangleV4 : public SegListV4 {
 public:
  TriangleV4() {};
  ~TriangleV4() {};

  std::string getSpec() const;

  double getArea() const;
};

#endif
\end{fileverb}

\begin{fileverb}
/*----------------------------------------------------------------------*/
/*  FILE:  TriangleV4.cpp                                               */
/*  WGET:  wget http://oceanai.mit.edu/cpplabs/TriangleV4.cpp           */
/*----------------------------------------------------------------------*/

#include "TriangleV4.h"

using namespace std;

//-----------------------------------------------------------------------
// Procedure: getSpec()

string TriangleV4::getSpec() const
{
  string spec = "type=triangle,";
  spec += SegListV4::getSpec();
  return(spec);
}

//-----------------------------------------------------------------------
// Procedure: getArea()

double TriangleV4::getArea() const
{
  // First compute the area of the bounding box
  double xmin = getMinX();
  double xmax = getMaxX();
  double ymin = getMinY();
  double ymax = getMaxY();

  double square_area = (xmax-xmin) * (ymax-ymin);
  
  return(square_area / 2);
}
\end{fileverb}

\begin{fileverb}
/*----------------------------------------------------------------------*/
/*  FILE:  SquareV4.h                                                   */
/*  WGET:  wget http://oceanai.mit.edu/cpplabs/SquareV4.h               */
/*----------------------------------------------------------------------*/

#ifndef SQUARE_V4_HEADER
#define SQUARE_V4_HEADER

#include &lt;string&gt;
#include "SegListV4.h"

class SquareV4 : public SegListV4 {
 public:
  SquareV4() {};
  ~SquareV4() {};

  std::string getSpec() const;

  double getArea() const;
};
#endif
\end{fileverb}

\begin{fileverb}
/*----------------------------------------------------------------------*/
/*  FILE:  SquareV4.cpp                                                 */
/*  WGET:  wget http://oceanai.mit.edu/cpplabs/SquareV4.cpp             */
/*----------------------------------------------------------------------*/

#include "SquareV4.h"

using namespace std;

//-----------------------------------------------------------------------
// Procedure: getSpec()

string SquareV4::getSpec() const
{
  string spec = "type=square,";
  spec += SegListV4::getSpec();
  return(spec);
}

//-----------------------------------------------------------------------
// Procedure: getArea()

double SquareV4::getArea() const
{
  // First compute the area of the bounding box
  double xmin = getMinX();
  double xmax = getMaxX();
  double ymin = getMinY();
  double ymax = getMaxY();

  double area = (xmax-xmin) * (ymax-ymin);
  
  return(area);
}
\end{fileverb}

</pre></body></html>