DM560, Introduction to Programming in C++

Sheet 5

Task 1

Find the object that has global scope in the calculator example and modify the program in such a way that it does not have anymore objects with global scope.

Task 2

This is the Drill 2 from page 298 of the textbook.

Write three functions swap_v(int,int), swap_r(int&,int&), and swap_cr(const int&, const int&). Each should have the body { int temp; temp = a, a=b; b=temp; } where a and b are the names of the arguments. Try calling each swap like this

int x = 7;
int y =9;
swap_?(x,y);  // replace ? by v, r, or cr
swap_?(7,9);
const int cx = 7;
const int cy = 9;
swap_?(cx,cy); 
swap_?(7.7,9.9);
double dx = 7.7;
double dy = 9.9;
swap_?(dx,dy);
swap_?(7.7,9.9);

Which functions and calls compiled, and why? After each swap that compiled, print the value of the arguments after the call to see if they were actually swapped.