# Problem 1 — Geometric Points

We will follow the rule of separating the declaration and implementation of functions. In particular, we will extend the definition of the class `Point2D` in the file `point.h`, but we will write the implementations of all functions in the file `point.cpp`.

In this problem, we will write a class to model geometric points in two dimensions.

Download the file `code.zip` and add its content (the files `point.cpp`, `point.h`, `test.cpp`, `test.h`, `asserts.h`) to a new Code::Blocks project.

## a — Getters and Setters

It is advisable to declare the members `x` and `y` as private in order to keep a user of our class from changing the data without using our member functions (which we control).

We begin with the data representation. We also add means of reading and changing the data.

Add two data members `x` and `y` to the class `Point2D`.

Then, add a member function `get_x`. This function takes no parameters and returns a real number (of type `double` in C++) equal to the value of `x`. Add another member function `set_x` that takes as an argument a `double` and that updates `x` to this value.

Add function `get_y` and `set_y` that do the same for `y`.

You can test your functions by executing the program whose `main` function is defined in the file `test.cpp`.

## b — Sum of Coordinates

To work with the data of our class, we will compute the sum of the two coordinates of our geometric point.

Add a member function `sum` to the class `Point2D` that returns the sum of the coordinates `x` and `y`.

You can test your function by executing the program whose `main` function is defined in the file `test.cpp`.

## c — The Function `multiply`

The function we write next updates both coordinates at the same time.

Add a function `multiply` to your class `Point2D`. It takes as an argument a `double` and updates the coordinates `x` and `y` of the current object by multiplying the previous values by the argument.

You can test your function by executing the program whose `main` function is defined in the file `test.cpp`.

## d — Constructors

We will now add two constructors to our class to initialize the coordinates directly during the declaration of a variable, without having to go through the setters.

Add a constructor without arguments to `Point2D` that initializes the two coordinates `x` and `y` to zero.

Add a second constructor that takes as arguments two `double`s to initialize `x` and `y`.

You can test your constructors by executing the program whose `main` function is defined in the file `test.cpp`.

back to the course web site