Skip to content
Snippets Groups Projects
Commit ca2ef2e5 authored by Steven Palmer's avatar Steven Palmer
Browse files

T09 material

parent e1b5417c
No related branches found
No related tags found
No related merge requests found
Showing
with 551 additions and 84 deletions
#include "ClassA.h"
ClassA::ClassA(int a)
{
this->a = a;
}
\ No newline at end of file
#ifndef CLASSA_H
#define CLASSA_H
class ClassA {
private:
int a;
public:
ClassA(int a);
};
#endif
\ No newline at end of file
#include "ClassB.h"
#include "ClassA.h"
// we use the initializer list a(ca)
// to initialize the field a with ca
ClassB::ClassB(ClassA ca) : a(ca)
{
}
\ No newline at end of file
#ifndef CLASSB_H
#define CLASSB_H
#include "ClassA.h"
class ClassB {
private:
ClassA a;
public:
ClassB(ClassA ca);
};
#endif
\ No newline at end of file
#include "ClassA.h"
ClassA::ClassA(int a)
{
this->a = a;
}
\ No newline at end of file
#ifndef CLASSA_H
#define CLASSA_H
class ClassA {
private:
int a;
public:
ClassA(int a);
};
#endif
\ No newline at end of file
#include "ClassB.h"
#include "ClassA.h"
ClassB::ClassB(ClassA ca)
{
this->a = ca;
}
\ No newline at end of file
#ifndef CLASSB_H
#define CLASSB_H
#include "ClassA.h"
class ClassB {
private:
ClassA a;
public:
ClassB(ClassA ca);
};
#endif
\ No newline at end of file
#include "Example.h"
template <class T>
Example<T>::Example(T a){
this->a = a;
}
template <class T>
T Example<T>::getA(){
return a;
}
\ No newline at end of file
#ifndef EXAMPLE_H
#define EXAMPLE_H
template <class T>
class Example {
private:
T a;
public:
Example(T a);
T getA();
};
#endif
\ No newline at end of file
#include "Example.h"
int main(){
Example<int> exInt(4);
Example<double> exDbl(4.0);
}
\ No newline at end of file
File added
% Define Document Class
% Class Options Include:
% notes, notesonly, handout, trans,
% hidesubsections, shadesubsections,
% inrow, blue, red, grey, brown
%--------------------------------------------------------------------------
\documentclass[xcolor=dvipsnames, shownotes, colorlinks]{beamer}
%--------------------------------------------------------------------------
% -------------------------------------------------------------------------
% Define Package Theme
%--------------------------------------------------------------------------
\usepackage{color}
\usepackage[T1]{fontenc}
%% \usepackage{fix-cm}
\usepackage{hyperref}
\hypersetup{
urlcolor=cyan,
linkcolor=white
}
\usepackage{subfigure}
%% \usepackage{xspace}
\usepackage{}
\usepackage{enumerate}
\usetheme{Antibes}
\setbeamertemplate{sidebar}[right]
\usepackage{listings} % Code formatting
\usepackage{lstautogobble}
%% \usepackage{framed}
\usepackage{booktabs}
\usepackage{tabularx}
\usepackage{marvosym}
\usepackage{tikz}
\usetikzlibrary{shapes}
\usetikzlibrary{arrows}
\usetikzlibrary{calc,positioning}
\setbeamertemplate{caption}[numbered]
\lstset{language=C++,
basicstyle=\ttfamily,
keywordstyle=\color{blue}\ttfamily,
stringstyle=\color{red}\ttfamily,
commentstyle=\color{ForestGreen}\ttfamily,
morecomment=[l][\color{magenta}]{\#},
autogobble=true,
showstringspaces=false,
escapeinside={<@}{@>}
}
%------------------------------------------------------------------------------
% Commands
%------------------------------------------------------------------------------
\newcommand*\oldmacro{}%
\let\oldmacro\insertshorttitle%
\renewcommand*\insertshorttitle{%
\oldmacro\hfill%
\insertframenumber\,/\,\inserttotalframenumber}
% Figure Source
\usepackage[absolute,overlay]{textpos}
\setbeamercolor{framesource}{fg=gray}
\setbeamerfont{framesource}{size=\tiny}
\newcommand{\source}[1]{\begin{textblock*}{\paperwidth}(-5pt,\textheight)
\begin{beamercolorbox}[ht=0.5cm,right]{framesource}
\usebeamerfont{framesource}\usebeamercolor[fg]{framesource} Source: {#1}
\end{beamercolorbox}
\end{textblock*}}
% Presentation Title Slide
%--------------------------------------------------------------------------
\title{C++ for A3\\(miscellaneous topics that will be helpful for A3)}
\subtitle{CS 2ME3/SE 2AA4}
\author{Steven Palmer}
\institute{Department of Computing and Software\\
McMaster University\\ }
\date{}
%--------------------------------------------------------------------------
% Document
% To add notes to the slides use: \note{}
% Add \section{} or \subsection{} for use in table of contents
%--------------------------------------------------------------------------
\begin{document}
% Create Title Slide
\begin{frame}
\maketitle
\end{frame}
\section[Outline]{}
% Create Table of Contents Slide - Outline
\begin{frame}
\frametitle{Outline}
{\hypersetup{linkcolor=black}
\tableofcontents
}
\end{frame}
% -----------------------------------------------------
\section{Compiling on Mills}
% -----------------------------------------------------
% -----------------------------------------------------
\begin{frame}[t]
\frametitle{A Note About Compiling on Mills}
\begin{itemize}
\item Your code for A3 should adhere to the \texttt{C++11} standard
\item The default version of \texttt{g++} on mills is outdated -- it will not recognize the \texttt{-std=c++11} option
\item If you try to run the makefile for A3 on Mills, it will fail
\item You must first run the command:
\item[] \qquad \texttt{\bf . /opt/rh/devtoolset-7/enable}
\item This will update the \texttt{g++} version to one that supports \texttt{C++11}
\item You will then be able to compile A3 successfully
\end{itemize}
\end{frame}
% -----------------------------------------------------
\begin{frame}[t]
\frametitle{Automating It}
\begin{itemize}
\item Everytime you ssh into Mills, you will need to run the command on the previous slide
\item For convenience, you can edit your \texttt{.bashrc} file so that this command is automatically run every time you log in by doing the following:\\
\begin{enumerate}
\item run
\item[] \qquad \texttt{nano \textasciitilde/.bashrc}
\item paste
\item[] \qquad \texttt{. /opt/rh/devtoolset-7/enable}
\item[] on a new line at the end of the file
\item CTRL + O, then ENTER to save
\item CTRL + X to exit nano
\end{enumerate}
\end{itemize}
\end{frame}
% -----------------------------------------------------
\section{Initializer Lists}
% -----------------------------------------------------
% -----------------------------------------------------
\begin{frame}[t,fragile]
\frametitle{Initializing Classes}
\begin{itemize}
\item When you create a class instance in C++, all class fields are initialized to default values \emph{before} the constructor is called
\item This can be a problem if you've defined classes for which you have supplied custom constructors, but have not defined a default constructor (one that takes no arguments)
\item For an example of why this might be a problem, see the example code on the slide
\end{itemize}
\end{frame}
% -----------------------------------------------------
\begin{frame}[t,fragile]
\frametitle{A Failing Example}
\begin{lstlisting}
class MyClass {
private:
int a;
public:
MyClass(int a) { this->a = a; }
};
class MyOtherClass {
private:
MyClass m;
public:
MyOtherClass(MyClass mc) { this->m = mc; }
};
\end{lstlisting}
\end{frame}
% -----------------------------------------------------
\begin{frame}[t]
\frametitle{Why The Previous Example Fails}
\begin{itemize}
\item The code on the previous slide will fail to compile
\item In \texttt{MyClass}, we did not define a default constructor -- this class has no default value
\item Since the fields of class instances are initialized to default values before the constructor is called,
the compiler won't know what to do about the \texttt{MyClass m} field of \texttt{MyOtherClass}
\item Even though we never actually use the default value, and we assign to \texttt{m} in the constructor, the compiler will still insist on a default value
\item This happens fairly often, where we have a class with no default constructor as a field of another class -- so how do we get around this problem?
\end{itemize}
\end{frame}
% -----------------------------------------------------
\begin{frame}[t,fragile]
\frametitle{Initializer Lists}
\begin{itemize}
\item Initializer lists are used to initialize class variables immediately with values when the class is being instantiated -- no default values are constructed for fields with initializers
\item When defining a constructor, we can put an initializer list right after the signature like this:
\end{itemize}
\begin{lstlisting}
Class::Class(...) : fld1(val1), fld2(val2), ...
{
// constructor body if necessary
// or just blank body
}
\end{lstlisting}
\begin{itemize}
\item where fld1, fld2, etc. are the field names that you want to initialize, and val1, val2, etc. are the values you want to initialize them with
\end{itemize}
\end{frame}
% -----------------------------------------------------
\begin{frame}[t,fragile]
\frametitle{Initializer List Example}
\begin{itemize}
\item For example, we could fix the previous failing example by changing the definition of the MyOtherClass to be:
\end{itemize}
\begin{lstlisting}
class MyOtherClass {
private:
MyClass m;
public:
MyOtherClass(MyClass mc) : m(mc) { }
};
\end{lstlisting}
\begin{itemize}
\item Using the initializer list means the compiler will no longer try to construct a default value for \texttt{m} when an instance of the class is created
\end{itemize}
\end{frame}
% -----------------------------------------------------
\begin{frame}[t,fragile]
\frametitle{Exercise 1: Initializer Lists}
\begin{block}{Exercise 1}
There is some example code in \texttt{example/initializer/without}. In this code, \texttt{ClassB} has a field of type \texttt{ClassA}. In \texttt{ClassA} we have defined a constructor that takes an \texttt{int}, but no default constructor. Try compiling this code:\\[\baselineskip]
\qquad\texttt{g++ -c *.cpp}\\[\baselineskip]
You should find that there is a compilation error. Since we didn't use an initializer list, the compiler doesn't know how to construct a default value for the \texttt{ClassA} field in \texttt{ClassB}.
\end{block}
\end{frame}
% -----------------------------------------------------
\begin{frame}[t,fragile]
\frametitle{Exercise 1: Initializer Lists}
\begin{block}{Exercise 1}
Now look at the code in \texttt{example/initializer/with}. This code is the same as before, except we have used an initializer list in \texttt{ClassB}. You should be able to compile this without issue:\\[\baselineskip]
\qquad\texttt{g++ -c *.cpp}
\end{block}
\end{frame}
% -----------------------------------------------------
\begin{frame}[t,fragile]
\frametitle{Initializer List Hint}
{\bf HINT for A3:}
\begin{itemize}
\item You will require an initializer list in your definition of the LineT constructor in LineADT.cpp
\end{itemize}
\end{frame}
% -----------------------------------------------------
\section{Template Classes}
% -----------------------------------------------------
% -----------------------------------------------------
\begin{frame}[t]
\frametitle{Template (Generic) Class Implementation}
\begin{itemize}
\item Template classes are not actually class definitions -- they are patterns that the compiler uses to generate a family of classes
\item To generate a class from a template class, the compiler needs to ``see'' the entire pattern -- it needs the full definition, not just the declaration
\item If we compile .cpp files without the full definition, the linker will complain about undefined references
\item This mean that if we separate the declaration and definitions of a template class into .h and .cpp files, we will run into linking errors when trying to link other source files that use instantiations of the template class
\end{itemize}
\end{frame}
% -----------------------------------------------------
\begin{frame}[t]
\frametitle{Ways to Handle Template Class Issue}
There are two ways around this issue, but each come with pros and cons:
\begin{enumerate}
\item Explicit instantiation of the template class with particular types in the .cpp file
\begin{itemize}
\item Pro: you are still able to hide the implementation
\item Con: you can only use the generic class with the types that have been explicitly instantiated
\end{itemize}
\item Writing the full class definition in the .h file with no .cpp file
\begin{itemize}
\item Pro: you get a true generic class that can be instantiated with any type
\item Con: you expose the implementation details
\end{itemize}
\end{enumerate}
\end{frame}
% -----------------------------------------------------
\begin{frame}[t,fragile]
\frametitle{Explicit Instantiation of Template Classes}
\begin{itemize}
\item Say we have a template class called \texttt{MyClass} that we declare in a header file, i.e.
\end{itemize}
\begin{lstlisting}
template <class T>
class MyClass {
...
};
\end{lstlisting}
\begin{itemize}
\item In the corresponding source file, after we define all of the member functions, we can make explicit instantiations via:
\end{itemize}
\begin{lstlisting}
template class MyClass<int>;
template class MyClass<bool>;
template class MyClass<MyOtherClass>; // etc.
\end{lstlisting}
\end{frame}
% -----------------------------------------------------
\begin{frame}[t]
\frametitle{Exercise 2: Explicit Instantiation of Template Classes}
\begin{block}{Exercise 2}
There is some example code in \texttt{example/template}. This code declares a template class called Example in \texttt{Example.h}, with definitions in \texttt{Example.cpp}. There is also a main function in \texttt{main.cpp} that tries to make instances of the Example class instantiated with \texttt{int} and \texttt{double}.\\[\baselineskip]
Try compiling this code into a program:\\[\baselineskip]
\qquad\texttt{g++ -c *.cpp}
\qquad\texttt{g++ -o prog *.o}
\end{block}
\end{frame}
% -----------------------------------------------------
\begin{frame}[t,fragile]
\frametitle{Exercise 2: Explicit Instantiation of Template Classes}
\begin{block}{Exercise 2}
Your compilation attempt should have failed with a linking error about undefined references to \texttt{Example<int>} and \texttt{Example<double>}. The linker see we are trying to use \texttt{Example<int>} and \texttt{Example<double>} in \texttt{main.o}, but can't resolve them to a type. This is a result of the problem discussed earlier -- we can't separate the declaration and definition of a template class in the usual way.\\[\baselineskip]
Now let's try adding explicit instantiations. At the bottom of \texttt{Example.cpp}, add the following lines:\\[\baselineskip]
\begin{lstlisting}
template class Example<int>;
template class Example<double>;
\end{lstlisting}
\end{block}
\end{frame}
% -----------------------------------------------------
\begin{frame}[t,fragile]
\frametitle{Exercise 2: Explicit Instantiation of Template Classes}
\begin{block}{Exercise 2}
Now try compiling again:\\[\baselineskip]
\qquad\texttt{g++ -c *.cpp}
\qquad\texttt{g++ -o prog *.o}\\[\baselineskip]
The explicit instantiations of \texttt{Example<int>} and \texttt{Example<double>} cause the compiler to store the full definition of those classes in \texttt{Example.o}. Now when linker can resolve the types referenced in \texttt{main.o} and compilation is successful.
\end{block}
\end{frame}
% -----------------------------------------------------
\begin{frame}[t,fragile]
\frametitle{Template (Generic) Classes}
\begin{itemize}
\item In A3, you need to implement the generic class Seq2D<T>
\item Since you know ahead of time that you will only need two type instances of the Seq2D class (LanduseT and int), {\bf you should use should use explicit instantiation}
\item {\bf HINT for A3:} the last two lines of your Seq2D.cpp file should be:
\end{itemize}
\begin{lstlisting}
template class Seq2D<LanduseT>;
template class Seq2D<int>;
\end{lstlisting}
\end{frame}
% -----------------------------------------------------
\section{\texttt{typedef}}
% -----------------------------------------------------
% -----------------------------------------------------
\begin{frame}[t,fragile]
\frametitle{The \texttt{typedef} Keyword}
\begin{itemize}
\item \texttt{typedef} is used to create type aliases in \texttt{C++}
\item Usage:
\item[] \qquad \texttt{typedef <known type> <new type alias>}
\item For example:
\end{itemize}
\begin{lstlisting}
// nat as alias for unsigned int
typedef unsigned int nat
// real as alias for double
typedef double real
// myIntClass as alias for MyClass<int>
typedef MyClass<int> myIntClass
\end{lstlisting}
\end{frame}
% -----------------------------------------------------
\begin{frame}[t,fragile]
\frametitle{Using \texttt{typedef}'d Types}
\begin{itemize}
\item Once \texttt{typedef}'s have been made, you can use them like any other type
\item For example, with the \texttt{typedef}'s from the previous slide, we could then write:
\end{itemize}
\begin{lstlisting}
nat n = 0;
real r = 1.50;
// assuming MyClass(T) constructor exists:
myIntClass mic(5);
\end{lstlisting}
\end{frame}
% -----------------------------------------------------
\begin{frame}[t]
\frametitle{\texttt{typedef} Hint}
{\bf HINT for A3:}
\begin{itemize}
\item The LanduseMap and DEM modules will just be \texttt{typedef}'s in the LanduseMap.h and DEM.h files
\item These module don't need .cpp files -- no further implementation is required (the implementation is all done in Seq2D.cpp!)
\end{itemize}
\end{frame}
\end{document}
File deleted
File deleted
public class MVCPatternDemo {
private static Student retriveStudentFromDatabase(){
Student student = new Student();
student.setName("Robert");
student.setStudentNumber(10);
return student;
}
public static void main(String[] args) {
//fetch student record based on his Student Number from the database
Student model = retriveStudentFromDatabase();
//Create a view : to write student details on console
StudentView view = new StudentView();
StudentController controller = new StudentController(model, view);
controller.updateView();
//update model data
controller.setStudentName("John");
controller.setStudentNumber(11);
controller.updateView();
}
}
public class Student {
private String name;
private int number;
public int getStudentNumber() {
return number;
}
public void setStudentNumber(int number) {
this.number = number;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class StudentController {
private Student model;
private StudentView view;
public StudentController(Student model, StudentView view){
this.model = model;
this.view = view;
}
public void setStudentName(String name){
model.setName(name);
}
public String getStudentName(){
return model.getName();
}
public void setStudentNumber(int number){
model.setStudentNumber(number);
}
public int getStudentstudentNumber(){
return model.getStudentNumber();
}
public void updateView(){
view.printStudentDetails(model.getName(), model.getStudentNumber());
}
}
public class StudentView {
public void printStudentDetails(String studentName, int studentNumber){
System.out.println("---Student---");
System.out.println("Name: " + studentName);
System.out.println("Student Number: " + studentNumber);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment