#ifndef LPATH_H #define LPATH_H /////////////////////////////////////////////////////////////// // lpath.h -- represents a labeled path of points // // ver 1.2 // // // // 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. The path can be labeled. Public Interface: ================= lpath P(15,"path P"); construct path with space to hold 15 points having label "path P" lpath P; construct path with space to hold 10 points having label "" lpath 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 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.label() = "Path P"; sets P's label to the string "Path P" string L = P.label(); retrieves P's label 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: // // lpath.h, lpath.cpp, path.h, lpoint.h, lpoint.cpp, // // point.h, point.cpp // // // // Build Command: // // cl /GX /DTEST_LPATH lpath.cpp path.cpp lpoint.cpp // // point.cpp // /////////////////////////////////////////////////////////////// /* Maintenance History: ==================== ver 1.0 : 22 Jun 99 - first release ver 1.1 : 01 Jul 99 - converted to template class - modified lpath constructors to use initialization via base class constructors - added indexing - completed manual and maintenance pages ver 1.2 : 06 Jul 99 - modified notes section below - removed using namespace std from this header file 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 the added advantage that path and lpath classes can hold either points or lpoints. - lpath does not define an assignment operator since its only data member is a standard C++ string which has defined assignment sem- antics. Therefore the member-wise assignment of compiler generated assignment works correctly. It must, however, define a copy con- structor since it is MANDATORY that it calls base copy constructor in an initialization sequence. Note that path must define both copy constructor and assignment operator since it has a pointer to allocated heap memory as a data member. - lpath and lpoint doe not define insertion or extraction operators since those defined by the base classes will work correctly with lpath and lpoint due to the fact that they are based on virtual functions put and get. */ // #include #include #include "path.h" template class lpath : public path { public: lpath(int max=10, const std::string& label=""); lpath(const lpath &lp); virtual ~lpath(); // lpath& operator=(const lpath& lp); // compiler will generate lpath operator+(const lpath& lp); std::string& label(); virtual void put(std::ostream &out) const; virtual void get(std::istream &in); protected: std::string _label; }; //----< access label >----------------------------------------- template inline std::string& lpath::label() { return _label; } //----< sizing constructor >----------------------------------- template lpath::lpath(int max, const std::string& label) : path(max), _label(label) { } //----< copy constructor >------------------------------------- template lpath::lpath(const lpath &lp) : path(lp), _label(lp._label) { } //----< destructor >------------------------------------------- template lpath::~lpath() { /* nothing to do */ } //----< assignment - not needed, but here's how to do it >----- /* template lpath& lpath::operator=(const lpath &lp) { if(this == &lp) return *this; path::operator=(lp); // call base assignment _label = lp._label; return *this; } */ // //----< concatenation of paths >------------------------------- template lpath lpath::operator+(const lpath &lp) { lpath temp = *this; temp += lp; return temp; } //----< display path of points >------------------------------- // // show path with format _label // (_x1,_y1,_label1) // : template void lpath::put(std::ostream &out) const { out << "\n " << _label << "\n " << _size; int i; for(i=0; i<_size; i++) out << "\n " << array[i]; out << endl; } //----< get path of points >----------------------------------- // // collect path with format _label // (_x1,_y1,_label1) // : template void lpath::get(std::istream &in) { _label.erase(); char ch; in >> ch; // read 1st non whitespace char while(ch != '\n') { _label += ch; // collect label chars in.get(ch); } T temp; _size = 0; // get points int number; in >> number; int i; for(i=0; i> temp; add(temp); } } #endif