Improving your Classes

Exercise 1: Extra Constructors

In this exercise we are going to add extra constructors. But first we do a little experiment.

In the Point class constructor and destructor, add some code that displays some text.

In the main program, make sure you use the Distance() function to calculate the distance

between two points. Run the program and count how many times the constructor and

destructor are called. Are they the same?

Now add a copy constructor to the Point class that also displays some text. Also add a

constructor that accepts x- and y-coordinates so you can create a point with the right values

without using the set functions. Use this constructor to create the point from the user input.

Run the program again and count the number of times the constructors and destructor are

called. Is the copy constructor called and is the number of constructor calls now the same as

the number of destructor calls?

We can derive two things from these results:

  1. When calling the Distance() function, the input point is copied (call by value).
  2. You will get the copy constructor ‘for free’ when you do not create one yourself.

Exercise 2: Pass by Reference

In the previous exercise, you saw that the point passed to the Distance() method was copied.

Since creating a copy is unnecessary in this case, change this function so that it passes the

input point “by reference” so that no copy is made. Pass it as “const reference” to make it

impossible to change the input point from within the Distance() function.

Run the program again. It should call the copy constructor fewer times than before.

Also test if you can change the input point in the Distance() function. This should result in a

compiler error.

Exercise 3: Function Overloading

Previously you saw that there could be more than one constructor as long as the input

arguments are different. You can do the same for normal member functions. Thus you can

rename the DistanceOrigin() function to Distance(). Also you can rename

the SetX() andGetX() functions to just X(). The same is true for the setter and getter of the y

coordinate.

Exercise 4: Const Functions

In the test program create a const point and try to set the x-coordinate:

const Point cp(1.5, 3.9);

cp.X(0.3);

Compile the program. Did you get a compiler error? It should give a compiler error because

you try to change a const object.Now replace the line that changes the x-coordinate to code that reads the x-coordinate:

cout<<cp.X()<<endl;

Compile the program again. You will see that is still gives a compiler error even while

retrieving the x-coordinate does not change the point object. This is because the compiler

does not know that the function does not change anything. So we need to mark the x

coordinate getter as const by making it a const function. Do this also for the y-coordinate

getter and the Distance() and ToString() functions because these don’t change the point

object as well.

Recompile the application. It should now work.

Exercise 5: Line Class

In the final exercise for this chapter we are going to create a Line class. The Line class has a

start- and an end-point. So the Line class should have two Point objects as data members.

This mechanism is called “composition”. See also Figure 3.

Give the Line class the following functionality:

  • Default constructor (set the points to 0, 0).
  • Constructor with a start- and end-point.
  • Copy constructor.
  • Destructor.
  • Overloaded getters for the start- and end-point.
  • Overloaded setters for the start- and end-point.
  • A ToString() function that returns a description of the line.
  • A Length() function that returns the length of the line. Note that you can use the

distance function on the embeddedPoint objects to calculate the length. This

mechanism is called “delegation”.

Use const arguments, const functions and pass objects by reference where applicable.

 

Exercise 5: Line Class

In the final exercise for this chapter we are going to create a Line class. The Line class has a

start- and an end-point. So the Line class should have two Point objects as data members.

This mechanism is called “composition”. See also Figure 3.

Give the Line class the following functionality:

  • Default constructor (set the points to 0, 0).
  • Constructor with a start- and end-point.
  • Copy constructor.
  • Destructor.
  • Overloaded getters for the start- and end-point.
  • Overloaded setters for the start- and end-point.
  • A ToString() function that returns a description of the line.
  • A Length() function that returns the length of the line. Note that you can use the

distance function on the embeddedPoint objects to calculate the length. This

mechanism is called “delegation”.

Use const arguments, const functions and pass objects by reference where applicable.

Leave a Reply

Your email address will not be published. Required fields are marked *