Skip to content
Snippets Groups Projects
Commit 993a4b7c authored by Georgiana Mania's avatar Georgiana Mania
Browse files

add cpp and python pictorams to signal the implementation

parent faf40b6f
No related branches found
No related tags found
1 merge request!52Draft: Programming paradigms update bases on students' questions
......@@ -139,7 +139,7 @@ classDiagram
:::fragment
class definition
class definition ![](static/cpp_logo.png)
```cpp
class Book {
public:
......@@ -157,6 +157,7 @@ public:
::::
:::{.notes}
UML = Unified Modeling Language
......@@ -175,7 +176,7 @@ An specific instance of a class used to store the **state** (concrete variables
::: {.column width="50%"}
class definition
class definition ![](static/cpp_logo.png)
```cpp
class Book {
......@@ -193,7 +194,7 @@ public:
::: {.fragment}
create an object of a class
create an object of a class ![](static/cpp_logo.png)
```cpp
// instantiate class
......@@ -228,7 +229,7 @@ myBookObj.get_page_count();
::: {.column width="48%"}
C++
![](static/cpp_logo.png)
```cpp
class Book {
public:
......@@ -247,7 +248,7 @@ public:
::: {.column width="52%"}
Python
![](static/python_logo.png)
```python
class Book:
def __init__(self):
......@@ -270,7 +271,7 @@ class Book:
::: {.column width="47%"}
C++ class example
Class example ![](static/cpp_logo.png)
```cpp
class Book {
public:
......@@ -296,7 +297,7 @@ book.get_page_count();
::: {.column width="53%"}
::: {.fragment}
Python class example
Class example ![](static/python_logo.png)
```python
class Book:
......@@ -364,6 +365,7 @@ Used to increase code **reusability** and avoid code **duplication**
::: {.column width="48%"}
Define a book class ![](static/python_logo.png)
```python
class Book:
def __init__(self, pages):
......@@ -380,6 +382,7 @@ class Book:
::: {.fragment}
Define an EBook class ![](static/python_logo.png)
```python
class EBook(Book):
def __init__(self, pages, link):
......@@ -399,7 +402,7 @@ class EBook(Book):
::: {.fragment}
Use an EBook object
Use an EBook object ![](static/python_logo.png)
```python
book_obj = EBook(100, "www.link-to-book.com")
......@@ -467,6 +470,7 @@ Used to **hide complexity** and increase **code correctness**.
::: {.column width="50%"}
Define a Book class ![](static/cpp_logo.png)
```cpp
class Book {
public:
......@@ -489,6 +493,7 @@ private:
:::fragment
Use a Book object ![](static/cpp_logo.png)
```cpp
// instantiate a Book object
auto book = Book{100};
......@@ -507,7 +512,7 @@ book.page_count = 50;
:::
::::
<br/>
:::fragment
::: {.smaller}
......@@ -525,7 +530,7 @@ Hide unwanted information and expose only essential behavior.
:::fragment
::: {.smaller}
Header file (book.hpp)
Header file (book.hpp) ![](static/cpp_logo.png)
:::
```cpp
class Book {
......@@ -542,7 +547,7 @@ private:
::: {.column width="62%"}
:::fragment
::: {.smaller}
Implementation file (book.cpp)
Implementation file (book.cpp) ![](static/cpp_logo.png)
:::
```cpp
#include "book.hpp"
......@@ -652,7 +657,7 @@ Design object to **share** interface and **specialize** behaviour.
::::
## Static polymorphism with function overloading
## Static polymorphism with function overloading {.leftalign}
:::: {.columns}
......@@ -671,6 +676,8 @@ classDiagram
::: {.column width="60%"}
:::fragment
:::{.smaller}
```cpp
class Book {
public:
......@@ -689,22 +696,25 @@ private:
```
:::
:::
:::
::::
&NewLine;
:::fragment
:::{.smaller}
Use a book object ![](static/cpp_logo.png)
```cpp
auto book = Book{};
book.update_owners(); // call fn in line 3 => owners = 1
book.update_owners(3);// call fn in line 6 => owners = 4
```
:::
:::
## Static polymorphism with function templating
:::{.info .small}
Also called **parametric polymorphism** using template functions in C++.
Also called **parametric polymorphism** using template functions in ![](static/cpp_logo.png)
:::
:::: {.columns}
......@@ -787,8 +797,10 @@ classDiagram
::: {.column width="60%"}
:::fragment
:::{.smaller}
:::fragment
```cpp
#include <iostream>
class Book {
......@@ -806,13 +818,14 @@ public:
```
:::
:::
:::
::::
&NewLine;
:::fragment
:::{.info .smaller}
:::{.info .smaller .leftalign}
Book reference ![](static/cpp_logo.png)
```cpp
Book* book = new Book();
book->print_type(); // prints "book"
......@@ -823,7 +836,7 @@ eBook->print_type(); // prints "eBook"
:::
## Dynamic polymorphism without inheritance
## Dynamic polymorphism without inheritance {.leftalign}
:::{.info .small}
Straight-forward in dynamically-typed languages (e.g. Python)
......@@ -860,10 +873,15 @@ class Laptop:
```
:::
:::
&NewLine;
::::
:::fragment
:::{.smaller}
Use case ![](static/python_logo.png)
:::
```python
book = Book(..)
laptop = Laptop(..)
......@@ -872,7 +890,7 @@ for x in (book, laptop):
```
:::
## Hands-on Session! {background-color=var(--dark-bg-color) .leftalign}
## Hands-on Session! ![](static/python_logo.png) {background-color=var(--dark-bg-color) .leftalign}
1. Define a class `Person` with a constructor which initialises `name` and `address` attributes
2. Define a class `Student` that extends `Person` and sets a `university` attribute
......@@ -887,7 +905,7 @@ for x in (book, laptop):
* C++ allows the definition of templates for classes or functions
* Python supports generics through `typing` module, but it is not mandatory
## Templates in C++
## Templates in C++ ![](static/cpp_logo.png)
```cpp
// using C++20
......@@ -904,7 +922,7 @@ int main() {
}
```
## Generic programming in Python
## Generic programming in Python ![](static/python_logo.png)
```python
from typing import Sequence, TypeVar
......@@ -938,6 +956,10 @@ Example from [docs.python.org](https://docs.python.org/3.5/library/typing.html)
**Closures** allow functions to access and manipulate variables that are defined outside their scope
:::{.smaller .leftalign}
Closure example ![](static/python_logo.png)
:::
```python
def f(x):
return lambda y: x + y
......@@ -947,7 +969,6 @@ h = g(2)
print(h) # what is printed here?
```
<br/>
:::fragment
......@@ -972,6 +993,9 @@ Make sure the students know what a lambda function is.
:::
:::fragment
:::{.smaller .leftalign}
Print callback example ![](static/python_logo.png)
:::
```python
def g():
print("Function g")
......@@ -989,20 +1013,21 @@ f(g)
**Pure** functions have no side effects
:::fragment
:::: {.columns}
::: {.column width="50%"}
Pure function ![](static/python_logo.png)
```python
# pure function
def f(x):
return x * x
```
:::
::: {.column width="50%"}
Impure function ![](static/python_logo.png)
```python
# impure function
def g(x):
print(x)
```
......@@ -1021,7 +1046,7 @@ def g(x):
An example of how to use immutable data structures can be found [here](#imm-backup).
:::
## Map, filter, reduce {.leftalign}
## Map, filter, reduce ![](static/python_logo.png) {.leftalign}
:::fragment
......@@ -1062,7 +1087,7 @@ sum_items=reduce(lambda x, y : x + y, items, 0) # 0 is initializer value
```
:::
## Hands-on Session! {background-color=var(--dark-bg-color) .leftalign}
## Hands-on Session! ![](static/python_logo.png) {background-color=var(--dark-bg-color) .leftalign}
Write a higher-order function using map/filter/reduce which returns the product of the squares of the positive values from the input array
......@@ -1097,6 +1122,7 @@ the result should be 722534400
A **lambda function** is an anonymous function defined in-place.
Lambda function ![](static/python_logo.png)
```python
lambda x, y: x + y
```
......@@ -1109,7 +1135,7 @@ It can be :
## Immutability in large data objects {.leftalign #imm-backup}
When using [xarray.Dataset](https://docs.xarray.dev/en/stable/user-guide/data-structures.html#dataset) make sure your function does not change the input data
When using [xarray.Dataset](https://docs.xarray.dev/en/stable/user-guide/data-structures.html#dataset) make sure your function does not change the input data ![](static/python_logo.png)
```python
def convert_wind_from_uv_to_magnitude_and_direction(ds):
......
lectures/programming-paradigms/static/cpp_logo.png

5.72 KiB

lectures/programming-paradigms/static/python_logo.png

5.36 KiB

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