Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

For this c++ program I need to create a container class using pointers and free

ID: 3720921 • Letter: F

Question

For this c++ program I need to create a container class using pointers and free store. The container is called Line and the Line container holds the Point class. The Point class is very simple and already implemented. For this program I need to complete the following tasks.:

-Implement the new Line member functions
-Use a pointer to an array of Points to hold the Point class and use free store to hold the Point objects
-I need to allow the user to add as many Point objects as they wish. This will require code to expand the free store.
-I cannot use existing container classes. Some container classes are implemented as container adapters and use an encapsulated object of a specific container class as its underlying container. This program will not implement a container adapter.
-Implement a main() to test the Line class implementation. Include adding multiple Point objects to the line to force the Line to expand.
-Include out-of-bounds checking and throw the appropriate exception when the condition occurs

Do Not change the Line.h file!!!! The Point.h file, however, can be changed if necessary (some code is unnecessary for this program)

Here is the Line.h file that I have so far:


/*
* Line.h
*
*/
#ifndef LINE_H_
#define LINE_H_
#include "Point.h"
class Line {
public:
/**
* Constructor and destructor
*/
Line();
virtual ~Line();
/**
* Add a point to the end of our line. If the line contains
* ten points then throw an out_of_range exception.
*/
void push_back(const Point& p);
/**
* Clear the list of points
*/
void clear();
/**
* Return the length of the line. The length is calculated as
* the sum of the distance between all points in the line.
*/
double length();
/**
* return the number of Points in our line
*/
unsigned int size() const;
/**
* [] operator override
*/
Point & operator[](int index);
private:
unsigned int index;
Point *points;
};
#endif /* LINE_H_ */

and here is the point.h file:


ifndef POINT_H_
#define POINT_H_
#include <iostream>
class Point {
public:
/**
* Constructor and destructor
*/
Point(const double x, const double y);
Point():x(0), y(0) {};
virtual ~Point();
/**
* Get the x value
*/
double getX() const;
/**
* Get the y value
*/
double getY() const;
/**
* Return the distance between Points
*/
double distance(const Point &p) const;
/**
* Output the Point as (x, y) to an output stream
*/
friend std::ostream& operator <<(std::ostream&, const Point&);
/**
* Declare comparison relationships
*/
friend bool operator ==(const Point &lhs, const Point &rhs);
friend bool operator <(const Point &lhs, const Point &rhs);
/**
* Declare math operators
*/
friend Point operator +(const Point &lhs, const Point &rhs);
friend Point operator -(const Point &lhs, const Point &rhs);
/**
* Copy constructor
*/
Point(const Point & t);
/**
* Copy assignment
*/
Point& operator =( const Point& rhs );
private:
double x;
double y;
};
#endif /* POINT_H_ */

Explanation / Answer

#include <iostream>
#include <sstream>
#include "Line.h"
#include "Point.h"

using namespace std;

int main()
{
   // Testing
   Line l;
   Point p(1, 3);
   Point p1(4, 5);
   Point p2(19, 3);
   Point p3(17, 3);
   Point p4(1, 81);
   Point p5(15, 15);
   Point p6(11, 11);
   Point p7(1, 22);
   Point p8(14, 85);
   Point p9(56, 46);
   Point p10(24, 32);
   Point p11(18, 39);
   Point p12(16, 4);
   Point p13(12,13);
   Point p14(6,4);

   l.addBack(p);   // Testing size and length
   l.addBack(p1);
   cout << l.size() << endl;   // 2
   cout << l.length() << endl;   // 3

   l.addBack(p2);   // Testing "if" condition, extending original array
   l.addBack(p3);
   l.addBack(p4);
   l.addBack(p5);
   l.addBack(p6);
   l.addBack(p7);
   l.addBack(p8);
   l.addBack(p9);
   l.addBack(p10);
   l.addBack(p11);

   cout << l.size() << endl;   // 12
   cout << l.length() << endl;   // 349

   l.clear();   // Testing clear method
   l.addBack(p12);
   l.addBack(p13);
   l.addBack(p14);
   cout << l.size() << endl;   // 3
   cout << l.length() << endl;   // Testing length on odd number of points
                               // 19

   l[11];   // Testing out of bounds exception

   return 0;
}

Point.h


#ifndef POINT_H_
#define POINT_H_


class Point {
public:
    Point(int x = 0, int y = 0);
    Point(const Point & t);
    virtual ~Point() {};

    int getX() const {return x;};   // get X value
    int getY() const {return y;};   // get Y value

    Point& operator =( const Point& rhs );

private:
    int x, y;
};


#endif /* POINT_H_ */


Point.cpp

#include <iostream>
#include "Point.h"
#include "Line.h"

using namespace std;

/**
* Point constructor requires a X and Y coordinate
*
* @param x
* @param y
*/
Point::Point(int x, int y) : x(x), y(y)
{

}

/**
* Point copy constructor
*
* @param t
*/
Point::Point(const Point & p)
{
   x = p.x;
   y = p.y;
}

/**
* Point copy assignment operator
*
* @param rhs
* @return
*/
Point& Point::operator =(const Point& rhs)
{
   if (this != &rhs)
   {
       this->x = rhs.x;
       this->y = rhs.y;
   }
   return *this;
}

Line.h

#ifndef LINE_H_
#define LINE_H_

#include <stdexcept>
#include <string>
#include "Point.h"

/**
* The line class contains a list of points. The line class is a container class.
*/
class Line {
public:
   Line();
   virtual ~Line();

   /**
   * add a Point to the end of our line
   */
   void addBack(Point p);

   /**
   * return the number of Points in our line
   */
   unsigned int size() const;

   /**
   * clear the Points from our line
   */
   void clear();

   /**
   * length of our line
   */
   int length() const;

   /**
   * [] operator override
   */
   Point& operator[](int index);

   void error(std::string s);
private:
   Point* pointArray;
   int arraySize;
   int elementSize;
};

#endif /* LINE_H_ */

Line.cpp

#include "Line.h"
#include "Point.h"
#include <cmath>
#include <string>
#include <stdexcept>
#include <iostream>

/**
* Default Line constructor
*/
Line::Line()   // Default constructor, sets initial array size to 10 and assigns pointer to it
{
   arraySize = 10;
   elementSize = 0;
   pointArray = new Point[arraySize];
}

void Line::addBack(Point a)   // Adds a point to the back of the array
{
   if(elementSize == arraySize)   // If array is too small copy all data to a new array, reassign pointer
   {
       Point* tempArray = new Point[arraySize + 10];   // New temp array in free store
       for(int i = 0; i < arraySize; i++)
       {
           tempArray[i] = pointArray[i];
       }
       delete[] pointArray;
       pointArray = tempArray;
       pointArray[elementSize++] = a;
       arraySize = (10 + arraySize);
   }
   else
   {
       pointArray[elementSize++] = a;
   }
}

unsigned int Line::size() const   // Returns how many elements in the array (not total size)
{
   return elementSize;
}

int Line::length() const   // Performs distance calculations on each set of points, and sums them
                           // (i/e index 2 and 1, then 3 and 2, then 4 and 3...)
{
   int d;
   for(int i = 0; i < (elementSize - 1); i++)
   {
       int xf, xi, yf, yi;
       xf = pointArray[i + 1].getX();
       yf = pointArray[i + 1].getY();
       xi = pointArray[i].getX();
       yi = pointArray[i].getY();
       d += sqrt(((xf - xi)*(xf - xi)) + ((yf - yi)*(yf - yi)));
   }
   return d;
}

void Line::clear()   // Deletes the pointer, reassigns it to a new array
{
   delete[] pointArray;
   arraySize = 10;
   elementSize = 0;
   pointArray = new Point[arraySize];
}

Point& Line::operator [](int index)   // Overloaded [], returns the point at user selected index, throws if oor
{
   if(arraySize < index)
   {
       error("Out of Bounds!!");
   }

   return pointArray[index];
}

void Line::error(std::string s)   // Error method
{
   std::cerr << s;
   throw std::out_of_range(s);
}
/**
* Line deconstructor
*/
Line::~Line()
{
   delete[] pointArray;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote