Design Exercise: Using Class Libraries

Dr. Mark J. Sebern


CS-182 Main Page Schedule

Overview

This design exercise makes use of an existing class library to re-implement the "triangle" application from an earlier lab project, making use of a class library that is available for your use. Unlike the earlier lab, you are not required to write any functions of your own, though you will almost certainly want to make use of class member functions declared in the class library.

Class library interface

The class library available for use in this exercise defines two classes: Point and Line. The Point class represents a point in 2-dimensional Cartesian space, while Line represents a line segment determined by two points (origin point and end point). The interfaces for these classes is as follows:

// geom2d.h - Interface declaration for 2-dimensional geometry classes

class Point
// Class Point: describes a point in Cartesian space
{
public:
// Interface portion of class declaration

	double GetX() const;
	// Returns: the x-coordinate of the point

	double GetY() const;
	// Returns: the y-coordinate of the point

	void SetX(double x);
	// Arguments:
	//    x - the new x-coordinate of the point

	void SetY(double y);
	// Arguments:
	//    y - the new y-coordinate of the point

	void Read (istream& in_stream);
	// Reads new x- and y-coordinate values
	// Arguments:
	//    in_stream - the input stream to read from

	void Print (ostream& out_stream) const;
	// Prints the point "(xxx,yyy)" to an output stream
	// Arguments:
	//    out_stream - output stream to write to
};
class Line
// Class Line: describes a line segment in Cartesian space,
//             defined by two points (origin point and endpoint)
{
public:
// Interface portion of class declaration

	Point GetOrigin() const;
	// Returns: the origin point of the line

	Point GetEnd() const;
	// Returns: the end point of the line

	void SetOrigin(Point org);
	// Arguments:
	//   org - the new origin point of the line

	void SetEnd(Point end);
	// Arguments:
	//   end - the new end point of the line

	void SetPoints(Point org, Point end);
	// Arguments:
	//   org - the new origin point of the line
	//   end - the new end point of the line

	double Length() const;
	// Returns: the length of the line

	double CalcDistanceToPoint(Point pt) const;
	// Arguments:
	//    pt - reference point for distance calculation
	// Returns: the perpendicular distance from
	//          the line to the reference point
};

Sample solution

Once you have completed this exercise, you may wish to compare your solution with a sample implementation. The sample program is called triang2 and the class library is called geom2d; you can download one of the following ZIP files, depending on the compiler you are using:


This page was last updated on September 03, 1997; send comments to Mark Sebern.