Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Apress committed Oct 18, 2016
0 parents commit 09ee066
Show file tree
Hide file tree
Showing 174 changed files with 8,058 additions and 0 deletions.
8 changes: 8 additions & 0 deletions 9781484200087_Exercise_Solutions/Chapter 1/Soln1_01.cpp
@@ -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;
}
11 changes: 11 additions & 0 deletions 9781484200087_Exercise_Solutions/Chapter 1/Soln1_02.cpp
@@ -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
)
9 changes: 9 additions & 0 deletions 9781484200087_Exercise_Solutions/Chapter 1/Soln1_03.cpp
@@ -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 9781484200087_Exercise_Solutions/Chapter 10/Soln10_01/main.cpp
@@ -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 9781484200087_Exercise_Solutions/Chapter 10/Soln10_01/print.cpp
@@ -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 9781484200087_Exercise_Solutions/Chapter 10/Soln10_01/print.h
@@ -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
@@ -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 9781484200087_Exercise_Solutions/Chapter 10/Soln10_01/printthat.h
@@ -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
@@ -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 9781484200087_Exercise_Solutions/Chapter 10/Soln10_01/printthis.h
@@ -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 9781484200087_Exercise_Solutions/Chapter 10/Soln10_02/main.cpp
@@ -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 9781484200087_Exercise_Solutions/Chapter 10/Soln10_02/print.cpp
@@ -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 9781484200087_Exercise_Solutions/Chapter 10/Soln10_02/print.h
@@ -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
@@ -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 9781484200087_Exercise_Solutions/Chapter 10/Soln10_02/printthat.h
@@ -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
@@ -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 9781484200087_Exercise_Solutions/Chapter 10/Soln10_02/printthis.h
@@ -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 9781484200087_Exercise_Solutions/Chapter 10/Soln10_03/main.cpp
@@ -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 9781484200087_Exercise_Solutions/Chapter 10/Soln10_03/print.cpp
@@ -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 9781484200087_Exercise_Solutions/Chapter 10/Soln10_03/print.h
@@ -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
@@ -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 9781484200087_Exercise_Solutions/Chapter 10/Soln10_03/printthat.h
@@ -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
@@ -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 9781484200087_Exercise_Solutions/Chapter 10/Soln10_03/printthis.h
@@ -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 9781484200087_Exercise_Solutions/Chapter 10/Soln10_04/main.cpp
@@ -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
}

0 comments on commit 09ee066

Please sign in to comment.