Commit 334028a5 authored by GILLES Sebastien's avatar GILLES Sebastien
Browse files

TP for object programming in progress. Will have to go back and fiddle with...

TP for object programming in progress. Will have to go back and fiddle with outputs from previous exercices.
parent 8cc12de3
......@@ -20,7 +20,7 @@ set(CMAKE_C_COMPILER clang)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_EXTENSIONS OFF)
project(GettingStartedWithModernCppTP)
project(GettingStartedWithModernCpp_TP_Procedural)
add_cxx_compiler_flag("-Weverything") # all warnings for clang
......
......@@ -12,11 +12,16 @@
"metadata": {},
"source": [
"* [Introduction to the concept of object](/notebooks/2-ObjectProgramming/1-Introduction.ipynb)\n",
" * [TP 3](/notebooks/2-ObjectProgramming/1b-TP.ipynb)\n",
"* [Member functions](/notebooks/2-ObjectProgramming/2-Member-functions.ipynb)\n",
" * [TP 4](/notebooks/2-ObjectProgramming/2b-TP.ipynb)\n",
"* [Base constructors and destructor](/notebooks/2-ObjectProgramming/3-constructors-destructor.ipynb)\n",
" * [TP 5](/notebooks/2-ObjectProgramming/3b-TP.ipynb)\n",
"* [Encapsulation](/notebooks/2-ObjectProgramming/4-encapsulation.ipynb)\n",
" * [TP 6](/notebooks/2-ObjectProgramming/4b-TP.ipynb)\n",
"* [Static attributes](/notebooks/2-ObjectProgramming/5-static.ipynb)\n",
"* [Inheritance and polymorphism](/notebooks/2-ObjectProgramming/6-inheritance.ipynb)"
"* [Inheritance and polymorphism](/notebooks/2-ObjectProgramming/6-inheritance.ipynb)\n",
" * [TP 7](/notebooks/2-ObjectProgramming/6b-TP.ipynb)"
]
},
{
......
%% Cell type:markdown id: tags:
# [Getting started in C++](/) - [Object programming](/notebooks/2-ObjectProgramming/0-main.ipynb)
%% Cell type:markdown id: tags:
* [Introduction to the concept of object](/notebooks/2-ObjectProgramming/1-Introduction.ipynb)
* [TP 3](/notebooks/2-ObjectProgramming/1b-TP.ipynb)
* [Member functions](/notebooks/2-ObjectProgramming/2-Member-functions.ipynb)
* [TP 4](/notebooks/2-ObjectProgramming/2b-TP.ipynb)
* [Base constructors and destructor](/notebooks/2-ObjectProgramming/3-constructors-destructor.ipynb)
* [TP 5](/notebooks/2-ObjectProgramming/3b-TP.ipynb)
* [Encapsulation](/notebooks/2-ObjectProgramming/4-encapsulation.ipynb)
* [TP 6](/notebooks/2-ObjectProgramming/4b-TP.ipynb)
* [Static attributes](/notebooks/2-ObjectProgramming/5-static.ipynb)
* [Inheritance and polymorphism](/notebooks/2-ObjectProgramming/6-inheritance.ipynb)
* [TP 7](/notebooks/2-ObjectProgramming/6b-TP.ipynb)
%% Cell type:markdown id: tags:
© _CNRS 2016_ - _Inria 2018_
_This notebook is an adaptation of a lecture prepared and redacted by David Chamont (CNRS) under the terms of the licence [Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0)](http://creativecommons.org/licenses/by-nc-sa/4.0/)_
_The present version has been redacted by Sébastien Gilles and Vincent Rouvreau (Inria)_
......
......@@ -14,42 +14,47 @@
},
"source": [
"<h1>Table of contents<span class=\"tocSkip\"></span></h1>\n",
"<div class=\"toc\"><ul class=\"toc-item\"><li><span><a href=\"#EXERCICE-1:-introduce-PowerOfTwoApprox-structure\" data-toc-modified-id=\"EXERCICE-1:-introduce-PowerOfTwoApprox-structure-1\">EXERCICE 1: introduce <code>PowerOfTwoApprox</code> structure</a></span></li></ul></div>"
"<div class=\"toc\"><ul class=\"toc-item\"><li><span><a href=\"#Introduction\" data-toc-modified-id=\"Introduction-1\">Introduction</a></span></li><li><span><a href=\"#EXERCICE-1:-introduce-PowerOfTwoApprox-structure\" data-toc-modified-id=\"EXERCICE-1:-introduce-PowerOfTwoApprox-structure-2\">EXERCICE 1: introduce <code>PowerOfTwoApprox</code> structure</a></span></li></ul></div>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Introduction\n",
"\n",
"[This notebook](/notebooks/TP/HowTo.ipynb) explains very briefly your options to run the TP.\n",
"\n",
"We will start from the solution to exercice 10 of the procedural programming TP. (we let aside pointer functions and writing into an output file done in exercices 11 and 12). `initial_file.cpp` in the TP directory is a copy to the solution of this exercice (symbolic link wouldn't have worked with Docker).\n",
"\n",
"\n",
"### EXERCICE 1: introduce `PowerOfTwoApprox` structure\n",
"\n",
"We will tackle the same problem with a dedicated `struct` called `PowerOfTwoApprox` which will be used to represent a real by two integers: the numerator and the exponent to the power of 2 used in the denominator.\n",
"\n",
"Create this structure and use it throughout the program.\n",
"\n",
"**NOTE:** The initial file given in the TP section is the solution of the exercice 10 (we drop the extension to write into file).\n",
"Create this structure and use it throughout the program (`compute_power_of_2_approx()` signature should for instance be modified accordingly).\n",
"\n",
"The expected output is unchanged from the initial file:\n",
"\n",
"````\n",
"0.65 ~ 3 / 2^2 (0.75) [error = 15 % for 2 bits] \n",
"0.65 ~ 10 / 2^4 (0.625) [error = 4 % for 4 bits] \n",
"0.65 ~ 42 / 2^6 (0.65625) [error = 1 % for 6 bits] \n",
"0.65 ~ 166 / 2^8 (0.648438) [error = 0 % for 8 bits] \n",
"\n",
"0.35 ~ 3 / 2^3 (0.375) [error = 7 % for 2 bits] \n",
"0.35 ~ 11 / 2^5 (0.34375) [error = 2 % for 4 bits] \n",
"0.35 ~ 45 / 2^7 (0.351562) [error = 0 % for 6 bits] \n",
"0.35 ~ 179 / 2^9 (0.349609) [error = 0 % for 8 bits] \n",
"\n",
"1 bits : 0.65 * 3515 + 0.35 * 4832 = 3976 ~ 2965 [error = 254 / 1000] \n",
"2 bits : 0.65 * 3515 + 0.35 * 4832 = 3976 ~ 4448 [error = 119 / 1000] \n",
"3 bits : 0.65 * 3515 + 0.35 * 4832 = 3976 ~ 4008 [error = 8 / 1000] \n",
"4 bits : 0.65 * 3515 + 0.35 * 4832 = 3976 ~ 3857 [error = 30 / 1000] \n",
"5 bits : 0.65 * 3515 + 0.35 * 4832 = 3976 ~ 3967 [error = 2 / 1000] \n",
"6 bits : 0.65 * 3515 + 0.35 * 4832 = 3976 ~ 4004 [error = 7 / 1000] \n",
"7 bits : 0.65 * 3515 + 0.35 * 4832 = 3976 ~ 3977 [error = 0 / 1000] \n",
"8 bits : 0.65 * 3515 + 0.35 * 4832 = 3976 ~ 3968 [error = 2 / 1000] \n",
"[With 2 bits]: 0.65 ~ 3 / 2^2 (0.75) [error = 15/100] \n",
"[With 4 bits]: 0.65 ~ 10 / 2^4 (0.625) [error = 4/100] \n",
"[With 6 bits]: 0.65 ~ 42 / 2^6 (0.65625) [error = 1/100] \n",
"[With 8 bits]: 0.65 ~ 166 / 2^8 (0.648438) [error = 0/100] \n",
" \n",
"[With 2 bits]: 0.35 ~ 3 / 2^3 (0.375) [error = 7/100] \n",
"[With 4 bits]: 0.35 ~ 11 / 2^5 (0.34375) [error = 2/100] \n",
"[With 6 bits]: 0.35 ~ 45 / 2^7 (0.351562) [error = 0/100] \n",
"[With 8 bits]: 0.35 ~ 179 / 2^9 (0.349609) [error = 0/100] \n",
" \n",
"[With 1 bits]: 0.65 * 3515 + 0.35 * 4832 = 3976 ~ 2965 [error = 254/1000] \n",
"[With 2 bits]: 0.65 * 3515 + 0.35 * 4832 = 3976 ~ 4448 [error = 119/1000] \n",
"[With 3 bits]: 0.65 * 3515 + 0.35 * 4832 = 3976 ~ 4008 [error = 8/1000] \n",
"[With 4 bits]: 0.65 * 3515 + 0.35 * 4832 = 3976 ~ 3857 [error = 30/1000] \n",
"[With 5 bits]: 0.65 * 3515 + 0.35 * 4832 = 3976 ~ 3967 [error = 2/1000] \n",
"[With 6 bits]: 0.65 * 3515 + 0.35 * 4832 = 3976 ~ 4004 [error = 7/1000] \n",
"[With 7 bits]: 0.65 * 3515 + 0.35 * 4832 = 3976 ~ 3977 [error = 0/1000] \n",
"[With 8 bits]: 0.65 * 3515 + 0.35 * 4832 = 3976 ~ 3968 [error = 2/1000] \n",
"````"
]
},
......
%% Cell type:markdown id: tags:
# [Getting started in C++](/) - [Object programming](/notebooks/2-ObjectProgramming/0-main.ipynb) - [TP 1](/notebooks/2-ObjectProgramming/1b-TP.ipynb)
%% Cell type:markdown id: tags:
<h1>Table of contents<span class="tocSkip"></span></h1>
<div class="toc"><ul class="toc-item"><li><span><a href="#EXERCICE-1:-introduce-PowerOfTwoApprox-structure" data-toc-modified-id="EXERCICE-1:-introduce-PowerOfTwoApprox-structure-1">EXERCICE 1: introduce <code>PowerOfTwoApprox</code> structure</a></span></li></ul></div>
<div class="toc"><ul class="toc-item"><li><span><a href="#Introduction" data-toc-modified-id="Introduction-1">Introduction</a></span></li><li><span><a href="#EXERCICE-1:-introduce-PowerOfTwoApprox-structure" data-toc-modified-id="EXERCICE-1:-introduce-PowerOfTwoApprox-structure-2">EXERCICE 1: introduce <code>PowerOfTwoApprox</code> structure</a></span></li></ul></div>
%% Cell type:markdown id: tags:
### Introduction
[This notebook](/notebooks/TP/HowTo.ipynb) explains very briefly your options to run the TP.
We will start from the solution to exercice 10 of the procedural programming TP. (we let aside pointer functions and writing into an output file done in exercices 11 and 12). `initial_file.cpp` in the TP directory is a copy to the solution of this exercice (symbolic link wouldn't have worked with Docker).
### EXERCICE 1: introduce `PowerOfTwoApprox` structure
We will tackle the same problem with a dedicated `struct` called `PowerOfTwoApprox` which will be used to represent a real by two integers: the numerator and the exponent to the power of 2 used in the denominator.
Create this structure and use it throughout the program.
**NOTE:** The initial file given in the TP section is the solution of the exercice 10 (we drop the extension to write into file).
Create this structure and use it throughout the program (`compute_power_of_2_approx()` signature should for instance be modified accordingly).
The expected output is unchanged from the initial file:
````
0.65 ~ 3 / 2^2 (0.75) [error = 15 % for 2 bits]
0.65 ~ 10 / 2^4 (0.625) [error = 4 % for 4 bits]
0.65 ~ 42 / 2^6 (0.65625) [error = 1 % for 6 bits]
0.65 ~ 166 / 2^8 (0.648438) [error = 0 % for 8 bits]
0.35 ~ 3 / 2^3 (0.375) [error = 7 % for 2 bits]
0.35 ~ 11 / 2^5 (0.34375) [error = 2 % for 4 bits]
0.35 ~ 45 / 2^7 (0.351562) [error = 0 % for 6 bits]
0.35 ~ 179 / 2^9 (0.349609) [error = 0 % for 8 bits]
1 bits : 0.65 * 3515 + 0.35 * 4832 = 3976 ~ 2965 [error = 254 / 1000]
2 bits : 0.65 * 3515 + 0.35 * 4832 = 3976 ~ 4448 [error = 119 / 1000]
3 bits : 0.65 * 3515 + 0.35 * 4832 = 3976 ~ 4008 [error = 8 / 1000]
4 bits : 0.65 * 3515 + 0.35 * 4832 = 3976 ~ 3857 [error = 30 / 1000]
5 bits : 0.65 * 3515 + 0.35 * 4832 = 3976 ~ 3967 [error = 2 / 1000]
6 bits : 0.65 * 3515 + 0.35 * 4832 = 3976 ~ 4004 [error = 7 / 1000]
7 bits : 0.65 * 3515 + 0.35 * 4832 = 3976 ~ 3977 [error = 0 / 1000]
8 bits : 0.65 * 3515 + 0.35 * 4832 = 3976 ~ 3968 [error = 2 / 1000]
[With 2 bits]: 0.65 ~ 3 / 2^2 (0.75) [error = 15/100]
[With 4 bits]: 0.65 ~ 10 / 2^4 (0.625) [error = 4/100]
[With 6 bits]: 0.65 ~ 42 / 2^6 (0.65625) [error = 1/100]
[With 8 bits]: 0.65 ~ 166 / 2^8 (0.648438) [error = 0/100]
[With 2 bits]: 0.35 ~ 3 / 2^3 (0.375) [error = 7/100]
[With 4 bits]: 0.35 ~ 11 / 2^5 (0.34375) [error = 2/100]
[With 6 bits]: 0.35 ~ 45 / 2^7 (0.351562) [error = 0/100]
[With 8 bits]: 0.35 ~ 179 / 2^9 (0.349609) [error = 0/100]
[With 1 bits]: 0.65 * 3515 + 0.35 * 4832 = 3976 ~ 2965 [error = 254/1000]
[With 2 bits]: 0.65 * 3515 + 0.35 * 4832 = 3976 ~ 4448 [error = 119/1000]
[With 3 bits]: 0.65 * 3515 + 0.35 * 4832 = 3976 ~ 4008 [error = 8/1000]
[With 4 bits]: 0.65 * 3515 + 0.35 * 4832 = 3976 ~ 3857 [error = 30/1000]
[With 5 bits]: 0.65 * 3515 + 0.35 * 4832 = 3976 ~ 3967 [error = 2/1000]
[With 6 bits]: 0.65 * 3515 + 0.35 * 4832 = 3976 ~ 4004 [error = 7/1000]
[With 7 bits]: 0.65 * 3515 + 0.35 * 4832 = 3976 ~ 3977 [error = 0/1000]
[With 8 bits]: 0.65 * 3515 + 0.35 * 4832 = 3976 ~ 3968 [error = 2/1000]
````
%% Cell type:markdown id: tags:
© _CNRS 2016_ - _Inria 2018_
_This notebook is an adaptation of a lecture prepared and redacted by David Chamont (CNRS) under the terms of the licence [Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0)](http://creativecommons.org/licenses/by-nc-sa/4.0/)_
_The present version has been redacted by Sébastien Gilles and Vincent Rouvreau (Inria)_
......
......@@ -4,7 +4,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"# [Getting started in C++](/) - [Object programming](/notebooks/2-ObjectProgramming/0-main.ipynb) - [TP 2](/notebooks/2-ObjectProgramming/2b-TP.ipynb)"
"# [Getting started in C++](/) - [Object programming](/notebooks/2-ObjectProgramming/0-main.ipynb) - [TP 4](/notebooks/2-ObjectProgramming/2b-TP.ipynb)"
]
},
{
......@@ -36,7 +36,7 @@
"source": [
"### EXERCICE 3: Add a method to give away the double value\n",
"\n",
"Add in the struct a constant method `GetDoubleValue()` which computes the double value from both data attributes and returns it; use it in `display_power_of_2_approx()`.\n",
"Add in the struct a constant method `DoubleValue()` which computes the double value from both data attributes and returns it; use it in `display_power_of_2_approx()`.\n",
"\n",
"Output should remain unchanged after this modification."
]
......
%% Cell type:markdown id: tags:
# [Getting started in C++](/) - [Object programming](/notebooks/2-ObjectProgramming/0-main.ipynb) - [TP 2](/notebooks/2-ObjectProgramming/2b-TP.ipynb)
# [Getting started in C++](/) - [Object programming](/notebooks/2-ObjectProgramming/0-main.ipynb) - [TP 4](/notebooks/2-ObjectProgramming/2b-TP.ipynb)
%% Cell type:markdown id: tags:
<h1>Table of contents<span class="tocSkip"></span></h1>
<div class="toc"><ul class="toc-item"><li><span><a href="#EXERCICE-2:-transform-compute_power_of_2_approx()-into-a-PowerOfTwoApprox-method" data-toc-modified-id="EXERCICE-2:-transform-compute_power_of_2_approx()-into-a-PowerOfTwoApprox-method-1">EXERCICE 2: transform <code>compute_power_of_2_approx()</code> into a <code>PowerOfTwoApprox</code> method</a></span></li><li><span><a href="#EXERCICE-3:-Add-a-method-to-give-away-the-double-value" data-toc-modified-id="EXERCICE-3:-Add-a-method-to-give-away-the-double-value-2">EXERCICE 3: Add a method to give away the double value</a></span></li></ul></div>
%% Cell type:markdown id: tags:
### EXERCICE 2: transform `compute_power_of_2_approx()` into a `PowerOfTwoApprox` method
What we did in exercice 1 is a bit clunky: the object is introduced but we keep spending time fetching the data attribute to act upon them (even initialization to 0 is done manually several times).
We'll streamline this a bit by making some of the functions methods of the `PowerOfTwoApprox` struct; starting with `compute_power_of_2_approx()` that will become a method named `Compute()`. This method will change the internal data attributes rather than changing an output argument.
Output should remain unchanged after this modification.
%% Cell type:markdown id: tags:
### EXERCICE 3: Add a method to give away the double value
Add in the struct a constant method `GetDoubleValue()` which computes the double value from both data attributes and returns it; use it in `display_power_of_2_approx()`.
Add in the struct a constant method `DoubleValue()` which computes the double value from both data attributes and returns it; use it in `display_power_of_2_approx()`.
Output should remain unchanged after this modification.
%% Cell type:markdown id: tags:
© _CNRS 2016_ - _Inria 2018_
_This notebook is an adaptation of a lecture prepared and redacted by David Chamont (CNRS) under the terms of the licence [Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0)](http://creativecommons.org/licenses/by-nc-sa/4.0/)_
_The present version has been redacted by Sébastien Gilles and Vincent Rouvreau (Inria)_
......
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# [Getting started in C++](/) - [Object programming](/notebooks/2-ObjectProgramming/0-main.ipynb) - [TP 5](/notebooks/2-ObjectProgramming/3b-TP.ipynb)"
]
},
{
"cell_type": "markdown",
"metadata": {
"toc": true
},
"source": [
"<h1>Table of contents<span class=\"tocSkip\"></span></h1>\n",
"<div class=\"toc\"><ul class=\"toc-item\"><li><span><a href=\"#EXERCICE-4:-change-interface-of-PowerOfTwoApprox-struct\" data-toc-modified-id=\"EXERCICE-4:-change-interface-of-PowerOfTwoApprox-struct-1\">EXERCICE 4: change interface of <code>PowerOfTwoApprox</code> struct</a></span></li></ul></div>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### EXERCICE 4: change interface of `PowerOfTwoApprox` struct\n",
"\n",
"* Transform `Compute()` into a constructor.\n",
"* Assign a default value of `0` to data attributes.\n",
"\n",
"Expected output is the same as previously."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"© _CNRS 2016_ - _Inria 2018_ \n",
"_This notebook is an adaptation of a lecture prepared and redacted by David Chamont (CNRS) under the terms of the licence [Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0)](http://creativecommons.org/licenses/by-nc-sa/4.0/)_ \n",
"_The present version has been redacted by Sébastien Gilles and Vincent Rouvreau (Inria)_"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "C++17",
"language": "C++17",
"name": "xeus-cling-cpp17"
},
"language_info": {
"codemirror_mode": "text/x-c++src",
"file_extension": ".cpp",
"mimetype": "text/x-c++src",
"name": "c++",
"version": "-std=c++17"
},
"latex_envs": {
"LaTeX_envs_menu_present": true,
"autoclose": false,
"autocomplete": true,
"bibliofile": "biblio.bib",
"cite_by": "apalike",
"current_citInitial": 1,
"eqLabelWithNumbers": true,
"eqNumInitial": 1,
"hotkeys": {
"equation": "Ctrl-E",
"itemize": "Ctrl-I"
},
"labels_anchors": false,
"latex_user_defs": false,
"report_style_numbering": false,
"user_envs_cfg": false
},
"toc": {
"base_numbering": 1,
"nav_menu": {},
"number_sections": false,
"sideBar": true,
"skip_h1_title": true,
"title_cell": "Table of contents",
"title_sidebar": "Contents",
"toc_cell": true,
"toc_position": {},
"toc_section_display": true,
"toc_window_display": true
}
},
"nbformat": 4,
"nbformat_minor": 2
}
%% Cell type:markdown id: tags:
# [Getting started in C++](/) - [Object programming](/notebooks/2-ObjectProgramming/0-main.ipynb) - [TP 5](/notebooks/2-ObjectProgramming/3b-TP.ipynb)
%% Cell type:markdown id: tags:
<h1>Table of contents<span class="tocSkip"></span></h1>
<div class="toc"><ul class="toc-item"><li><span><a href="#EXERCICE-4:-change-interface-of-PowerOfTwoApprox-struct" data-toc-modified-id="EXERCICE-4:-change-interface-of-PowerOfTwoApprox-struct-1">EXERCICE 4: change interface of <code>PowerOfTwoApprox</code> struct</a></span></li></ul></div>
%% Cell type:markdown id: tags:
### EXERCICE 4: change interface of `PowerOfTwoApprox` struct
* Transform `Compute()` into a constructor.
* Assign a default value of `0` to data attributes.
Expected output is the same as previously.
%% Cell type:markdown id: tags:
© _CNRS 2016_ - _Inria 2018_
_This notebook is an adaptation of a lecture prepared and redacted by David Chamont (CNRS) under the terms of the licence [Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0)](http://creativecommons.org/licenses/by-nc-sa/4.0/)_
_The present version has been redacted by Sébastien Gilles and Vincent Rouvreau (Inria)_
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# [Getting started in C++](/) - [Object programming](/notebooks/2-ObjectProgramming/0-main.ipynb) - [TP 6](/notebooks/2-ObjectProgramming/4b-TP.ipynb)"
]
},
{
"cell_type": "markdown",
"metadata": {
"toc": true
},
"source": [
"<h1>Table of contents<span class=\"tocSkip\"></span></h1>\n",
"<div class=\"toc\"><ul class=\"toc-item\"><li><span><a href=\"#EXERCICE-5:-transform-struct-PowerOfTwoApprox-into-a-class\" data-toc-modified-id=\"EXERCICE-5:-transform-struct-PowerOfTwoApprox-into-a-class-1\">EXERCICE 5: transform struct <code>PowerOfTwoApprox</code> into a class</a></span></li><li><span><a href=\"#EXERCICE-6:-transform-multiply()-into-a-method-Multiply()-of-PowerOfTwoApprox\" data-toc-modified-id=\"EXERCICE-6:-transform-multiply()-into-a-method-Multiply()-of-PowerOfTwoApprox-2\">EXERCICE 6: transform <code>multiply()</code> into a method <code>Multiply()</code> of <code>PowerOfTwoApprox</code></a></span></li><li><span><a href=\"#EXERCICE-7:-transform-display_power_of_2_approx()-into-a-class\" data-toc-modified-id=\"EXERCICE-7:-transform-display_power_of_2_approx()-into-a-class-3\">EXERCICE 7: transform <code>display_power_of_2_approx()</code> into a class</a></span></li><li><span><a href=\"#EXERCICE-8:-transform-display_sum()-into-a-class\" data-toc-modified-id=\"EXERCICE-8:-transform-display_sum()-into-a-class-4\">EXERCICE 8: transform <code>display_sum()</code> into a class</a></span></li><li><span><a href=\"#EXERCICE-9:-factorize-display-of-error\" data-toc-modified-id=\"EXERCICE-9:-factorize-display-of-error-5\">EXERCICE 9: factorize display of error</a></span></li></ul></div>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### EXERCICE 5: transform struct `PowerOfTwoApprox` into a class\n",
"\n",
"Make `PowerOfTwoApprox` into a class, with proper encapsulation:\n",
"\n",
"* Both data attributes should be made private.\n",
"* Constant accessors will therefore be needed (non-constant ones should not be required here)\n",
"\n",
"Expected output is the same as previously."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### EXERCICE 6: transform `multiply()` into a method `Multiply()` of `PowerOfTwoApprox`\n",
"\n",
"The method will take as argument only the integer coefficient.\n",
"\n",
"`display_sum()` will of course need also some light rewriting to accomodate that change.\n",
"\n",
"Expected output is the same as previously.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### EXERCICE 7: transform `display_power_of_2_approx()` into a class\n",
"\n",
"Create a class `TestDisplayPowerOfTwoApprox` which will be in charge of printing the display for the 0.65 and 0.35 values.\n",
"\n",
"Use two methods in this class:\n",
"\n",
"* A public method `Do()` which will call the test for 0.65 and 0.35; this method will take the number of bits as argument.\n",
"* A private method `Display()` which will provide the display for a given double value (and will therefore be called twice: once for 0.65 and once for 0.35).\n",
"\n",
"New main should look like:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"int main(int argc, char** argv)\n",
"{\n",
" static_cast<void>(argc); // to silence warning about unused argc - don't bother \n",
" static_cast<void>(argv); // to silence warning about unused argv - don't bother \n",
" \n",
" TestDisplayPowerOfTwoApprox test_display_approx;\n",
" \n",
" for (int Nbits = 2; Nbits <= 8; Nbits += 2)\n",
" test_display_approx.Do(Nbits); \n",
"\n",
" std::cout << std::endl;\n",
"\n",
" for (int Nbits = 1; Nbits <= 8; ++Nbits)\n",
" display_sum(Nbits, 0.65, 3515, 0.35, 4832);\n",
"\n",
" return EXIT_SUCCESS;\n",
"}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Output will be ordered differently:\n",
"\n",
"````\n",
"[With 2 bits]: 0.65 ~ 3 / 2^2 (0.75) [error = 15/100] \n",
"[With 2 bits]: 0.35 ~ 3 / 2^3 (0.375) [error = 7/100] \n",
"[With 4 bits]: 0.65 ~ 10 / 2^4 (0.625) [error = 4/100] \n",
"[With 4 bits]: 0.35 ~ 11 / 2^5 (0.34375) [error = 2/100] \n",
"[With 6 bits]: 0.65 ~ 42 / 2^6 (0.65625) [error = 1/100] \n",
"[With 6 bits]: 0.35 ~ 45 / 2^7 (0.351562) [error = 0/100] \n",
"[With 8 bits]: 0.65 ~ 166 / 2^8 (0.648438) [error = 0/100] \n",
"[With 8 bits]: 0.35 ~ 179 / 2^9 (0.349609) [error = 0/100] \n",
" \n",
"[With 1 bits]: 0.65 * 3515 + 0.35 * 4832 = 3976 ~ 2965 [error = 254/1000] \n",
"[With 2 bits]: 0.65 * 3515 + 0.35 * 4832 = 3976 ~ 4448 [error = 119/1000] \n",
"[With 3 bits]: 0.65 * 3515 + 0.35 * 4832 = 3976 ~ 4008 [error = 8/1000] \n",
"[With 4 bits]: 0.65 * 3515 + 0.35 * 4832 = 3976 ~ 3857 [error = 30/1000] \n",
"[With 5 bits]: 0.65 * 3515 + 0.35 * 4832 = 3976 ~ 3967 [error = 2/1000] \n",
"[With 6 bits]: 0.65 * 3515 + 0.35 * 4832 = 3976 ~ 4004 [error = 7/1000] \n",
"[With 7 bits]: 0.65 * 3515 + 0.35 * 4832 = 3976 ~ 3977 [error = 0/1000] \n",
"[With 8 bits]: 0.65 * 3515 + 0.35 * 4832 = 3976 ~ 3968 [error = 2/1000] \n",
"\n",
"````"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### EXERCICE 8: transform `display_sum()` into a class\n",
"\n",
"Likewise, create a class `TestDisplaySum` which will be in charge of printing the display for 0.65 * 3515 + 0.35 * 4832 with public method `Do()` and private method `Display()`."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### EXERCICE 9: factorize display of error\n",
"\n",
"Create the `error()` function that calculates and displays the error rate between a real number and its approximation, on an integer scale from 0 to a given maximum index. The input arguments are:\n",
"\n",
"* The number of bits used during the approximation\n",
"* The exact actual number\n",
"* The approximation of this number\n",
"* The maximum error index\n",
"* Optionally strings to enable keeping the exact same output as previously.\n",
"\n",
"This `error()` function should be called in both `Display()` methods."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"© _CNRS 2016_ - _Inria 2018_ \n",
"_This notebook is an adaptation of a lecture prepared and redacted by David Chamont (CNRS) under the terms of the licence [Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0)](http://creativecommons.org/licenses/by-nc-sa/4.0/)_ \n",
"_The present version has been redacted by Sébastien Gilles and Vincent Rouvreau (Inria)_"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "C++17",
"language": "C++17",
"name": "xeus-cling-cpp17"
},
"language_info": {
"codemirror_mode": "text/x-c++src",
"file_extension": ".cpp",
"mimetype": "text/x-c++src",
"name": "c++",
"version": "-std=c++17"
},
"latex_envs": {
"LaTeX_envs_menu_present": true,
"autoclose": false,
"autocomplete": true,
"bibliofile": "biblio.bib",
"cite_by": "apalike",
"current_citInitial": 1,
"eqLabelWithNumbers": true,
"eqNumInitial": 1,
"hotkeys": {
"equation": "Ctrl-E",
"itemize": "Ctrl-I"
},
"labels_anchors": false,
"latex_user_defs": false,
"report_style_numbering": false,
"user_envs_cfg": false
},
"toc": {
"base_numbering": 1,
"nav_menu": {},
"number_sections": false,
"sideBar": true,
"skip_h1_title": true,
"title_cell": "Table of contents",
"title_sidebar": "Contents",
"toc_cell": true,
"toc_position": {},
"toc_section_display": true,
"toc_window_display": true
}
},
"nbformat": 4,
"nbformat_minor": 2
}
%% Cell type:markdown id: tags:
# [Getting started in C++](/) - [Object programming](/notebooks/2-ObjectProgramming/0-main.ipynb) - [TP 6](/notebooks/2-ObjectProgramming/4b-TP.ipynb)
%% Cell type:markdown id: tags:
<h1>Table of contents<span class="tocSkip"></span></h1>
<div class="toc"><ul class="toc-item"><li><span><a href="#EXERCICE-5:-transform-struct-PowerOfTwoApprox-into-a-class" data-toc-modified-id="EXERCICE-5:-transform-struct-PowerOfTwoApprox-into-a-class-1">EXERCICE 5: transform struct <code>PowerOfTwoApprox</code> into a class</a></span></li><li><span><a href="#EXERCICE-6:-transform-multiply()-into-a-method-Multiply()-of-PowerOfTwoApprox" data-toc-modified-id="EXERCICE-6:-transform-multiply()-into-a-method-Multiply()-of-PowerOfTwoApprox-2">EXERCICE 6: transform <code>multiply()</code> into a method <code>Multiply()</code> of <code>PowerOfTwoApprox</code></a></span></li><li><span><a href="#EXERCICE-7:-transform-display_power_of_2_approx()-into-a-class" data-toc-modified-id="EXERCICE-7:-transform-display_power_of_2_approx()-into-a-class-3">EXERCICE 7: transform <code>display_power_of_2_approx()</code> into a class</a></span></li><li><span><a href="#EXERCICE-8:-transform-display_sum()-into-a-class" data-toc-modified-id="EXERCICE-8:-transform-display_sum()-into-a-class-4">EXERCICE 8: transform <code>display_sum()</code> into a class</a></span></li><li><span><a href="#EXERCICE-9:-factorize-display-of-error" data-toc-modified-id="EXERCICE-9:-factorize-display-of-error-5">EXERCICE 9: factorize display of error</a></span></li></ul></div>
%% Cell type:markdown id: tags:
### EXERCICE 5: transform struct `PowerOfTwoApprox` into a class
Make `PowerOfTwoApprox` into a class, with proper encapsulation:
* Both data attributes should be made private.
* Constant accessors will therefore be needed (non-constant ones should not be required here)
Expected output is the same as previously.
%% Cell type:markdown id: tags:
### EXERCICE 6: transform `multiply()` into a method `Multiply()` of `PowerOfTwoApprox`
The method will take as argument only the integer coefficient.
`display_sum()` will of course need also some light rewriting to accomodate that change.
Expected output is the same as previously.
%% Cell type:markdown id: tags:
### EXERCICE 7: transform `display_power_of_2_approx()` into a class
Create a class `TestDisplayPowerOfTwoApprox` which will be in charge of printing the display for the 0.65 and 0.35 values.
Use two methods in this class:
* A public method `Do()` which will call the test for 0.65 and 0.35; this method will take the number of bits as argument.
* A private method `Display()` which will provide the display for a given double value (and will therefore be called twice: once for 0.65 and once for 0.35).
New main should look like:
%% Cell type:code id: tags:
``` C++17
int main(int argc, char** argv)
{
static_cast<void>(argc); // to silence warning about unused argc - don't bother
static_cast<void>(argv); // to silence warning about unused argv - don't bother
TestDisplayPowerOfTwoApprox test_display_approx;
for (int Nbits = 2; Nbits <= 8; Nbits += 2)
test_display_approx.Do(Nbits);
std::cout << std::endl;
for (int Nbits = 1; Nbits <= 8; ++Nbits)
display_sum(Nbits, 0.65, 3515, 0.35, 4832);
return EXIT_SUCCESS;
}
```
%% Cell type:markdown id: tags:
Output will be ordered differently:
````
[With 2 bits]: 0.65 ~ 3 / 2^2 (0.75) [error = 15/100]
[With 2 bits]: 0.35 ~ 3 / 2^3 (0.375) [error = 7/100]
[With 4 bits]: 0.65 ~ 10 / 2^4 (0.625) [error = 4/100]
[With 4 bits]: 0.35 ~ 11 / 2^5 (0.34375) [error = 2/100]
[With 6 bits]: 0.65 ~ 42 / 2^6 (0.65625) [error = 1/100]
[With 6 bits]: 0.35 ~ 45 / 2^7 (0.351562) [error = 0/100]
[With 8 bits]: 0.65 ~ 166 / 2^8 (0.648438) [error = 0/100]
[With 8 bits]: 0.35 ~ 179 / 2^9 (0.349609) [error = 0/100]
[With 1 bits]: 0.65 * 3515 + 0.35 * 4832 = 3976 ~ 2965 [error = 254/1000]
[With 2 bits]: 0.65 * 3515 + 0.35 * 4832 = 3976 ~ 4448 [error = 119/1000]
[With 3 bits]: 0.65 * 3515 + 0.35 * 4832 = 3976 ~ 4008 [error = 8/1000]
[With 4 bits]: 0.65 * 3515 + 0.35 * 4832 = 3976 ~ 3857 [error = 30/1000]
[With 5 bits]: 0.65 * 3515 + 0.35 * 4832 = 3976 ~ 3967 [error = 2/1000]
[With 6 bits]: 0.65 * 3515 + 0.35 * 4832 = 3976 ~ 4004 [error = 7/1000]
[With 7 bits]: 0.65 * 3515 + 0.35 * 4832 = 3976 ~ 3977 [error = 0/1000]
[With 8 bits]: 0.65 * 3515 + 0.35 * 4832 = 3976 ~ 3968 [error = 2/1000]
````
%% Cell type:markdown id: tags:
### EXERCICE 8: transform `display_sum()` into a class
Likewise, create a class `TestDisplaySum` which will be in charge of printing the display for 0.65 * 3515 + 0.35 * 4832 with public method `Do()` and private method `Display()`.
%% Cell type:markdown id: tags:
### EXERCICE 9: factorize display of error
Create the `error()` function that calculates and displays the error rate between a real number and its approximation, on an integer scale from 0 to a given maximum index. The input arguments are:
* The number of bits used during the approximation
* The exact actual number
* The approximation of this number
* The maximum error index
* Optionally strings to enable keeping the exact same output as previously.
This `error()` function should be called in both `Display()` methods.
%% Cell type:markdown id: tags:
© _CNRS 2016_ - _Inria 2018_