Showing posts with label chapter 8 drill. Show all posts
Showing posts with label chapter 8 drill. Show all posts

Wednesday, 4 April 2018

Chapter 8 // Drill 3 - 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 3



3. Write a program using a single file containing three namespaces X, Y, and Z so that the following main() works correctly:

int main()
{
X::var = 7;
X::print(); //print X's var

using namespace Y;
var = 9;
print(); //print Y's var

{
using Z::var;
using Z::print;
var = 11;
print(); //print Z's var
}

print(); //print Y's var
X::print(); //print X's var

}

Each namespace needs to define a variable called var and a function called print() that outputs the appropriate var using cout.


#include "std_lib_facilities.h"

namespace X {
 double var;

 void print()
 {
  cout << "X: " << var << endl;
 }
}

namespace Y {
 double var;

 void print()
 {
  cout << "Y: " << var << endl;
 }
}

namespace Z {
 double var;

 void print()
 {
  cout << "Z: " << var << endl;
 }
}

int main()
{
 X::var = 7;
 X::print();  //print X's var

 using namespace Y;
 var = 9;
 print();  //print Y's var

 {
  using Z::var;
  using Z::print;
  var = 11;
  print(); //print Z's var
 }

 print();  //print Y's var
 X::print();  //print X's var

 keep_window_open();

 return 0;
}

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();
}




Wednesday, 28 March 2018

Chapter 8 // Drill 1 - 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 1



1. Create three files: my.h, my.cpp, and use.cpp. The header file my.h contains:

extern int foo;
void print_foo();
void print(int);

The source code file, my.cpp #includes my.h and std_lib_facilities.h, defines print_foo() to print the value of foo using cout, and print(int i) to print the value of i using cout.

The source code file use.cpp #includes my.h, defines main() to set the value of foo to 7 and print it using print_foo(), and to print the value of 99 using print(). Note that use.cpp does not #include std_lib_facilities.h as it doesn't directly use any those facilities.

Get these files compiled and run. On Windows, you need to have both use.cpp and my.cpp in a project and use { char cc; cin >> cc} in use.cpp to be able to see your output. Hint: You need to #include <iostream> to use cin.


//my.h
#pragma once
//my.h

//declarations
extern int foo;
void print_foo();
void print(int);

void keepWindowOpen();

//my.cpp

//my.cpp

#include "my.h"
#include "std_lib_facilities.h"

int foo;

//print the value of foo
void print_foo()
{
 cout << foo << endl;
}

//print a given integer
void print(int i)
{
 cout << i << endl;
}

//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()
{
 //assign 7 to foo
 foo = 7;

 //print what's in foo
 print_foo();

 //send an integer to be printed to screen
 print(99);

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


A pretty simple exercise which showcases how to split a project up into separate files. Seeing as how you can't use keep_window_open() from std_lib_facilities I created a function in my.cpp which does the same thing and then declared it in my.h so it could be used in main(). This way I didn't have to #include <iostream> in use.cpp as it was already declared in my.cpp from the other header file.