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

For this lab we will create a container class using pointers. Our container is c

ID: 3688471 • Letter: F

Question

For this lab we will create a container class using pointers. Our container is called Line and the Line container holds the Point class. The Point class is very simple and already implemented. For this lab: Implement the Line member functions Use a pointer to an array of Points to hold your Point class You cannot use existing container classes. Some container classes are implemented as container adaptors and use an encapsulated object of a specific container class as its underlying container. This assignment will not implement a container adaptor. Implement a main() to test your Line class implementation. Include adding multiple Point objects to your line to force your Line to expand. The attached tar file contains the Point and Line class definitions as an Eclipse project


Below are the

- Lab10.cpp

- Line.cpp

- Point.cpp

- Line.h

- Point.h

files respectively.

//============================================================================
// Name : Lab10.cpp
// Author :
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
using namespace std;

int main() {
   cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!
   return 0;
}

//============================================================================

/*
* Line.cpp
*
* Created on: Nov 1, 2014
* Author: williamhooper
*/

#include "Line.h"

// Constructor
Line::Line()
{
}

// Deconstructor
Line::~Line()
{

}

//============================================================================

//============================================================================
// Name : Point.cpp
// Author :
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

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

using namespace std;

Point::Point(float x, float y) :
x(x), y(y)
{}

Point::Point(const Point & p)
{
x = p.x;
y = p.y;
}

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

//============================================================================

/*
* Line.h
*
* Created on: Nov 1, 2014
* Author: williamhooper
*/

#ifndef LINE_H_
#define LINE_H_

#include "Point.h"

class Line
{
public:
Line();
virtual ~Line();
void addBack(Point p); // add a Point to the end of our line
unsigned int size() const; // return the number of Points in our line
void clear(); // clear the Points from our line
float length() const; // Length of line
Point & operator[](int index); // [] operator override
private:
};

#endif /* LINE_H_ */

//============================================================================

// Point.h

#ifndef POINT_H_
#define POINT_H_


class Point {
public:
// default constructor
Point(const Point & t);
Point(float x = 0, float y = 0);
virtual ~Point() {};

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

Point& operator =( const Point& rhs );

private:
float x, y;
};


#endif /* POINT_H_ */

Explanation / Answer

main.cpp

#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;
}

sample output

2                                                                                                                                                           
32557                                                                                                                                                       
12                                                                                                                                                          
32903                                                                                                                                                       
3                                                                                                                                                           
32573                                                                                                                                                       

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