Commit 98935baf authored by CHAMONT David's avatar CHAMONT David
Browse files

Upgrade videos, adn some coding style.

parent 4684ad1f
......@@ -88,7 +88,7 @@
}
},
"source": [
"## C++11: generalization of initialization by braces"
"## C++11: generalization of initialization by curly braces"
]
},
{
......@@ -198,7 +198,7 @@
}
},
"source": [
"#### Benefit: the use of braces prohibits automatic implicit narrowing of values."
"#### Benefit: the use of curly braces prohibits automatic implicit narrowing of values."
]
},
{
......
......@@ -776,23 +776,17 @@
},
"source": [
"#### When a class manage some dynamic resource, it is useful to add a *move constructor*\n",
"\n",
"```c++\n",
"class A\n",
" {\n",
"class A {\n",
" public :\n",
" A()\n",
" : m_data {new int[1000000]} {}\n",
" A( A const & other )\n",
" : m_data {new int[1000000]}\n",
" A() : m_data {new int[1000000]} {}\n",
" A( A const & other ) : m_data {new int[1000000]}\n",
" { std::copy(other.m_data,other.m_data+1000000,m_data) ; }\n",
" A( A && other )\n",
" : data_ {other.m_data}\n",
" { other.m_data = nullptr ; }\n",
" A( A && other ) : m_data {other.m_data} { other.m_data = 0 ; }\n",
" ~A() { delete [] m_data; }\n",
" private :\n",
" int * m_data ;\n",
" } ;\n",
"} ;\n",
"```"
]
},
......
......@@ -421,7 +421,7 @@
},
{
"cell_type": "code",
"execution_count": 20,
"execution_count": 1,
"metadata": {
"attributes": {
"classes": [
......@@ -436,13 +436,11 @@
"outputs": [],
"source": [
"class Y {\n",
" private:\n",
" Y() = default ; // Change access\n",
" public:\n",
" Y( Y & ) = default ; // Take a non-const reference\n",
" Y & operator=( Y const & ) = default ; // Declare as defaulted for documentation\n",
" Y & operator=( Y const & ) = default ; // Make it explicit\n",
" virtual ~Y() = default ; // Add virtual\n",
" protected:\n",
" virtual ~Y() = default ; // Change access and add virtual\n",
" Y() = default ; // Change access\n",
"} ; "
]
},
......@@ -514,9 +512,9 @@
"class B : public A\n",
" {\n",
" public :\n",
" void fct1 () override ; // OK\n",
" void ft2 ( int ) override ; // error: A::ft2 does not exist\n",
" void fct2 ( bool ) override ; // error: not the good types\n",
" void fct1() override ; // OK\n",
" void ft2( int ) override ; // error: A::ft2 does not exist\n",
" void fct2( bool ) override ; // error: not the good types\n",
" } ;"
]
},
......@@ -573,8 +571,8 @@
"outputs": [],
"source": [
"struct C final : public B {\n",
" void fct1 () override ; // OK\n",
" void fct3 ( bool ) override ; // error: B::fct3 is final\n",
" void fct1() override ; // OK\n",
" void fct3( bool ) override ; // error: B::fct3 is final\n",
"} ;"
]
},
......
......@@ -120,7 +120,7 @@
}
],
"source": [
"const int n = 0 ;\n",
"int const n {} ;\n",
"// ...\n",
"static_assert(n>0,\"Wrong table dimension t\\n\") ;\n",
"double values[n] ;"
......@@ -565,7 +565,7 @@
"\n",
"int main()\n",
" {\n",
" Tuple<int,double,std::string> tuple = { 42, 3.14, \"bonjour\" } ;\n",
" Tuple<int,double,std::string> tuple { 42, 3.14, \"bonjour\" } ;\n",
" std::cout << tuple.data << std::endl ;\n",
" std::cout << tuple.others.data << std::endl ;\n",
" std::cout << tuple.others.others.data << std::endl ;\n",
......
......@@ -118,7 +118,7 @@
"outputs": [],
"source": [
"constexpr int t1 = 2 ;\n",
"const int t2 = 2 ;\n",
"int const t2 = 2 ;\n",
"int t3 = 2 ;\n",
"//..."
]
......@@ -198,7 +198,7 @@
"class Point\n",
" {\n",
" public :\n",
" constexpr Point( int x, int y) : m_x(x), m_y(y) {}\n",
" constexpr Point( int x, int y ) : m_x {x}, m_y {y} {}\n",
" private :\n",
" int m_x, m_y ;\n",
" } ;"
......
......@@ -35,7 +35,7 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 2,
"metadata": {
"slideshow": {
"slide_type": "skip"
......@@ -150,7 +150,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 5,
"metadata": {
"attributes": {
"classes": [
......@@ -167,14 +167,14 @@
"name": "stderr",
"output_type": "stream",
"text": [
"\u001b[1minput_line_12:7:30: \u001b[0m\u001b[0;1;31merror: \u001b[0m\u001b[1mvariable 'm' cannot be implicitly captured in a lambda with no capture-default specified\u001b[0m\n",
" []( int n ){ std::cout << (m*n) << \" \" ; }\n",
"\u001b[1minput_line_11:7:30: \u001b[0m\u001b[0;1;31merror: \u001b[0m\u001b[1mvariable 'coef' cannot be implicitly captured in a lambda with no capture-default specified\u001b[0m\n",
" []( int n ){ std::cout << (coef*n) << \" \" ; }\n",
"\u001b[0;1;32m ^\n",
"\u001b[0m\u001b[1minput_line_12:3:5: \u001b[0m\u001b[0;1;30mnote: \u001b[0m'm' declared here\u001b[0m\n",
"int m = 3 ;\n",
"\u001b[0m\u001b[1minput_line_11:3:5: \u001b[0m\u001b[0;1;30mnote: \u001b[0m'coef' declared here\u001b[0m\n",
"int coef = 3 ;\n",
"\u001b[0;1;32m ^\n",
"\u001b[0m\u001b[1minput_line_12:7:3: \u001b[0m\u001b[0;1;30mnote: \u001b[0mlambda expression begins here\u001b[0m\n",
" []( int n ){ std::cout << (m*n) << \" \" ; }\n",
"\u001b[0m\u001b[1minput_line_11:7:3: \u001b[0m\u001b[0;1;30mnote: \u001b[0mlambda expression begins here\u001b[0m\n",
" []( int n ){ std::cout << (coef*n) << \" \" ; }\n",
"\u001b[0;1;32m ^\n",
"\u001b[0m"
]
......@@ -190,11 +190,11 @@
],
"source": [
"std::vector<int> numbers { 1, 2, 3, 4, 5 } ;\n",
"int m = 3 ;\n",
"int coef = 3 ;\n",
"std::for_each\n",
" (\n",
" numbers.begin(),numbers.end(),\n",
" []( int n ){ std::cout << (m*n) << \" \" ; }\n",
" []( int n ){ std::cout << (coef*n) << \" \" ; }\n",
" ) ;"
]
},
......@@ -230,7 +230,7 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 6,
"metadata": {
"attributes": {
"classes": [
......@@ -253,11 +253,11 @@
],
"source": [
"std::vector<int> numbers { 1, 2, 3, 4, 5 } ;\n",
"int m = 3 ;\n",
"int coef = 3 ;\n",
"std::for_each\n",
" (\n",
" numbers.begin(),numbers.end(),\n",
" [m]( int n ){ std::cout << (m*n) << \" \" ; }\n",
" [coef]( int n ){ std::cout << (coef*n) << \" \" ; }\n",
" ) ;"
]
},
......@@ -274,7 +274,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 3,
"metadata": {
"slideshow": {
"slide_type": "-"
......@@ -285,16 +285,16 @@
"class Multiplier\n",
" {\n",
" public :\n",
" Multiplier( int m ) : m_(m) {}\n",
" void operator() ( int n ) { std::cout << (m_*n) << \" \" ; }\n",
" Multiplier( int coef ) : m_coef(coef) {}\n",
" void operator() ( int n ) { std::cout << (m_coef*n) << \" \" ; }\n",
" private :\n",
" int m_ ;\n",
" int const m_coef ;\n",
" } ;"
]
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 4,
"metadata": {
"attributes": {
"classes": [
......@@ -316,7 +316,7 @@
}
],
"source": [
"std::vector<int> numbers = { 1, 2, 3, 4, 5 } ;\n",
"std::vector<int> numbers { 1, 2, 3, 4, 5 } ;\n",
"std::for_each(numbers.begin(),numbers.end(),Multiplier(3)) ;"
]
},
......@@ -368,12 +368,12 @@
}
],
"source": [
"std::vector<int> numbers = { 1, 2, 3, 4, 5 } ;\n",
"int m = 3 ;\n",
"std::vector<int> numbers { 1, 2, 3, 4, 5 } ;\n",
"int coef = 3 ;\n",
"std::for_each\n",
" (\n",
" numbers.begin(),numbers.end(),\n",
" [m]( int n ) -> void { std::cout << (m*n) << \" \" ; }\n",
" [coef]( int n ) -> void { std::cout << (coef*n) << \" \" ; }\n",
" ) ;"
]
},
......@@ -403,7 +403,7 @@
},
{
"cell_type": "code",
"execution_count": 19,
"execution_count": 7,
"metadata": {
"slideshow": {
"slide_type": "skip"
......@@ -414,7 +414,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Overwriting tmp.lambdas.cpp\n"
"Writing tmp.lambdas.cpp\n"
]
}
],
......@@ -441,18 +441,18 @@
" } ;\n",
"\n",
"// main program\n",
"int main ( int argc, char * argv[] )\n",
"int main()\n",
" {\n",
" int dim {atoi(argv[1])} ;\n",
" int degree {atoi(argv[2])} ;\n",
" int const DIM {10} ;\n",
" int const DEGREE {5} ;\n",
" \n",
" // generate random input\n",
" std::vector<double> input(dim) ;\n",
" std::vector<double> input(DIM) ;\n",
" std::for_each(input.begin(),input.end(),random_unit) ;\n",
"\n",
" // compute output\n",
" std::vector<double> output(dim) ;\n",
" std::transform(input.begin(),input.end(),output.begin(),Pow(degree)) ;\n",
" std::vector<double> output(DIM) ;\n",
" std::transform(input.begin(),input.end(),output.begin(),Pow(DEGREE)) ;\n",
" \n",
" // print sum\n",
" std::cout<<std::accumulate(output.begin(),output.end(),0.)<<std::endl ;\n",
......@@ -461,7 +461,7 @@
},
{
"cell_type": "code",
"execution_count": 20,
"execution_count": 8,
"metadata": {
"slideshow": {
"slide_type": "skip"
......@@ -474,7 +474,7 @@
},
{
"cell_type": "code",
"execution_count": 21,
"execution_count": 9,
"metadata": {
"slideshow": {
"slide_type": "skip"
......@@ -490,7 +490,7 @@
}
],
"source": [
"!./tmp.lambdas.exe 10 5"
"!./tmp.lambdas.exe"
]
},
{
......
......@@ -27,7 +27,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 7,
"id": "505f163e",
"metadata": {},
"outputs": [
......@@ -58,7 +58,7 @@
" }\n",
"\n",
"// fill an array of random complexes\n",
"void random( std::complex<real> * cs, int dim )\n",
"void random( std::complex<real> * cs, std::size_t dim )\n",
" {\n",
" std::srand(1) ;\n",
" for ( int i = 0 ; i < dim ; ++i )\n",
......@@ -75,14 +75,14 @@
" }\n",
"\n",
"// raise an array of complexes to power degree\n",
"void power( std::complex<real> * cs, int dim, int degree )\n",
"void power( std::complex<real> * cs, std::size_t dim, int degree )\n",
" {\n",
" for ( int i = 0 ; i < dim ; ++i )\n",
" { cs[i] = power(cs[i],degree) ; }\n",
" }\n",
"\n",
"// return the global product of n complex numbers \n",
"std::complex<real> product( const std::complex<real> * cs, int dim )\n",
"std::complex<real> product( const std::complex<real> * cs, std::size_t dim )\n",
" {\n",
" std::complex<real> res(1.,0.) ;\n",
" for ( int i = 0 ; i < dim ; ++i )\n",
......@@ -91,23 +91,23 @@
" }\n",
"\n",
"// main program\n",
"int main( int argc, char * argv[] )\n",
"int main()\n",
" {\n",
" // input parameters\n",
" int dim = atoi(argv[1]) ;\n",
" int degree = atoi(argv[2]) ;\n",
" // general parameters\n",
" const std::size_t DIM = 10 ;\n",
" const int DEGREE = 5 ;\n",
" \n",
" // allocate memory\n",
" std::complex<real> * cs = new std::complex<real> [dim] ;\n",
" std::complex<real> * cs = new std::complex<real> [DIM] ;\n",
" \n",
" // generate random input\n",
" random(cs,dim) ;\n",
" random(cs,DIM) ;\n",
"\n",
" // compute output\n",
" power(cs,dim,degree) ;\n",
" power(cs,DIM,DEGREE) ;\n",
" \n",
" // check result\n",
" std::cout<<product(cs,dim)<<std::endl ;\n",
" std::cout<<product(cs,DIM)<<std::endl ;\n",
" \n",
" // release memory\n",
" delete [] cs ;\n",
......@@ -119,7 +119,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 8,
"id": "9d1aa2cf",
"metadata": {},
"outputs": [],
......@@ -129,7 +129,7 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 9,
"id": "3a940bec",
"metadata": {},
"outputs": [
......@@ -142,7 +142,7 @@
}
],
"source": [
"!./tmp.dojo.exe 10 5"
"!./tmp.dojo.exe"
]
},
{
......
......@@ -58,7 +58,7 @@
" }\n",
"\n",
"// fill an array of random complexes\n",
"void random( std::complex<real> * cs, int dim )\n",
"void random( std::complex<real> * cs, std::size_t dim )\n",
" {\n",
" std::srand(1) ;\n",
" for ( int i = 0 ; i < dim ; ++i )\n",
......@@ -75,14 +75,14 @@
" }\n",
"\n",
"// raise an array of complexes to power degree\n",
"void power( std::complex<real> * cs, int dim, int degree )\n",
"void power( std::complex<real> * cs, std::size_t dim, int degree )\n",
" {\n",
" for ( int i = 0 ; i < dim ; ++i )\n",
" { cs[i] = power(cs[i],degree) ; }\n",
" }\n",
"\n",
"// return the global product of n complex numbers \n",
"std::complex<real> product( const std::complex<real> * cs, int dim )\n",
"std::complex<real> product( const std::complex<real> * cs, std::size_t dim )\n",
" {\n",
" std::complex<real> res(1.,0.) ;\n",
" for ( int i = 0 ; i < dim ; ++i )\n",
......@@ -91,23 +91,23 @@
" }\n",
"\n",
"// main program\n",
"int main( int argc, char * argv[] )\n",
"int main()\n",
" {\n",
" // input parameters\n",
" int dim = atoi(argv[1]) ;\n",
" int degree = atoi(argv[2]) ;\n",
" // general parameters\n",
" const std::size_t DIM = 10 ;\n",
" const int DEGREE = 5 ;\n",
" \n",
" // allocate memory\n",
" std::complex<real> * cs = new std::complex<real> [dim] ;\n",
" std::complex<real> * cs = new std::complex<real> [DIM] ;\n",
" \n",
" // generate random input\n",
" random(cs,dim) ;\n",
" random(cs,DIM) ;\n",
"\n",
" // compute output\n",
" power(cs,dim,degree) ;\n",
" power(cs,DIM,DEGREE) ;\n",
" \n",
" // check result\n",
" std::cout<<product(cs,dim)<<std::endl ;\n",
" std::cout<<product(cs,DIM)<<std::endl ;\n",
" \n",
" // release memory\n",
" delete [] cs ;\n",
......@@ -129,7 +129,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 3,
"id": "91d760ae",
"metadata": {},
"outputs": [
......@@ -142,7 +142,7 @@
}
],
"source": [
"!./tmp.dojo.exe 10 5"
"!./tmp.dojo.exe"
]
},
{
......
......@@ -4,13 +4,13 @@
class Particule {
public :
explicit Particule( double masse ) : masse_(masse) {}
explicit Particule( double masse ) : m_masse {masse} {}
Particule( Particule const & ) = delete ;
double masse() { return masse_ ; }
double masse() { return m_masse ; }
virtual std::string nom() { return "Particule" ; }
virtual ~Particule() = default ;
private :
double masse_ = 0 ;
double m_masse = 0 ;
} ;
class ParticuleNonChargee : public Particule {
......@@ -23,12 +23,12 @@ public :
class ParticuleChargee : public Particule {
public :
ParticuleChargee( double masse, double charge )
: Particule(masse), charge_(charge) {}
double charge() { return charge_ ; }
: Particule(masse), m_charge {charge} {}
double charge() { return m_charge ; }
virtual std::string nom() override
{ return "ParticuleChargee" ; }
private :
double charge_ ;
double m_charge ;
} ;
void affiche( Particule & p ) {
......@@ -38,16 +38,16 @@ void affiche( Particule & p ) {
int main() {
std::random_device rd ;
std::default_random_engine gen(rd()) ;
std::uniform_int_distribution<unsigned> dis(0,1) ;
std::default_random_engine gen {rd()} ;
std::uniform_int_distribution<unsigned> dis {0,1} ;
unsigned alea = dis(gen) ;
if (alea==0) {
ParticuleNonChargee p(2) ;
ParticuleNonChargee p {2} ;
affiche(p) ;
}
else if (alea==1) {
ParticuleChargee p(1,1) ;
ParticuleChargee p {1,1} ;
affiche(p) ;
std::cout << "charge = " << p.charge() << std::endl ;
}
......
......@@ -5,24 +5,24 @@
class Particle
{
public :
Particle( double mass ) : mass_(mass) {}
Particle( double mass ) : m_mass {mass} {}
Particle( Particle const & ) = delete ;
virtual ~Particle() = default ;
double mass() { return mass_ ; }
double mass() { return m_mass ; }
virtual std::string name() { return "Particle" ; }
private :
double mass_ ;
double m_mass ;
} ;
class ChargedParticle : public Particle
{
public :
ChargedParticle( double mass, double charge )
: Particle(mass), charge_(charge) {}
double charge() { return charge_ ; }
: Particle(mass), m_charge {charge} {}
double charge() { return m_charge ; }
std::string name() override { return "ChargedParticle" ; }
private :
double charge_ = 0.0 ;
double m_charge = 0.0 ;
} ;
void display( Particle & p )
......@@ -37,12 +37,12 @@ int main()
{
if ( std::rand() < (0.5 * double(RAND_MAX)) )
{
Particle p(2) ;
Particle p {2} ;
display(p) ;
}
else
{
ChargedParticle p(1,1) ;
ChargedParticle p {1,1} ;
display(p) ;
std::cout << " charge = " << p.charge() << std::endl ;
}
......
......@@ -7,15 +7,15 @@ public :
static_assert(MAX>1,"Nombre max de caractères par mot TROP BAS.") ;
Mot( char const * texte ) {
for ( unsigned i = 0 ; i<MAX ; ++i ) {
donnees_[i] = texte[i] ;
if (!donnees_[i]) break ;
m_donnees[i] = texte[i] ;
if (!m_donnees[i]) break ;
}
donnees_[MAX-1] = 0 ;
m_donnees[MAX-1] = 0 ;
}
private :
char donnees_[MAX] ;
char m_donnees[MAX] ;
friend std::ostream & operator<<( std::ostream & os, Mot const & m ) {
return (os<<m.donnees_) ;
return (os<<m.m_donnees) ;
}
} ;
......@@ -23,12 +23,12 @@ template< unsigned MAX >
class Phrase {
public :
void ajoute( char const * texte ) {
donnees_.push_back(texte) ;
m_donnees.push_back(texte) ;
}
private :