forked from jeremyhansen/rooksguide-cplusplus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chap_constants.tex
100 lines (79 loc) · 3.83 KB
/
chap_constants.tex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
% This work by Jeremy A. Hansen is licensed under a Creative Commons
% Attribution-NonCommercial-ShareAlike 3.0 Unported License,
% as described at http://creativecommons.org/licenses/by-nc-sa/3.0/legalcode
\LevelD{Literals}
A literal is a value outside of a variable such as $5$, $9$, $103$, and $-21$.
Each of those is an \Code{int}, but a literal can be of any data type.
The point is, these are values that the C++ compiler already recognizes, and can't be changed.
In other words, you can't convince the compiler to give the literal 3 the value of 4, because 3 is constant.
Table \ref{table-literal-examples} contains a few examples.
\begin{table}
\centering
\begin{tabular}{| c | c |}
\hline
\textbf{Literal value} & \textbf{Data Type} \\ \hline
\Code{123.45f} & \Code{float} \\ \hline
\Code{13.8903} & \Code{double} \\ \hline
\Code{-389283220.342423} & \Code{double} \\ \hline
\Code{49e-8} & \Code{double} \\ \hline
\Code{12} & \Code{int} \\ \hline
\Code{12u} & \Code{unsigned int} \\ \hline
\Code{'x'} & \Code{char} \\ \hline
\Code{"text"} & \Code{string} \\ \hline
\Code{true} & \Code{bool} \\ \hline
\Code{false} & \Code{bool} \\ \hline
\end{tabular}
\caption{Examples of a few literals} \label{table-literal-examples}
\end{table}
\LevelD{Declared Constants}
We call a variable whose value we cannot change a constant.
After you declare a constant, you are unable to change it, no matter what.
The difference between declaring a normal variable and a constant is that we simply place the keyword \Code{const} before the data type in the declaration.
This indicates whatever variable and type that follows the \Code{const} will be a constant and cannot be changed.
Since it is a constant, we will also need to initialize the value at the time we declare the variable.
Here is an example (we cover the \Code{cout} object shortly in Chapter \ref{chap_output}): \nopagebreak[4]
\noindent\begin{minipage}{\linewidth}\begin{lstlisting}
const float pi = 3.14;
float radius = 5, area;
area = radius * radius * pi;
cout << area; // outputs 78.5
\end{lstlisting}\end{minipage}
\LevelD{Review Questions}
\begin{enumerate}
\item Describe the difference between literals and declared constants.
When would a declared constant be more useful than a literal constant?
\item What is the difference between a normal variable and a constant?
% \item Write code that uses a \Code{const} to add 3\% interest to a bank account every year. Print out the balance for the next ten years.
\item Build a program in C++ that does the following:
\begin{enumerate}
\item Declare a \Code{double} variable named \Code{Feet}. Initialize it to your height.
\item Declare a \Code{double} constant named \Code{MetersPerFoot}, with the value of $0.3048$.
\item Declare a \Code{double} variable named \Code{Meters}. Set it to \Code{Feet} multiplied by \Code{MetersPerFoot}.
% \item Display \Code{Meters} to the screen.
\end{enumerate}
\item Create a program that displays the diameter and area of a circle for any given radius. Use a \Code{const float} to represent $\pi$.
\end{enumerate}
\LevelD{Review Answers}
\begin{enumerate}
\item A literal is a value not stored in a variable; a constant is an unchanging value stored in a variable.
\item Normal variables can be changed or overwritten; constants cannot be changed or overwritten.
\item
\noindent\begin{minipage}{\linewidth}\begin{lstlisting}
double Feet 5.5;
const double MetersPerFoot = .3048;
double Meters = Feet * MetersPerFoot;
\end{lstlisting}\end{minipage}
\item
\noindent\begin{minipage}{\linewidth}\begin{lstlisting}
float radius = 5;
const float pi = 3.14159
double diameter, area;
diameter = radius * 2;
area = pi * (radius * radius)
\end{lstlisting}\end{minipage}
\end{enumerate}
%\LevelD{Further Reading}
%\begin{itemize}
%\item \url{~}
%\item \url{~}
%\end{itemize}