point_t
object, read a file, and add points
to a list (std::vector
).
data_t
C++ code example discussed at length
in class, write C++ code to define a point_t
class
(object) that will hold
x-, y-, and z-coordinates of
a 3D point.
point.h
that holds the object interface
and point.cpp
that holds the object implemantation.
point_t
object should provide the following:
point_t(double x=0.0, double y=0.0, double z=0.0);
point_t(std::vector<double> c);
point_t(const point_t& rhs);
)
~point_t();
), default ok (can comment this out)
point_t operator=(const point_t& rhs);
)
friend istream& operator>>(istream& s, point_t& rhs);
friend ostream& operator<<(ostream& s, const point_t& rhs);
[ ]
subscript operator.
double distance(point_t& rhs);
)
that computes and returns the Euclidean distance between
this
point and the rhs
point:
\(
\sqrt{(x_1-x_2)^2 + (y_1-y_2)^2 + (z_1-z_2)^2}
\)
point_t
class should store the
\((x,y,z)\)
coordinates in a private
std::vector<double>
container.
main()
routine works unaltered:
int main(int argc, char *argv[])
{
point_t point;
std::vector<point_t> points;
std::vector<point_t>::iterator pit;
// read input file consisting of names and id codes adding entities to list
while(!std::cin.eof()) {
std::cin >> point;
if(std::cin.good())
points.push_back(point);
}
// now play it back
std::cout << "*** printing list" << std::endl;
for(int i=0; i<(int)points.size(); i++) {
std::cout << points[i] << std::endl;
}
// try it again...
std::cout << "*** printing list again" << std::endl;
for(int i=0; i<(int)points.size(); i++) {
std::cout << points[i] << std::endl;
}
std::cout << "*** printing point coords" << std::endl;
// get at x- and y-coords of p_i
for(int i=0; i<(int)points.size(); i++) {
std::cout << "p_i[0] = " << points[i][0] << " ";
std::cout << "p_i[1] = " << points[i][1] << std::endl;
std::cout << "p_i[2] = " << points[i][2] << std::endl;
}
std::cout << "*** checking distance function" << std::endl;
// check distance between two points
std::cout << "|p_0 - p_1| = " << points[0].distance(points[1]) << std::endl;
// clear list via STL clear() function (memory leak if point_t used new!!)
std::cout << "*** deleting list" << std::endl;
points.clear();
// nope ... see if we can delete an empty list
std::cout << "*** deleting list again" << std::endl;
points.clear();
// prove we survived
std::cout << "*** done" << std::endl;
}
pointdata.txt
file used to test the above program is:
15 14 13
600 320 140
104 29 23
19 78 83
450 321 33
*** printing list
15.00, 14.00, 13.00
600.00, 320.00, 140.00
104.00, 29.00, 23.00
19.00, 78.00, 83.00
450.00, 321.00, 33.00
*** printing list again
15.00, 14.00, 13.00
600.00, 320.00, 140.00
104.00, 29.00, 23.00
19.00, 78.00, 83.00
450.00, 321.00, 33.00
*** printing point coords
p_i[0] = 15.00 p_i[1] = 14.00 p_i[2] = 13.00
p_i[0] = 600.00 p_i[1] = 320.00 p_i[2] = 140.00
p_i[0] = 104.00 p_i[1] = 29.00 p_i[2] = 23.00
p_i[0] = 19.00 p_i[1] = 78.00 p_i[2] = 83.00
p_i[0] = 450.00 p_i[1] = 321.00 p_i[2] = 33.00
*** checking distance function
|p_0 - p_1| = 672.30
*** deleting list
*** deleting list again
*** done
Makefile
that you can use to compile the project:
.SUFFIXES: .c .o .cpp .cc .cxx .C
UNAME = $(shell uname)
PLATFORM = $(shell uname -p)
CC = g++
COPTS = -g -Wall
INCDIR =
LIBDIR =
ifeq ("$(shell uname)", "Linux")
LIBS = -lm
else
ifeq ("$(shell uname)", "Darwin")
LIBS = -framework Foundation -lstdc++
endif
endif
.c.o:
$(CC) -c $(INCDIR) $(COPTS) -o $@ $<
.cpp.o:
$(CC) -c $(INCDIR) $(COPTS) -o $@ $<
all : main
OBJECTS = \
point.o
main : $(OBJECTS) main.o
$(CC) -o $@ $(INCDIR) $(COPTS) $(OBJECTS) $@.o $(LIBDIR) $(LIBS)
point.o: point.h point.cpp
clean :
rm -f *.o
rm -rf main main.app
tar.gz
archive of your asg##/ directory, including:
README
file containing
Makefile
.h
headers and .cpp
source)
make clean
before tar
)
handin
notes