% Created 2022-03-22 Tue 04:12
% Intended LaTeX compiler: pdflatex
\documentclass[11pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{graphicx}
\usepackage{grffile}
\usepackage{longtable}
\usepackage{wrapfig}
\usepackage{rotating}
\usepackage[normalem]{ulem}
\usepackage{amsmath}
\usepackage{textcomp}
\usepackage{amssymb}
\usepackage{capt-of}
\usepackage{hyperref}
\input{../preamble}
\author{Instructor: Mehmet Emre}
\date{CS 32 Spring '22}
\title{Homework 2: Templates and the standard library}
\hypersetup{
 pdfauthor={Instructor: Mehmet Emre},
 pdftitle={Homework 2: Templates and the standard library},
 pdfkeywords={},
 pdfsubject={},
 pdfcreator={Emacs 27.2 (Org mode 9.4.4)}, 
 pdflang={English}}
\begin{document}

\maketitle
\textbf{Due: 4/6 12:30pm} \\ 
\textbf{Name \& Perm \#:} \\ 
\textbf{Homework buddy (leave blank if you worked alone):}


\textbf{Reading:} "Templates and the STL", PS 8.3, 17, 18

\section{(10 pts)}
\label{sec:org04b3bf2}
In the lecture, we talked about STL being a misnomer. STL was a
historical predecessor of parts of the C++ standard
library. Nowadays, we would refer to those parts by name. What are
the four components of STL that are now part of the standard library?

\vspace{2em}

\section{(8 pts)}
\label{sec:org51d9e2c}
Suppose we have a C++ class called \texttt{Student}. There are many ways one can create a collection \texttt{Student} objects.  Among these: one can use a normal C++ array or an STL \texttt{vector}, and one could create a collection of objects, or of pointers to objects (presumably allocated on the heap.) 
\begin{enumerate}
\item (2 pts) Write a line of C++ code that declares an array \texttt{a} that can hold 10 objects of type \texttt{Student}.
\vspace{3em}
\item (2 pts) Write a line of C++ code that declares an array \texttt{b} that can hold 10 pointers to objects of type \texttt{Student}.
\vspace{3em}
\item (2 pts) Write a line of C++ code that declares a standard library vector \texttt{c} that can hold 10 objects of type \texttt{Student}.
\vspace{3em}
\item (2 pts) Write a line of C++ code that declares a standard library vector \texttt{d} that can hold 10 pointers to objects of type \texttt{Student}.

\newpage
\end{enumerate}

\section{(20 pts)}
\label{sec:org0e79471}
In the previous problem, you were asked to write four different
declarations, 1, 2, 3, 4.  Each of these has "pros" and "cons".
There are also differences among them that we might describe as
"neutral", for whether they are a pro/con depends on the context.
For the "pros" and "cons" below, please indicate which of the
options above (1, 2, 3, 4) the statement applies to.  Note that in
some cases, you may have to indicate more than one number.

Circle the numbers to which the statement applies.  If you mess up,
cross out all the letters and list the letters in the space to the
right.

\begin{center}
\begin{tabular}{p{14cm}|c}
Hero or zero? & Choices\\
\hline
Con: Additional \texttt{\#include} directives are required to use this approach. & \(1 \quad 2 \quad 3 \quad 4\)\\
\hline
Pro: The item declared can be expanded beyond 10 items if needed. & \(1 \quad 2 \quad 3 \quad 4\)\\
\hline
Pro: You can declare this item without actually creating any Student objects. & \(1 \quad 2 \quad 3 \quad 4\)\\
\hline
Con: The class MUST have a default constructor, or this declaration is not permitted. & \(1 \quad 2 \quad 3 \quad 4\)\\
\hline
Pro: All space for the item and the Students it contains are co-located in memory--on the stack if the item is declared as a local. & \(1 \quad 2 \quad 3 \quad 4\)\\
\end{tabular}
\end{center}

\newpage

\section{(3 pts)}
\label{sec:org618d2e3}
In your own words, what is the "problem" for which "templates for
functions" is a solution? I'm looking for a \textbf{brief}
description--a single sentence, or at most 2-3 sentences that
get to the \emph{central point}, a description of the \emph{problem}, not a
detailed description of everything you know about templates, and
certainly \emph{not} a sentence copied, word-for-word, from either
textbook.

\vspace{3em}

\section{(3 pts)}
\label{sec:org3ade83d}
The C++ syntax for function templates includes \texttt{template <class
  Item>}.  The name \texttt{Item} in the example above is preceeded by the
keyword \texttt{class}.  One might infer from that that \texttt{Item} should be
the name of some \texttt{class}, e.g. \texttt{class Student}, or \texttt{class Roster},
etc.  But that is not a correct assumption.  Explain
why \textbf{briefly}.

\vspace{3em}

\section{(6 pts)}
\label{sec:org1cba9c0}

Let's say you have a function \texttt{int calc(int operand1, int operand2,
char op)} where \texttt{op} is either \texttt{+} or \texttt{-}.  Instead of overloading
this function for each numerical datatype in C++ (e.g. also defining
it for \texttt{float}, \texttt{double}, etc., you want to create a generic function
that is far more flexible.  In the space below, define \texttt{calc} as a
template function.  If op is neither \texttt{+} nor \texttt{-} , print an error
message on \texttt{std::cerr} and call the system function \texttt{exit(1);} (you
may assume that \texttt{\#include <cstdlib>} has already been done.)\footnote{Note
that once we've covered exceptions, that would be a better approach
than the \texttt{cerr/exit} technique.}
\end{document}