Commit 19480907 authored by CHAMONT David's avatar CHAMONT David
Browse files

Add first intersession.

parent 7e3575b4
......@@ -984,7 +984,7 @@
"file_extension": ".cpp",
"mimetype": "text/x-c++src",
"name": "c++",
"version": "14"
"version": "17"
}
},
"nbformat": 4,
......
#include <iostream>
#include <string>
#include <random>
class Particule {
public :
explicit Particule( double masse ) : masse_(masse) {}
Particule( Particule const & ) = delete ;
double masse() { return masse_ ; }
virtual std::string nom() { return "Particule" ; }
virtual ~Particule() = default ;
private :
double masse_ = 0 ;
} ;
class ParticuleNonChargee : public Particule {
public :
using Particule::Particule ;
virtual std::string nom() override
{ return "ParticuleNonChargee" ; }
} ;
class ParticuleChargee : public Particule {
public :
ParticuleChargee( double masse, double charge )
: Particule(masse), charge_(charge) {}
double charge() { return charge_ ; }
virtual std::string nom() override
{ return "ParticuleChargee" ; }
private :
double charge_ ;
} ;
void affiche( Particule & p ) {
std::cout << p.nom() << std::endl ;
std::cout << "masse = " << p.masse() << std::endl ;
}
int main() {
std::random_device rd ;
std::default_random_engine gen(rd()) ;
std::uniform_int_distribution<unsigned> dis(0,1) ;
unsigned alea = dis(gen) ;
if (alea==0) {
ParticuleNonChargee p(2) ;
affiche(p) ;
}
else if (alea==1) {
ParticuleChargee p(1,1) ;
affiche(p) ;
std::cout << "charge = " << p.charge() << std::endl ;
}
}
\ No newline at end of file
#include <cstdlib> // for std::rand()
#include <iostream>
#include <string>
class Particle
{
public :
Particle( double mass ) : mass_(mass) {}
Particle( const Particle & ) = delete ;
virtual ~Particle() = default ;
double mass() { return mass_ ; }
virtual std::string name() { return "Particle" ; }
private :
double mass_ ;
} ;
class ChargedParticle : public Particle
{
public :
ChargedParticle( double mass, double charge )
: Particle(mass), charge_(charge) {}
double charge() { return charge_ ; }
std::string name() override { return "ChargedParticle" ; }
private :
double charge_ = 0.0 ;
} ;
void display( Particle & p )
{
std::cout << p.name() << std::endl ;
std::cout << " mass = " << p.mass() << std::endl ;
}
int main()
{
for ( int i = 0 ; i < 5 ; ++i )
{
if ( std::rand() < (0.5 * double(RAND_MAX)) )
{
Particle p(2) ;
display(p) ;
}
else
{
ChargedParticle p(1,1) ;
display(p) ;
std::cout << " charge = " << p.charge() << std::endl ;
}
}
}
\ No newline at end of file
#include <iostream>
#include <list>
template< unsigned MAX >
class Mot {
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 ;
}
donnees_[MAX-1] = 0 ;
}
private :
char donnees_[MAX] ;
friend std::ostream & operator<<( std::ostream & os, Mot const & m ) {
return (os<<m.donnees_) ;
}
} ;
template< unsigned MAX >
class Phrase {
public :
void ajoute( char const * texte ) {
donnees_.push_back(texte) ;
}
private :
std::list<Mot<MAX> > donnees_ ;
friend std::ostream & operator<<( std::ostream & os, Phrase const & phr ) {
for ( const auto & mot : phr.donnees_ ) {
os<<mot<<" " ;
}
return os ;
}
} ;
int main() {
Phrase<10> phrase ;
phrase.ajoute("Bonjour") ;
phrase.ajoute("le") ;
phrase.ajoute("monde") ;
phrase.ajoute("!") ;
std::cout<<phrase<<std::endl ;
}
#include <cassert>
#include <iostream>
#include <list>
template< typename Real >
class Point
{
public :
Point( Real x, Real y )
: x_(x), y_(y) {}
friend std::ostream & operator<<( std::ostream & os, const Point & p )
{ return (os<<"("<<p.x_<<","<<p.y_<<")") ; }
private :
Real x_, y_ ;
} ;
enum class BrokenLineKind { Empty, Opened, Closed } ;
template< typename Real >
class BrokenLine
{
public :
void add( Point<Real> p )
{ points_.push_back(p) ; kind_ = BrokenLineKind::Opened ; }
void close()
{ assert(points_.size()>0) ; kind_ = BrokenLineKind::Closed ; }
friend std::ostream & operator<<( std::ostream & os, const BrokenLine & bl )
{
char sep = '(' ;
for ( auto p : bl.points_ )
{ os<<sep<<p ; sep = ',' ; }
if (bl.kind_==BrokenLineKind::Closed)
{ os<<sep<<bl.points_.front() ; }
return (os<<')') ;
}
private :
std::list<Point<Real>> points_ ;
BrokenLineKind kind_ = BrokenLineKind::Empty ;
} ;
long double operator ""_km ( long double value )
{ return 1000.*value ; }
long double operator ""_m ( long double value )
{ return 1.*value ; }
long double operator ""_mm ( long double value )
{ return 0.001*value ; }
int main()
{
BrokenLine<double> bl ;
bl.add(Point<double>(1000.0_mm,1.0_m)) ;
bl.add(Point<double>(0.002_km,3000.0_mm)) ;
bl.add(Point<double>(4.0_m,0.002_km)) ;
bl.close() ;
std::cout<<bl<<std::endl ;
}
\ No newline at end of file
#include <iostream>
auto add( auto a, auto b )
{ return (a+b) ; }
int main()
{
std::cout<<add(10,32)<<std::endl ;
std::cout<<add(1,2.14)<<std::endl ;
std::cout<<add(1.0,2.14)<<std::endl ;
}
#include <iostream>
#include <list>
template< typename Real >
class Point
{
public :
Point( Real x, Real y )
: x_{x}, y_{y} {}
friend std::ostream & operator<<( std::ostream & os, const Point & p )
{ return (os<<"{"<<p.x_<<","<<p.y_<<"}") ; }
private :
Real x_, y_ ;
} ;
enum class BrokenLineKind { Empty, Opened, Closed } ;
template< typename Real >
class BrokenLine
{
public :
BrokenLine( const std::initializer_list<Point<Real>> points, bool closed )
: points_(points.begin(),points.end())
{
if (!points.size()) return ;
if (closed)
{ kind_ = BrokenLineKind::Closed ; }
else
{ kind_ = BrokenLineKind::Opened ; }
}
friend std::ostream & operator<<( std::ostream & os, const BrokenLine & bl )
{
char sep { '{' } ;
for ( auto p : bl.points_ )
{ os<<sep<<p ; sep = ',' ; }
if (bl.kind_==BrokenLineKind::Closed)
{ os<<sep<<bl.points_.front() ; }
return (os<<'}') ;
}
private :
std::list<Point<Real>> points_ ;
BrokenLineKind kind_ = BrokenLineKind::Empty ;
} ;
int main()
{
BrokenLine<double> bl { { {1,1}, {2,3}, {4,2} }, true } ;
std::cout<<bl<<std::endl ;
}
\ No newline at end of file
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Modern C++ Syntax - Basic Features\n",
"\n",
"1. [Syntactic Sugar](en.1-syntactic-sugar.ipynb)\n",
"1. [Type Inference](en.2-type-inference.ipynb)\n",
"1. [Uniform Initialization](en.3-uniform-initialization.ipynb)\n",
"1. [Objects and Classes](en.4-objects-and-classes.ipynb)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "C++17",
"language": "C++17",
"name": "xcpp17"
},
"language_info": {
"codemirror_mode": "text/x-c++src",
"file_extension": ".cpp",
"mimetype": "text/x-c++src",
"name": "c++",
"version": "17"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# Basic syntactic sugar\n",
"\n",
"We group here some small modern features, added to ease your developer life."
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## At last, `>>` allowed for templates of templates\n",
"\n",
"When nesting template arguments, no more need to insert a space between subsequent \"<\" or \">\"."
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## At last, default member variable initialization\n",
"\n",
"In a class or struct definition, one can now give a default value to any member variable."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"#include <iostream>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"struct MyInt {\n",
" MyInt() {}\n",
" MyInt( int x ) : x_(x) {}\n",
" int x_ = 42 ; // default value\n",
"} ;"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"Int i ;\n",
"Int j(1) ;\n",
"std::cout << i.x_ << std::endl ;\n",
"std::cout << j.x_ << std::endl ;"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Preview of the keyword `auto`\n",
"\n",
"Tired of typing those painful iterator types, although what you want is so obvious ? When defining a variable with an initial value, you can now let the C++11 compiler deduce the type : jsut say `auto` instead of the type name. Would you guess how to do it below ?"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"#include <list>\n",
"#include <map>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"std::map<std::vector<int>,std::list<float>> m ;\n",
"std::map<std::vector<int>,std::list<float>>::iterator itr = m.begin() ;"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Preview of the \"range-based for\""
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"A new flavor of `for` enables to process simply all the elements of a collection, provided this collection has methods `begin()` and `end()`. Instead of:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"const int MAX = 5 ;\n",
"double values[MAX] = { 1.1, 2.2, 3.3, 4.4, 5.5 } ;\n",
"for ( int indice = 0 ; indice <MAX ; ++indice )\n",
" { std::cout << values[indice] << \" \" ; }\n",
"std::cout << std::endl ;"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"One can now write:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"const int MAX = 5 ;\n",
"double values[MAX] = { 1.1, 2.2, 3.3, 4.4, 5.5 } ;\n",
"for ( double value : values )\n",
" { std::cout << value << \" \" ; }\n",
"std::cout << std::endl ;"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"If you combine with `auto`, instead of:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"std::vector<int> values(5,0) ;\n",
"std::vector<int>::iterator itr ;\n",
"for ( itr = values.begin() ; itr != values.end() ; ++itr )\n",
" { std::cout << *itr << \" \" ; }\n",
"std::cout << std::endl ;"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"One can now write:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"std::vector<int> values(5,0) ;\n",
"for ( auto value : values )\n",
" { std::cout << value << \" \" ; }\n",
"std::cout << std::endl ;"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Scoped enumerations"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"In ancient C++, the enumerations let you define constants which are considered as integers. This is both handy and sometimes tricky. Try the code below."
]
},
{
"cell_type": "code",