(E) Swapping data contentsΒΆ

Goal: I will test different ways to pass parameters in C++ to demonstrate the material that I have previously read.

Instructions: Retrieve the code template: templates/03/swap/ -> student/03/swap/. For automated testing, the template contains the directive ifndef UNIT_TESTING (and the corresponding endif). You need not care about them, and you must not change them.

In the main program (function main) of the code template, you can see the call of the function swap. Your task is to write implementation for this function. Implement swap function in a way that allows calls in the given way, and do not change the call in main in any way. (If you just try to call the ready-made function swap of C++, your program will not pass the automatic tests.)

After implementing swap function, the program will work as follows:

Enter an integer: 42
Enter another integer: 0
The integers are 0 and 42

To be considered:

  • Even if you know immediately, based on the material you have previously read, how to solve the task, you have here an opportunity to test also other parameter passing ways of C++.
  • You can try what happens, if you pass literals as parameters to the function.
  • If C++ had dynamic typing, this function would be even more useful: If the types of the parameters did not matter, the same function could swap between integer and string variables.

A+ presents the exercise submission form here.