Exercises in this lecture   previous -- Keyboard shortcut: 'p'  next -- Keyboard shortcut: 'n'  Go to the notes, in which this exercise belongs -- Keyboard shortcut: 'u'   Alphabetic index   Course home      

Exercise 2.7
Use of class Point in C++


In this exercises we will explore some basic similarities and differences between use of classes in Java/C# and C++. (In the scope of this lecture we will NOT be concerned with class definition details. This belongs to the next lecture). In particular we will study creation/allocation of objects, and parameter passing of objects to a function/method.

The starting point is the simple Point class in C# and a sample C# client program that creates and operates on points. Make sure that you familiarize yourself with the correct output of this program. Predict it before you run it.

We provide a similar Point class in C++. The C++ definitions of the Point functions are also provided (but they are not important for this exercise). You are supposed to explore various uses of class Point in C++.

  1. Write a C++ client program that corresponds as close as possible to the C# client program. In this version of the C++ program you should access Point objects via pointers. First, write a version where you allocate the points in the free store.
  2. Adjust the C++ program from above such that the points are allocated on the stack, with pointers established to these (by use of the address operator).
  3. Write another version of the C++ client program uses C++ references instead of pointers. The goal is to come as close as possible to the pointer-based program you wrote above.
  4. Finally, write a C++ client program that use pure and simple value semantics of points. In other words, do not use references nor pointers at all in this version of the C++ program. We accept that the meaning of the program changes due to the introduction of value semantics. Predict the output of the program before you run it.

Feel free to do other experiments based on the Point classes that help you understand the similarities and differences between use of C# and C++ classes, pointers, references, and stack allocation of C++ objects.


Solution