C++ Lab 1: Jumping Right In

working with objects

C++ lets us program with objects. We describe objects in C++ by declaring and defining classes. We declare our class's structure in a header file, just like in C, and define it (that is, write the code that actually does the work) in a corresponding source code file.

Here is a sample header file Point.hh that describes an object that represents a point in two-dimensional Euclidean space:

 // A 2D point class!
 class Point {

 private:
     // Coordinates of the point.
     double x_coord;
     double y_coord;

 public:
     // Constructors
     Point();				// Default constructor
     Point(double x, double y);		// Two-argument constructor

     // Destructor
     ~Point();

     // Accessor methods
     double getX();
     double getY();

     // Mutator methods
     void setX(double val);
     void setY(double val);
 };	

We can instantiate, or create an instance of, our class anywhere in the rest of our code by calling any of the constructors we have defined:

 Point myPoint;            // Calls Point::Point()
 Point myOtherPoint(5, 3); // Calls two-argument constructor Point::Point(double, double)

Your Task

  1. Copy the source code for the Point class (Point.hh and Point.cc) to a convenient working directory.

  2. Change the Point class to represent points in three dimensions. Make sure to update the comments to match this change!

  3. Add a new member function to Point called distanceTo. This member function should accept as an argument a Point & (a reference to a Point), and it should return a double that approximates the distance between the two points.

    You will probably find a square-root function useful for this! The C standard library has one, called sqrt(). The function takes a double and returns another double.

    If you were programming in C, you would #include <math.h>, but in C++ you say #include <cmath>. (This means, "Include the C Math header.") And then you are all set.

  4. Create a new source file lab1.cc and implement two functions:

  5. Compile these sources together like so:

      g++ -Wall lab1.cc Point.cc -o lab1
    Remember:

  6. Run the generated program:

      ./lab1

  7. Go back and test computeArea with several boundary and/or extreme cases to make sure your implementation is robust for all triangles, including degenerate ones. Things you might try:

    In software-engineering parlance, these degenerate cases are called "edge cases" or "corner cases," because they test combinations of inputs that may be rare, but still possible. Usually, you want to start out by seeing if your program can handle simple cases that will expose obvious issues. Once your program can handle these, then you move on to the more subtle test scenarios.

That's it! Once you are finished, and have tested your code, place the files in your ~/cs11/cpp/lab1 directory so that I may retrieve them.


Updated April 10, 2008. Copyright (C) 2004-2008, California Institute of Technology.