Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Apress
committed
Oct 18, 2016
0 parents
commit 09ee066
Showing
174 changed files
with
8,058 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
// Exercise 1.1 Writing the line "Hello World" to the screen. | ||
|
||
#include <iostream> | ||
|
||
int main() | ||
{ | ||
std::cout << "Hello World" << std::endl; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// Exercise 1.2 Write your name and age on successive lines. | ||
// There are several possibilities. You can do it in one statement for example. | ||
// You could also output '\n' to go to a new line. | ||
|
||
#include <iostream> | ||
|
||
int main() | ||
{ | ||
std::cout << "Phile McCavity" << std::endl; // Name | ||
std::cout << "Age: 88" << std::endl; // and age | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// Exercise 1.3 Spot the errors. The correct version should closely resemble the answer to exercise 1.1. | ||
|
||
include <iostream> // # charaxcter missing before include | ||
|
||
Int main() // Should be int, not Int | ||
{ | ||
std:cout << "Hello World" << std:endl // A semicolon is missing from the end of this line | ||
// cout and endl must be prefixed with std:: | ||
) |
13 changes: 13 additions & 0 deletions
13
9781484200087_Exercise_Solutions/Chapter 10/Soln10_01/main.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// Exercise 10.1 Using multiple files and preprocessor directives. File: main.cpp | ||
// We have seven files in all. | ||
// The file main.cpp only needs to #include two of our three new header files, | ||
// since all it does is call the functions printthis() and printthat(): | ||
|
||
#include "printthis.h" | ||
#include "printthat.h" | ||
|
||
int main() | ||
{ | ||
print_this("This is a test string using printthis()."); | ||
print_that("This is a test string using printthat()."); | ||
} |
13 changes: 13 additions & 0 deletions
13
9781484200087_Exercise_Solutions/Chapter 10/Soln10_01/print.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// Exercise 10.1 Using multiple files and preprocessor directives. File: print.cpp | ||
// We have seven files in all. | ||
// The file print.cpp contains the definition for the function print(). | ||
|
||
#include "print.h" | ||
#include <string> | ||
#include <iostream> | ||
using std::string; | ||
|
||
void print(const string& s) | ||
{ | ||
std::cout << s << std::endl; | ||
} |
14 changes: 14 additions & 0 deletions
14
9781484200087_Exercise_Solutions/Chapter 10/Soln10_01/print.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// Exercise 10.1 Using multiple files and preprocessor directives. File: print.h | ||
// We have seven files in all. | ||
// The file print.h contains the prototype for the function print(). | ||
|
||
#ifndef PRINT_H | ||
#define PRINT_H | ||
|
||
#include <string> | ||
using std::string; | ||
|
||
// Function prototype | ||
void print(const string& s); | ||
|
||
#endif |
12 changes: 12 additions & 0 deletions
12
9781484200087_Exercise_Solutions/Chapter 10/Soln10_01/printthat.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// Exercise 10.1 Using multiple files and preprocessor directives. File: printthat.cpp | ||
// We have seven files in all. | ||
// The file printthat.cpp contains the definition for the function printthat(). | ||
// Each of the files printthis.cpp and printthat.cpp must #include the file print.h, | ||
// as well as its own header file: | ||
#include "printthat.h" | ||
#include "print.h" | ||
|
||
void print_that(const string& s) | ||
{ | ||
print(s); | ||
} |
13 changes: 13 additions & 0 deletions
13
9781484200087_Exercise_Solutions/Chapter 10/Soln10_01/printthat.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// Exercise 10.1 Using multiple files and preprocessor directives. File: printthat.h | ||
// We have seven files in all. | ||
// The file printthat.h contains the prototype for the function print_that(). | ||
#ifndef PRINTTHAT_H | ||
#define PRINTTHAT_H | ||
|
||
#include <string> | ||
using std::string; | ||
|
||
// Function prototype | ||
void print_that(const string& s); | ||
|
||
#endif |
12 changes: 12 additions & 0 deletions
12
9781484200087_Exercise_Solutions/Chapter 10/Soln10_01/printthis.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// Exercise 10.1 Using multiple files and preprocessor directives. File: printthis.cpp | ||
// We have seven files in all. | ||
// The file printthis.cpp contains the definition for the function printthis(). | ||
// Each of the files printthis.cpp and printthat.cpp must #include the file print.h, | ||
// as well as its own header file: | ||
#include "printthis.h" | ||
#include "print.h" | ||
|
||
void print_this(const string& s) | ||
{ | ||
print(s); | ||
} |
14 changes: 14 additions & 0 deletions
14
9781484200087_Exercise_Solutions/Chapter 10/Soln10_01/printthis.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// Exercise 10.1 Using multiple files and preprocessor directives. File: printthis.h | ||
// We have seven files in all. | ||
// The file printthis.h contains the prototype for the function print_this(). | ||
|
||
#ifndef PRINTTHIS_H | ||
#define PRINTTHIS_H | ||
|
||
#include <string> | ||
using std::string; | ||
|
||
// Function prototype | ||
void print_this(const string& s); | ||
|
||
#endif |
17 changes: 17 additions & 0 deletions
17
9781484200087_Exercise_Solutions/Chapter 10/Soln10_02/main.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// Exercise 10.2 Using a global variable to count calls to print(). File: main.cpp | ||
// Only this file and print.cpp are different from Ex 10.1. | ||
|
||
#include "printthis.h" | ||
#include "printthat.h" | ||
|
||
#include <iostream> // ADDED | ||
extern int print_count = 0; // Counts calls to print() ADDED | ||
|
||
int main() | ||
{ | ||
print_this("This is a test string using printthis()."); | ||
print_that("This is a test string using printthat()."); | ||
|
||
std::cout << "The print() function has been called a total of " // ADDED | ||
<< print_count << " times. " << std::endl; // ADDED | ||
} |
16 changes: 16 additions & 0 deletions
16
9781484200087_Exercise_Solutions/Chapter 10/Soln10_02/print.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// Exercise 10.2 Using a global variable to count calls to print(). File: print.cpp | ||
// Only this file and main.cpp are different from Ex 10.1. | ||
|
||
#include "print.h" | ||
#include <string> | ||
#include <iostream> | ||
using std::string; | ||
|
||
extern int print_count; // ADDED | ||
|
||
void print(const string& s) | ||
{ | ||
std::cout << s << std::endl; | ||
++print_count; // ADDED | ||
|
||
} |
14 changes: 14 additions & 0 deletions
14
9781484200087_Exercise_Solutions/Chapter 10/Soln10_02/print.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// Exercise 10.2 Using multiple files and preprocessor directives. File: print.h | ||
// We have seven files in all. | ||
// The file print.h contains the prototype for the function print(). | ||
|
||
#ifndef PRINT_H | ||
#define PRINT_H | ||
|
||
#include <string> | ||
using std::string; | ||
|
||
// Function prototype | ||
void print(const string& s); | ||
|
||
#endif |
12 changes: 12 additions & 0 deletions
12
9781484200087_Exercise_Solutions/Chapter 10/Soln10_02/printthat.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// Exercise 10.2 Using multiple files and preprocessor directives. File: printthat.cpp | ||
// We have seven files in all. | ||
// The file printthat.cpp contains the definition for the function printthat(). | ||
// Each of the files printthis.cpp and printthat.cpp must #include the file print.h, | ||
// as well as its own header file: | ||
#include "printthat.h" | ||
#include "print.h" | ||
|
||
void print_that(const string& s) | ||
{ | ||
print(s); | ||
} |
13 changes: 13 additions & 0 deletions
13
9781484200087_Exercise_Solutions/Chapter 10/Soln10_02/printthat.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// Exercise 10.2 Using multiple files and preprocessor directives. File: printthat.h | ||
// We have seven files in all. | ||
// The file printthat.h contains the prototype for the function print_that(). | ||
#ifndef PRINTTHAT_H | ||
#define PRINTTHAT_H | ||
|
||
#include <string> | ||
using std::string; | ||
|
||
// Function prototype | ||
void print_that(const string& s); | ||
|
||
#endif |
12 changes: 12 additions & 0 deletions
12
9781484200087_Exercise_Solutions/Chapter 10/Soln10_02/printthis.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// Exercise 10.2 Using multiple files and preprocessor directives. File: printthis.cpp | ||
// We have seven files in all. | ||
// The file printthis.cpp contains the definition for the function printthis(). | ||
// Each of the files printthis.cpp and printthat.cpp must #include the file print.h, | ||
// as well as its own header file: | ||
#include "printthis.h" | ||
#include "print.h" | ||
|
||
void print_this(const string& s) | ||
{ | ||
print(s); | ||
} |
14 changes: 14 additions & 0 deletions
14
9781484200087_Exercise_Solutions/Chapter 10/Soln10_02/printthis.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// Exercise 10.2 Using multiple files and preprocessor directives. File: printthis.h | ||
// We have seven files in all. | ||
// The file printthis.h contains the prototype for the function print_this(). | ||
|
||
#ifndef PRINTTHIS_H | ||
#define PRINTTHIS_H | ||
|
||
#include <string> | ||
using std::string; | ||
|
||
// Function prototype | ||
void print_this(const string& s); | ||
|
||
#endif |
16 changes: 16 additions & 0 deletions
16
9781484200087_Exercise_Solutions/Chapter 10/Soln10_03/main.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// Exercise 10.3 Using a global variable to count calls to print(). File: main.cpp | ||
// This file is the same as Ex 10.2. | ||
|
||
#include "printthis.h" | ||
#include "printthat.h" | ||
|
||
#include <iostream> // ADDED | ||
extern int print_count = 0; // Counts calls to print() ADDED | ||
|
||
int main(){ | ||
print_this("This is a test string using printthis()."); | ||
print_that("This is a test string using printthat()."); | ||
|
||
std::cout << "The print() function has been called a total of " // ADDED | ||
<< print_count << " times. " << std::endl; // ADDED | ||
} |
18 changes: 18 additions & 0 deletions
18
9781484200087_Exercise_Solutions/Chapter 10/Soln10_03/print.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// Exercise 10.3 Using namespaces. The solution is as Exercise 10.2, with changes to | ||
// print.h, print.cpp, printthis.cpp and printthat.cpp: | ||
|
||
// print.cpp | ||
#include "print.h" | ||
#include <string> | ||
#include <iostream> | ||
using std::string; | ||
|
||
void print1::print(const string& s) | ||
{ | ||
std::cout << s << " (Namespace name is print1.) " << std::endl; | ||
} | ||
|
||
void print2::print(const string& s) | ||
{ | ||
std::cout << s << " (Namespace name is print2.) " << std::endl; | ||
} |
23 changes: 23 additions & 0 deletions
23
9781484200087_Exercise_Solutions/Chapter 10/Soln10_03/print.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Exercise 10.3 Using namespaces. The solution is as Exercise 10.2, with changes to | ||
// print.h, print.cpp, printthis.cpp and printthat.cpp: | ||
|
||
// print.h | ||
#ifndef PRINT_H | ||
#define PRINT_H | ||
|
||
#include <string> | ||
using std::string; | ||
|
||
namespace print1 | ||
{ | ||
// Function prototype | ||
void print(const string& s); | ||
} | ||
|
||
namespace print2 | ||
{ | ||
// Function prototype | ||
void print(const string& s); | ||
} | ||
|
||
#endif |
11 changes: 11 additions & 0 deletions
11
9781484200087_Exercise_Solutions/Chapter 10/Soln10_03/printthat.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// Exercise 10.3 Using namespaces. The solution is as Exercise 10.2, with changes to | ||
// print.h, print.cpp, printthis.cpp and printthat.cpp: | ||
|
||
// printthat.cpp | ||
#include "printthat.h" | ||
#include "print.h" | ||
|
||
void print_that(const string& s) | ||
{ | ||
print2::print(s); | ||
} |
14 changes: 14 additions & 0 deletions
14
9781484200087_Exercise_Solutions/Chapter 10/Soln10_03/printthat.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// Exercise 10.2 Using multiple files and preprocessor directives. File: printthat.h | ||
// We have seven files in all. | ||
// The file printthat.h contains the prototype for the function print_that() | ||
// and is the same as for Exercise 10.2. | ||
#ifndef PRINTTHAT_H | ||
#define PRINTTHAT_H | ||
|
||
#include <string> | ||
using std::string; | ||
|
||
// Function prototype | ||
void print_that(const string& s); | ||
|
||
#endif |
11 changes: 11 additions & 0 deletions
11
9781484200087_Exercise_Solutions/Chapter 10/Soln10_03/printthis.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// Exercise 10.3 Using namespaces. The solution is as Exercise 10.2, with changes to | ||
// print.h, print.cpp, printthis.cpp and printthat.cpp: | ||
|
||
// printthis.cpp | ||
#include "printthis.h" | ||
#include "print.h" | ||
|
||
void print_this(const string& s) | ||
{ | ||
print1::print(s); | ||
} |
15 changes: 15 additions & 0 deletions
15
9781484200087_Exercise_Solutions/Chapter 10/Soln10_03/printthis.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// Exercise 10.3 Using multiple files and preprocessor directives. File: printthis.h | ||
// We have seven files in all. | ||
// This file printthis.h contains the prototype for the function print_this() | ||
// and is the same as for Exercise 10.2. | ||
|
||
#ifndef PRINTTHIS_H | ||
#define PRINTTHIS_H | ||
|
||
#include <string> | ||
using std::string; | ||
|
||
// Function prototype | ||
void print_this(const string& s); | ||
|
||
#endif |
25 changes: 25 additions & 0 deletions
25
9781484200087_Exercise_Solutions/Chapter 10/Soln10_04/main.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Exercise 10.4 More applications of preprocessor directives. | ||
|
||
// main.cpp | ||
#include "printthis.h" | ||
#include "printthat.h" | ||
|
||
//#define DO_THIS // uncomment to #define the token | ||
|
||
#ifdef DO_THIS | ||
#define PRINT(val) print_this(#val); | ||
#else | ||
#define PRINT(val) print_that(#val); | ||
#endif | ||
|
||
int main() | ||
{ | ||
#ifdef DO_THIS | ||
print_this("This is a test string using printthis()."); | ||
#else | ||
print_that("This is a test string using printthat()."); | ||
#endif | ||
|
||
std::cout << "The print() function has been called a total of " // ADDED | ||
<< print_count << " times. " << std::endl; // ADDED | ||
} |
Oops, something went wrong.