Skip to content

Commit

Permalink
Add Unit 9
Browse files Browse the repository at this point in the history
  • Loading branch information
Kayzels committed Nov 9, 2022
1 parent d3d69ba commit 4a5f0d1
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
1 change: 1 addition & 0 deletions notes.tex
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@
\addfile{units/unit06/unit06.tex}
\addfile{units/unit07/unit07.tex}
\addfile{units/unit08/unit08.tex}
\addfile{units/unit09/unit09.tex}
\addfile{units/unit10/unit10.tex}
\end{document}
57 changes: 57 additions & 0 deletions units/unit09/unit09.tex
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
\providecommand{\main}{../..}
\documentclass[\main/notes.tex]{subfiles}

\begin{document}
\setcounter{chapter}{8}
\chapter{Inheritance}
\begin{definition}{Inheritance}
The process by a which a new class (a \concept{derived class} or \concept{child class}) is created from another class (a \concept{base class} or \concept{parent class}).
\end{definition}
\begin{sidenote}{Inherited Members}
A derived class has all the member variables and ordinary member functions of the base class. If changing the definition of a member function, it needs to be included in the derived class definition.
\end{sidenote}
\begin{definition}{Constructors in Derived Classes}
A constructor from the base class is \emph{not} inherited by the derived class, but can still be called as part of the derived class constructor.
\begin{minted}[bgcolor=definitionCode]{cpp}
DerivedClass::DerivedClass(arguments) : BaseClass(arguments)
\end{minted}
If a call to a constructor is not included, the default constructor of the base class will automaticallt be called when the derived class constructor is called.
\end{definition}
\begin{sidenote}{Be careful using private member variables from the base class!}
Private member variables of the base class can only be accessed within the definition of a member function of the base class.

While a derived class has the member variable, this cannot be directly accessed using member functions of the derived class.

Instead, getters and setters should be used to access the variable. Also, the \mintinline{cpp}{protected} qualifer can be used instead of the \mintinline{cpp}{private} qualifer, to make variables accessible to derived classes.
\end{sidenote}
\begin{sidenote}{The \texttt{protected} qualifier}
Allows derived classes to access variables from a base class, but makes the variables private (inaccessible) to any function that is not part of the derived class.
\end{sidenote}
\begin{definition}{Qualifiers Access}
\begin{center}
\begin{tblr}{c|ccc}
& Base Class & Derived Class & Other Functions\\
\midrule
\texttt{public} & Yes & Yes & Yes\\
\texttt{private} & Yes & No & No\\
\texttt{protected} & Yes & Yes & No
\end{tblr}
\end{center}
\end{definition}
\begin{sidenote}{Redefining vs Overloading}
\begin{description}
\item[Redefining] The new function definition has the same number and types of parameters.
\item[Overloading] The function has a different number of parameters, or a parameter of a different type.
\end{description}
\end{sidenote}
\begin{sidenote}{Accessing a redefined base function}
If the function has been redefined in a derived class, it is still possible to access the original function. To do that:
\begin{minted}[bgcolor=noteCode]{cpp}
Object object;
DerivedObject derived;

derived.Object::baseClassFunction();
\end{minted}
\end{sidenote}
\rulechapterend
\end{document}

0 comments on commit 4a5f0d1

Please sign in to comment.