Skip to content

Commit

Permalink
Preprocessor, problem solving, separate compilation, STL chapters, mi…
Browse files Browse the repository at this point in the history
…nor fix in cpp.tex
  • Loading branch information
jeremyhansen committed Oct 11, 2013
1 parent d4359e3 commit 52be944
Show file tree
Hide file tree
Showing 5 changed files with 520 additions and 39 deletions.
59 changes: 59 additions & 0 deletions chap_preproc.tex
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
Preprocessor directives are lines of code that are executed before the compilation of the code begins.
These directives are like getting everyone in a room before starting a project or doing warmups before running a race.
One of the most frequently-used preprocessor directives is \Code{\#include}.

When we want to include in our code a system library or some other file, we use the keyword \Code{\#include} followed by the library name or the file name.
The way we distinguish between including libraries and including files is with angle brackets and quotes, respectively.
For example, when we want to use objects like \Code{cout} or \Code{cin}, we need to include the \Code{iostream} library like so:

\begin{lstlisting}
#include <iostream>
\end{lstlisting}

If we want to include a file, such as a file named \Code{myFile.h}, we can write:

\begin{lstlisting}
#include "myFile.h"
\end{lstlisting}

However, when we include files, they must be in the same directory as the file where the \Code{\#include} appears.
We discuss the Standard Template Library in Chapter \ref{chap_stl}, and include a short sample of other libraries below:

\begin{table}[tb]
\centering
\begin{tabular}{| l | p{0.8in} | p{2in} |}
\hline
\textbf{Library} & \textbf{Provides} & \textbf{Some common uses} \\ \hline

\Code{<iostream>} & Input/output stream objects & \Code{cout}, \Code{cin}: see Chapters~\ref{chap_input}~and~\ref{chap_output} \\ \hline
\Code{<cstdlib>} & The C standard library & \Code{rand()}, \Code{abs()}, \Code{NULL} \\ \hline
\Code{<math>} & Mathematical functions & \Code{pow()}, \Code{sqrt()}, \Code{cos()}, \Code{tan()}, \Code{sin()}: see Chapter~\ref{chap_advancedarith} \\ \hline
\Code{<iomanip>} & Input/output manipulation & \Code{get\_money()}, \Code{get\_time()}, \Code{put\_time()} \\ \hline
\Code{<ctime>} & Time-related functions & \Code{clock()}, \Code{time()}, \Code{ctime()} \\ \hline
\Code{<string>} & The \Code{string} class & See Chapter~\ref{chap_string} \\ \hline
\Code{<fstream>} & File input and output streams & See Chapter~\ref{chap_file_io} \\ \hline

\end{tabular}
\end{table}

%TODO: #if, #ifdef, #ifndef, #define, etc.


\LevelD{Review Questions}

\LevelD{Homework Questions}

\LevelD{Review Answers}

\LevelD{Homework Answers}

\LevelD{Further Reading}

\begin{itemize}
\item \url{~}
\item \url{~}
\item \url{~}
\end{itemize}



100 changes: 100 additions & 0 deletions chap_problems.tex
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
Problem solving and troubleshooting in programming is often referred to as debugging.
Does your program not compile?
Does it not achieve the desired effect?
Debugging is your answer.
And, unless you are a perfect programmer, you are likely to do quite a bit of debugging.

The first step to debugging is looking for common errors.

\LevelD{The Compilation Error}

These errors happen when your compiler returns an error message after you hit compile.
The messages usually tell you what is wrong, and what line the error is on, but be sure to double-check the lines immediately before and after the reported error.
Because the code is incorrect, the compiler can only guess at what you meant and give you a hint.

%TODO: screenshot
% \label{fig-netbeans-syntax-error}

For example, one of the most common errors a beginning programmer will encounter is forgetting a semicolon.
In some development environments (like NetBeans in Figure \ref{fig-netbeans-syntax-error}), this will cause the error to be reported not on the line with the missing semicolon, but on the following line.

\LevelD{The Logic Error}

Logic errors are often subtle, and occur after the code compiles.
When the code is executed, however, the result is wrong.
This may happen when arithmetic operators like \Code{+}, \Code{-}, \Code{*}, and \Code{/} get mixed up.
Another common issue is misplacement of parentheses, as a misplaced parenthesis can cause problems in complex expressions.

\LevelD{The Infinite Loop}

Another specific logic error is the infinite loop.
The infinite loop is a common error that can result in your program repeating the same block of code over and over.

For an infinite loop to occur, the conditional expression of a \Code{while}, \Code{for}, or \Code{do-while} loop remains true.
There are many ways for this to happen, such as accidentally using \Code{=} instead of \Code{==} to compare two numbers, or using the wrong operators, like a \Code{>} in the place of a \Code{<}.

%TODO: screenshot

\LevelD{Review Questions}
\begin{enumerate}
\item Consider the following function:
\begin{lstlisting}
double average (double s1, double s2, double s3, s4);
{
retun s1+s2+s3+s4/ 4
}
\end{lstlisting}
\begin{enumerate}
\item Find the syntax errors in the function.
\item There is a logic error in the function. What is it? How does it affect the output of the code?
\end{enumerate}

\item The below program compiles, but does not get the result the programmer wanted. Why?

\begin{lstlisting}
int main()
{
int shots, goals, saves;
double save_perc;
char cont;

cout.unsetf(ios::fixed);
cout.unsetf(ios::showpoint);

cout << "Enter the number of shots on goal:\t";
cin >> shots;
cout << "Enter the number of goals scored:\t";
cin >> goals;
cout << endl;

saves = shots - goals;

save_perc = (saves / shots); //no *100; hockey shows save perc as decimal to three places
cout << "If there were " << shots << " shots and " << goals << " goals\n";
cout << "then the goalie's save percentage was ";

cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(3);

cout << save_perc << endl << endl;
return 0;
}
\end{lstlisting}

\end{enumerate}

\LevelD{Homework Questions}

\LevelD{Review Answers}

\LevelD{Homework Answers}

\LevelD{Further Reading}

\begin{itemize}
\item \url{~}
\item \url{~}
\item \url{~}
\end{itemize}

113 changes: 113 additions & 0 deletions chap_separate.tex
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
Separate compilation is the process of breaking a C++ program into separate files to improve organization.
Parts of the program can be spread out over a number of different files that are later compiled individually, then \Keyword{linked} using a linker to produce the final, working program.
When changes are made, only those files with changes need to be recompiled, the result of which can then be relinked with the previously-compiled files.
This process is nearly invisible in most development environments, which recompile and relink these files automatically.
When the development environment takes care of these details, the user is left with the sole task of making changes where they are needed.

One of the most basic applications of separate compilation is used when writing abstract data types.
Recall from Chapter \ref{chap_classes} that there are declaration and definition sections in a \Code{class}.
The declaration contains class functions and variables, both public and private, while the definition section is where the function definitions and most actual code can be found.
The process of separate compilation requires the two sections to be split into separate files, each of which is written and maintained separately and later used together to create a working program.

Declarations will be put into the \Keyword{interface} file or the \Keyword{header file} which typically has a \Code{.h} suffix.
In most code written by novice programmers, there will be only one \Code{class} declaration in each header file.
To use the \Code{class} in your code elsewhere, you should use \Code{\#include} followed by the file name in double quotes.
Here is an example of the contents of an interface file called \Code{student.h}:

\begin{lstlisting}
#include <string>

using namespace std;

class student
{
public:
student();
int getAge();
void setAge(int update);
int getID();
void setID(int update);
string getName();
void setName(string update);
private:
int age;
int ID;
string name;
};
\end{lstlisting}

To use the \Code{student} class in some other source code file, that file should include the following line:

\begin{lstlisting}
#include "student.h"
\end{lstlisting}

The quotes around \Code{student.h} tell the compiler to find the header file in the same directory as the current file.

The \Keyword{implementation} file will include all the function definitions for the \Code{student} class.
The implementation file can be called anything the programmer wants, but typically ends with a \Code{.cpp} suffix.
For example, the implementation file for \Code{student} will probably be \Code{student.cpp}.

To ensure that a new implementation file is compiled into your program, you do not need to \Code{\#include} anything.
However, the development environment will automatically compile and link the implementation file if it has been added to your project.
The only files that you should \Code{\#include} are header files.

To avoid linker errors, your files should have safeguards to ensure that classes and functions are not declared more than once within the same program.
These safeguards are simple, and should be included in each header file.
For example, we place the following two lines at the top of the file \Code{student.h}:

\begin{lstlisting}
#ifndef STUDENT_H // STUDENT_H could be anything
#define STUDENT_H // as long as it is unique to this file
\end{lstlisting}

The following line should go at the end of the same file:

\begin{lstlisting}
#endif //STUDENT_H – a reminder about the #ifndef above
\end{lstlisting}

These three lines do the following:

\begin{enumerate}
\item Test if \Code{STUDENT\_H} has been previously \Code{\#define}d, usually because this header file has been \Code{\#include}d elsewhere.
\item If it has not been \Code{\#define}d, \Code{\#define} it now and proceed with compiling the code between the \Code{\#ifndef} and \Code{\#endif}.
\item Close the \Code{\#ifndef} block. If \Code{STUDENT\_H} was previously defined, skip to the line after this one.
\end{enumerate}

Here is an example of what these lines look like alongside some actual code:

\begin{lstlisting}
#ifndef STUDENT_H
#define STUDENT_H

class student
{
//class declaration because this is an interface file
};

#endif//STUDENT_H
\end{lstlisting}

Following this example will ensure that the \Code{student} class is only defined once.






\LevelD{Review Questions}

\LevelD{Homework Questions}

\LevelD{Review Answers}

\LevelD{Homework Answers}

\LevelD{Further Reading}

\begin{itemize}
\item \url{~}
\item \url{~}
\item \url{~}
\end{itemize}

0 comments on commit 52be944

Please sign in to comment.