Sunday, 1 April 2018

Chapter 8 // Drill 2 - Principles & Practice Using C++

In this exercise I am using Visual Studio Community 2017 and the header file "std_lib_facilities.h" which can be found here:

http://www.stroustrup.com/Programming/PPP2code/std_lib_facilities.h


Chapter 8 // Drill 2



2. 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 functions like this:
int x = 7;
int y = 9;
swap_?(x, y);
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 it they were actually swapped. If you are surprised by the result, consult section 8.6.

First of all the program won't compile because you cannot change a const value so the third function needs to have a different body. This can be done by creating a second temp variable, assigning b to it and then sending temp and temp2 to one of the previous functions to be swapped.

Also, when sending numbers to the functions they will not be printed as functions can only return 1 value so the print was added to end of each calling function instead apart from the const function as that calls one that already has a print command.

Also on the functions that send numbers you cannot use pass-by-reference because that requires an object to be passed not a value. Also the double values will be truncated as all three functions use ints.

For this exercise I modified the previous drill to accomadate this one, so the program is split into three seperate files; my.h, my.cpp and use.cpp which contains main();

//my.h
#pragma once
//my.h
#include "std_lib_facilities.h"

//declarations
void swap_v(int, int);
void swap_r(int&, int&);
void swap_cr(const int&, const int&);

void keepWindowOpen();


//my.cpp
//my.cpp

#include "my.h"


//swap two integers using pass-by-value
void swap_v(int a, int b)
{
 int temp;
 temp = a;
 a = b;
 b = temp;

 cout << a << '\t' << b << endl;

 return;
}

//swap two integers using pass-by-reference
void swap_r(int& a, int& b)
{
 int temp;
 temp = a;
 a = b;
 b = temp;

 cout << a << '\t' << b << endl;

 return;
}

//swap two integers using pass-by-const-reference
void swap_cr(const int& a, const int& b)
{
 int temp;
 int temp2;

 temp = a;
 temp2 = b;

 swap_r(temp, temp2);

 return;
}

//function to keep the window open
void keepWindowOpen()
{
 char c;
 cout << "\nPress any key to quit: ";
 cin >> c;
}


//use.cpp
/use.cpp

#include "my.h"

int main()
{
 //variables to swap
 int x = 7;
 int y = 9;

 swap_r(x, y);
 swap_cr(7, 9);

 //const variables to swap
 const int cx = 7;
 const int cy = 9;

 swap_v(cx, cy);
 swap_cr(7.7, 9.9);

 //double variables to swap
 double dx = 7.7;
 double dy = 9.9;

 swap_v(dx, dy);
 swap_cr(7.7, 9.9);

 //keep window open until any key press
 keepWindowOpen();
}




No comments:

Post a Comment