C++ track: assignment 1


Goals

In this assignment you will learn the basics of compiling programs and the most fundamental aspects of the C++ language.

Language concepts covered this week

Working with classes and 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:

class Point 
{
private:
    int x_coord, y_coord;

public:
    // Constructors:
    Point();                // no-argument version
    Point(int x, int y);    // two-argument version

    // "Mutator" methods:
    void setX( int val );
    void setY( int val );
    
    // "Accessor" methods:
    int getX();
    int getY();

    // Destructor:
    ~Point();
}; 

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;              // this calls the no-argument constructor
                            //   Point::Point()
Point myOtherPoint(5, 3);   // this calls the two-argument constructor 
                            //   Point::Point(int, int)


Program to write:

  1. Copy the source code for the Point class (Point.hh and Point.cc) to a convenient working directory.
  2. Add a new method to Point called distanceTo. This method should accept as an argument a Point & and return a double that approximates the distance between the two points.
  3. Create a new source code file lab1.cc and implement two functions:

  4. Compile these sources together like so:
    > g++ -Wall lab1.cc Point.cc -o lab1
  5. Run the generated program:
     ./lab1
  6. 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!

To hand in

Your updated Point.hh and Point.cc files, as well as the file lab1.cc. Before you hand them in, make sure that you run the style checking program on all of them to catch obvious style mistakes. If you don't, I will, and I'll make you re-write the programs if they fail. See the style guide for more information on this.