#ifndef PATH_H #define PATH_H /////////////////////////////////////////////////////////////// // path.h -- represents a path of points // // ver 1.1 // // // // Language: Visual C++, ver 6.0 // // Platform: Dell OptiPlex Gs, Win NT 4.0, sp 3 // // Application: Example for ECS500 // // Author: Jim Fawcett, CST 2-187, Syracuse Univ. // // (315) 443-3948, fawcett@ecs.syr.edu // /////////////////////////////////////////////////////////////// /* Class Operations: ================= This class represents a piece-wise linear path in a plane. Actually the connections are imagined - the class simply contains a collection of points. Public Interface: ================= path P(15); construct path with space to hold 15 points path P; construct path with space to hold 10 points having label "" path P2 = P1; construct P2 from a copy of P1's state P2.add(p1).add(p2); add points p1 and p2 to path P2 will allocate more space if needed P2.erase(); remove all points from path P2 P2.resize(25); reallocate point storage to hold 25 points - retains original point values P1.size(); number of points on path P1.max(); current allocation of point array P1.resize(20); resize current allocation to 20 points P1 = P2; assign P2's state to P1 P1 = P2 + P3; concatenate P3's points on P2's points and assign to P1. P2 is unchanged. P1 += P2; concatenate P2's points onto P1. P1's state holds the result P[2] = p1; assign point p1 to the 3rd element of P. only valid if P.size() is at least 2 p1 = P[0]; assign 1st point of path to p1 P.put(cout); send P's label and points to cout P.get(cin); get P's label and points from cin cout << P; send P to standard output in >> P; read P from stream in */ // /////////////////////////////////////////////////////////////// // Build Process // /////////////////////////////////////////////////////////////// // Required Files: // // path.h, lpoint.h, point.h, point.cpp // // // // Build Command: // // cl /GX /DTEST_LPATH path.cpp point.cpp // /////////////////////////////////////////////////////////////// /* Maintenance History: ==================== ver 1.0 : 22 Jun 99 - first release ver 1.1 : 01 Jul 99 - converted to template class - added indexing - remove using namespace std from this header file - completed manual and maintenance pages Notes: ====== - path and lpath classes are templatized so that when lpath derives from path it will inherit the type of array it needes to store its points. This has an added advantage that path and lpath classes can hold either points or lpoints. - path must define both a copy constructor and assignment operator since its data members include a pointer to allocated memory. Note that lpath need not define either since it has only a standard C++ string data member. - path defines insertion and extraction operators using virtual func- tions put and get since lpath will need to redefine those to display labels as well as points. */ // #include template class path { public: path(int n=10); // sizing constructor path(const path &p); // copy constructor virtual ~path(); // destructor path& add(const T &p); // adds points to path path& operator= (const path &p); // assignment path& operator+=(const path &p); // concatenation to self path operator+ (const path &p); // concatenation T& operator[](int n); // indexes non-const objects T operator[](int n) const; // indexes const objects int size() const; // returns number of points int max() const; // returns array size void erase(); // empties the path void resize(int n); // resize internal array virtual void put(std::ostream &out) const; // insert path on out virtual void get(std::istream &in); // extract path from in protected: int _size; // number of points in path int _max; // available space for points T *array; // points to available space }; //----< return physical size, e.g. size of array >------------- template inline int path::max() const { return _max; } //----< returns the logical size, e.g. number of points >------ template inline int path::size() const { return _size; } //----< empty path >------------------------------------------- template inline void path::erase() { _size = 0; } // //----< sizing constructor >----------------------------------- // // Note the initialization sequence template path::path(int n) : _max(n), _size(0) { array = new T[n]; if(array == NULL) throw "\n memory allocation failure\n"; } //----< copy constructor >------------------------------------- template path::path(const path &p) : _max(p._max), _size(p._size) { array = new T[_max]; if(array == NULL) throw "\n memory allocation failure\n\n"; int i; for(i=0; i<_size; i++) array[i] = p.array[i]; } //----< destructor >------------------------------------------- template path::~path() { delete [] array; // delete with [] if you new with [] _size = _max = 0; } //----< assignment >------------------------------------------- template path& path::operator =(const path &p) { if(this == &p) return *this; delete [] array; array = new T[_max = p._max]; if(array == NULL) throw "\n memory allocation failure\n"; int i; _size = p._size; for(i=0; i<_size; i++) array[i] = p.array[i]; return *this; } // //----< add points to path >----------------------------------- template path& path::add(const T &p) { if(_size >= _max) resize(2*_max); array[_size++] = p; return *this; } //----< resize internal array >-------------------------------- // // resizes array either larger or smaller template void path::resize(int n) { if(n < 1) { cout << "\n can't give array a non-positive size\n\n"; return; } if(n != _max) { T *temp = new T[_max=n]; if(temp == NULL) throw "\n memory allocation failure\n\n"; int i; for(i=0; i<_max && i<_size; i++) temp[i] = array[i]; delete [] array; array = temp; } } //----< concatenation to self >-------------------------------- template path& path::operator +=(const path &p) { int i; for(i=0; i---------------------------------------- template path path::operator +(const path &p) { path temp = *this; temp += p; return temp; } //----< index non-const objects >------------------------------ template T& path::operator [](int n) { if(n<0 || _size <=n) throw "\n domain error in operator[]\n\n"; return array[n]; } //----< index const objects >---------------------------------- template T path::operator [](int n) const { if(n<0 || _size <=n) throw "\n domain error in operator[]\n\n"; return array[n]; } //----< display points on path >------------------------------- template void path::put(std::ostream &out) const { out << " " << _size << '\n'; int i; for(i=0; i<_size; i++) out << " " << array[i] << '\n'; out << endl; } //----< insertion >-------------------------------------------- template std::ostream& operator<<(std::ostream &out, const path &p) { p.put(out); return out; } // //----< get path from stream >--------------------------------- template void path::get(std::istream &in) { T temp; _size = 0; int number; in >> number; int i; for(i=0; i> temp; add(temp); } } //----< extraction >------------------------------------------- template std::istream& operator>>(std::istream &in, path &p) { p.get(in); return in; } #endif