In this assignment you will learn the basics of compiling programs and the most fundamental aspects of the C++ language.
g++ compilerC++ 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)
float computeArea(Point &a, Point &b, Point &c)which takes by reference three Point objects, computes the area within the triangle cornered at them (hint: use Heron's Formula), and returns it as a float.
int main()which creates three points with interesting coordinates, computes the area using the aforementioned function, and displays the result. (It is not necessary to accept keyboard input from the user.)
> g++ -Wall lab1.cc Point.cc -o lab1
./lab1
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.