"### EXERCICE 15: Add a method to give away the double value\n",
"\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",
"Add in the struct a constant method `DoubleValue()` which computes the approximation 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 4](/notebooks/2-ObjectProgramming/2b-TP.ipynb)
%% Cell type:markdown id: tags:
<h1>Table of contents<spanclass="tocSkip"></span></h1>
<divclass="toc"><ulclass="toc-item"><li><span><ahref="#EXERCICE-14:-transform-compute_power_of_2_approx()-into-a-PowerOfTwoApprox-method"data-toc-modified-id="EXERCICE-14:-transform-compute_power_of_2_approx()-into-a-PowerOfTwoApprox-method-1">EXERCICE 14: transform <code>compute_power_of_2_approx()</code> into a <code>PowerOfTwoApprox</code> method</a></span></li><li><span><ahref="#EXERCICE-15:-Add-a-method-to-give-away-the-double-value"data-toc-modified-id="EXERCICE-15:-Add-a-method-to-give-away-the-double-value-2">EXERCICE 15: Add a method to give away the double value</a></span></li></ul></div>
%% Cell type:markdown id: tags:
### EXERCICE 14: 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 15: Add a method to give away the double value
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()`.
Add in the struct a constant method `DoubleValue()` which computes the approximation value from both data attributes and returns it; use it in `display_power_of_2_approx()`.
Output should remain unchanged after this modification.
_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)_